Revision 194

View differences:

org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/topology/checkLineDirectionCoherence/CheckLineDirectionCoherenceAlgorithm.java
8 8
import java.util.Iterator;
9 9
import java.util.Set;
10 10

  
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

  
11 14
import com.vividsolutions.jts.geom.Coordinate;
12 15
import com.vividsolutions.jts.geom.Geometry;
13 16
import com.vividsolutions.jts.geom.GeometryFactory;
......
21 24
import es.unex.sextante.dataObjects.IVectorLayer;
22 25
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter;
23 26
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
27
import es.unex.sextante.exceptions.OptionalParentParameterException;
24 28
import es.unex.sextante.exceptions.RepeatedParameterNameException;
29
import es.unex.sextante.exceptions.UndefinedParentParameterNameException;
25 30
import es.unex.sextante.outputs.OutputVectorLayer;
26 31

  
27 32

  
......
29 34
         extends
30 35
            GeoAlgorithm {
31 36

  
37
    private static final Logger LOG =
38
        LoggerFactory.getLogger(CheckLineDirectionCoherenceAlgorithm.class);
39

  
32 40
   public static final String  RESULT    = "RESULT";
33 41
   public static final String  LINES     = "LINES";
34 42
   public static final String  FIELD     = "FIELD";
......
49 57

  
50 58
      try {
51 59
         m_Parameters.addInputVectorLayer(LINES, Sextante.getText("Lines_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true);
60
         m_Parameters.addTableField(FIELD, Sextante.getText("Field"), LINES);
52 61
         m_Parameters.addNumericalValue(TOLERANCE, Sextante.getText("Tolerance"),
53 62
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 1, 0, Double.MAX_VALUE);
54 63
         addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_LINE);
64
      } catch (final RepeatedParameterNameException e) {
65
          LOG.error("Repeated parameter name", e);
66
          return;
67
      } catch (UndefinedParentParameterNameException e) {
68
          LOG.error("Undefined parameter name", e);
69
          return;
70
      } catch (OptionalParentParameterException e) {
71
          LOG.error("Optional parent parameter", e);
72
          return;
55 73
      }
56
      catch (final RepeatedParameterNameException e) {
57

  
58
      }
59

  
60 74
   }
61 75

  
62 76

  
......
112 126
   private void processLines(final ArrayList<IFeature> lines) {
113 127

  
114 128
      final BitSet bitset = new BitSet(lines.size());
115
      while (bitset.cardinality() != bitset.length()) {
129
      while (bitset.cardinality() != bitset.length() || bitset.isEmpty()) {
116 130
         final IFeature line = getExtremeLine(lines, bitset);
117 131
         m_Output.addFeature(line);
118 132
         followLine(line, lines, bitset);
119 133
      }
120

  
121

  
134
      for (int i = 0; i < lines.size(); i++) {
135
          if(!bitset.get(i)){
136
              IFeature line = lines.get(i);
137
              m_Output.addFeature(line);
138
          }
139
      }
122 140
   }
123 141

  
124 142

  
......
131 149
      for (int i = 0; i < lines.size(); i++) {
132 150
         if (!bitset.get(i)) {
133 151
            final Coordinate[] coords = line.getCoordinates();
134
            final Geometry line2 = lines.get(i).getGeometry();
135 152
            for (int j = 0; j < lines.size(); j++) {
153
                IFeature feature2 = lines.get(j);
154
                final Geometry line2 = feature2.getGeometry();
136 155
               final Coordinate[] coords2 = line2.getCoordinates();
137
               if (coords2[0].distance(coords[coords.length - 1]) < m_dTolerance) {
138
                  m_Output.addFeature(feature);
139
                  bitset.set(i);
140
                  followLine(lines.get(i), lines, bitset);
156
               if (!bitset.get(j) && !line.equals(line2) && coords2[0].distance(coords[coords.length - 1]) < m_dTolerance) {
157
                  m_Output.addFeature(feature2);
158
                  bitset.set(j);
159
                  followLine(feature2, lines, bitset);
141 160
               }
142 161
            }
143 162
         }
......
145 164
      for (int i = 0; i < lines.size(); i++) {
146 165
         if (!bitset.get(i)) {
147 166
            final Coordinate[] coords = line.getCoordinates();
148
            final Geometry line2 = lines.get(i).getGeometry();
149 167
            for (int j = 0; j < lines.size(); j++) {
168
                IFeature feature2 = lines.get(j);
169
                final Geometry line2 = feature2.getGeometry();
150 170
               final Coordinate[] coords2 = line2.getCoordinates();
151
               if (coords2[coords2.length - 1].distance(coords[coords.length - 1]) < m_dTolerance) {
171
               if (!bitset.get(j) && !line.equals(line2) && coords2[coords2.length - 1].distance(coords[coords.length - 1]) < m_dTolerance) {
152 172
                  final Geometry inverted = invertDirection(feature.getGeometry());
153
                  m_Output.addFeature(inverted, feature.getRecord().getValues());
154
                  bitset.set(i);
155
                  followLine(lines.get(i), lines, bitset);
173
                  m_Output.addFeature(inverted, feature2.getRecord().getValues());
174
                  bitset.set(j);
175
                  followLine(lines.get(j), lines, bitset);
156 176
               }
157 177
            }
158 178
         }
......
176 196

  
177 197

  
178 198
   private IFeature getExtremeLine(final ArrayList<IFeature> lines,
179
                                   final BitSet bitset) {
199
 final BitSet bitset) {
180 200

  
181
      for (int i = 0; i < lines.size(); i++) {
182
         if (!bitset.get(i)) {
183
            final Geometry line = lines.get(i).getGeometry();
184
            final Coordinate[] coords = line.getCoordinates();
185
            boolean bHasContiguousLineOnStart = false;
186
            for (int j = 0; j < lines.size(); j++) {
187
               final Coordinate[] coords2 = line.getCoordinates();
188
               if ((coords2[0].distance(coords[0]) < m_dTolerance)
189
                   || (coords2[coords2.length - 1].distance(coords[0]) < m_dTolerance)) {
190
                  bHasContiguousLineOnStart = true;
191
                  break;
192
               }
201
        for (int i = 0; i < lines.size(); i++) {
202
            if (!bitset.get(i)) {
203
                final Geometry line = lines.get(i).getGeometry();
204
                final Coordinate[] coords = line.getCoordinates();
205
                boolean bHasContiguousLineOnStart = false;
206
                for (int j = 0; j < lines.size(); j++) {
207
                    final Geometry line2 = lines.get(j).getGeometry();
208

  
209
                    final Coordinate[] coords2 = line2.getCoordinates();
210
                    if (i!=j && ((coords2[0].distance(coords[0]) < m_dTolerance)
211
                        || (coords2[coords2.length - 1].distance(coords[0]) < m_dTolerance))) {
212
                        bHasContiguousLineOnStart = true;
213
                        break;
214
                    }
215
                }
216
                boolean bHasContiguousLineOnEnd = false;
217
                for (int j = 0; j < lines.size(); j++) {
218
                    final Geometry line2 = lines.get(j).getGeometry();
219
                    final Coordinate[] coords2 = line2.getCoordinates();
220
                    if (i!=j && ((coords2[0].distance(coords[coords.length - 1]) < m_dTolerance)
221
                        || (coords2[coords2.length - 1].distance(coords[coords.length - 1]) < m_dTolerance))) {
222
                        bHasContiguousLineOnEnd = true;
223
                        break;
224
                    }
225
                }
226
                if (bHasContiguousLineOnEnd != bHasContiguousLineOnStart) {
227
                    bitset.set(i);
228
                    return lines.get(i);
229
                }
193 230
            }
194
            boolean bHasContiguousLineOnEnd = false;
195
            for (int j = 0; j < lines.size(); j++) {
196
               final Coordinate[] coords2 = line.getCoordinates();
197
               if ((coords2[0].distance(coords[coords.length - 1]) < m_dTolerance)
198
                   || (coords2[coords2.length - 1].distance(coords[coords.length - 1]) < m_dTolerance)) {
199
                  bHasContiguousLineOnEnd = true;
200
                  break;
201
               }
231
        }
232

  
233
        for (int i = 0; i < lines.size(); i++) {
234
            if (!bitset.get(i)) {
235
                bitset.set(i);
236
                return lines.get(i);
202 237
            }
203
            if (bHasContiguousLineOnEnd != bHasContiguousLineOnStart) {
204
               bitset.set(i);
205
               return lines.get(i);
206
            }
207
         }
208
      }
238
        }
209 239

  
210
      for (int i = 0; i < lines.size(); i++) {
211
         if (!bitset.get(i)) {
212
            bitset.set(i);
213
            return lines.get(i);
214
         }
215
      }
240
        return null;
216 241

  
217
      return null;
242
    }
218 243

  
219
   }
220

  
221 244
}

Also available in: Unified diff