1414
1515import java .util .ArrayList ;
1616import java .util .Collection ;
17- import java .util .Iterator ;
1817import java .util .List ;
1918import java .util .Stack ;
2019import java .util .Vector ;
@@ -46,39 +45,38 @@ public class FacetLocater
4645 * @param locations the source collection
4746 * @return a list of the vertex locations, if any
4847 */
49- public static List filterVertexLocations (Collection locations )
48+ public static List < GeometryLocation > filterVertexLocations (Collection < GeometryLocation > locations )
5049 {
51- ArrayList vertexLocs = new ArrayList ();
52- for (Iterator i = locations .iterator (); i .hasNext (); ) {
53- GeometryLocation loc = (GeometryLocation ) i .next ();
50+ ArrayList <GeometryLocation > vertexLocs = new ArrayList <GeometryLocation >();
51+ for (GeometryLocation loc : locations ) {
5452 if (loc .isVertex ()) vertexLocs .add (loc );
5553 }
5654 return vertexLocs ;
5755 }
5856
5957 private Geometry parentGeom ;
60- private List locations = new ArrayList ();
58+ private List < GeometryLocation > locations = new ArrayList < GeometryLocation > ();
6159 private Coordinate queryPt ;
6260 private double tolerance = 0.0 ;
6361
6462 public FacetLocater (Geometry parentGeom ) {
6563 this .parentGeom = parentGeom ;
6664 }
6765
68- public List getLocations (Coordinate queryPt , double tolerance )
66+ public List < GeometryLocation > getLocations (Coordinate queryPt , double tolerance )
6967 {
7068 this .queryPt = queryPt ;
7169 this .tolerance = tolerance ;
7270 findLocations (parentGeom , locations );
7371 return locations ;
7472 }
7573
76- private void findLocations (Geometry geom , List locations )
74+ private void findLocations (Geometry geom , List < GeometryLocation > locations )
7775 {
78- findLocations (new Stack (), parentGeom , locations );
76+ findLocations (new Stack < Integer > (), parentGeom , locations );
7977 }
8078
81- private void findLocations (Stack path , Geometry geom , List locations )
79+ private void findLocations (Stack < Integer > path , Geometry geom , List < GeometryLocation > locations )
8280 {
8381 if (geom instanceof GeometryCollection ) {
8482 for (int i = 0 ; i < geom .getNumGeometries (); i ++ ) {
@@ -108,7 +106,7 @@ else if (geom instanceof Point) {
108106 }
109107 }
110108
111- private void findLocations (Stack path , Polygon poly , List locations )
109+ private void findLocations (Stack < Integer > path , Polygon poly , List < GeometryLocation > locations )
112110 {
113111 path .push (0 );
114112 findLocations (path ,
@@ -125,40 +123,38 @@ private void findLocations(Stack path, Polygon poly, List locations)
125123 }
126124 }
127125
128- private void findLocations (Stack path , Geometry compGeom , CoordinateSequence seq , List locations )
126+ private void findLocations (Stack < Integer > path , Geometry compGeom , CoordinateSequence seq , List < GeometryLocation > locations )
129127 {
130- findVertexLocations (path , compGeom , seq , locations );
131- findSegmentLocations (path , compGeom , seq , locations );
132- }
133-
134- private void findVertexLocations (Stack path , Geometry compGeom , CoordinateSequence seq , List locations )
135- {
136- for (int i = 0 ; i < seq .size (); i ++) {
137- Coordinate p = seq .getCoordinate (i );
138- double dist = p .distance (queryPt );
139- if (dist <= tolerance )
140- locations .add (new GeometryLocation (parentGeom , compGeom , toIntArray (path ), i , true , p ));
141- }
142- }
143-
144- private void findSegmentLocations (Stack path , Geometry compGeom , CoordinateSequence seq , List locations )
145- {
146- LineSegment seg = new LineSegment ();
147- for (int i = 0 ; i < seq .size () - 1 ; i ++) {
128+ int lastVertexIndexAdded = -1 ;
129+ Coordinate p0 = seq .getCoordinate (0 );
130+ if (p0 .distance (queryPt ) <= tolerance ) {
131+ locations .add (new GeometryLocation (parentGeom , compGeom , toIntArray (path ), 0 , true , p0 ));
132+ lastVertexIndexAdded = 0 ;
133+ }
134+
135+ LineSegment seg = new LineSegment ();
136+ for (int i = 0 ; i < seq .size () - 1 ; i ++) {
148137 seg .p0 = seq .getCoordinate (i );
149138 seg .p1 = seq .getCoordinate (i +1 );
150- double dist = seg .distance (queryPt );
151- if (dist <= tolerance )
152- locations .add (new GeometryLocation (parentGeom , compGeom , toIntArray (path ), i , false , seg .p0 ));
153- }
139+
140+ if (seg .p1 .distance (queryPt ) <= tolerance ) {
141+ locations .add (new GeometryLocation (parentGeom , compGeom , toIntArray (path ), i +1 , true , seg .p1 ));
142+ lastVertexIndexAdded = i +1 ;
143+ }
144+ else {
145+ //-- to avoid redundancy only add segment location if vertex was NOT added
146+ double dist = seg .distance (queryPt );
147+ if (dist <= tolerance && i > lastVertexIndexAdded )
148+ locations .add (new GeometryLocation (parentGeom , compGeom , toIntArray (path ), i , false , seg .p0 ));
149+ }
150+ }
154151 }
155152
156- public static int [] toIntArray (Vector path )
153+ public static int [] toIntArray (Vector < Integer > path )
157154 {
158155 int [] index = new int [path .size ()];
159156 int i = 0 ;
160- for (Iterator it = path .iterator (); it .hasNext (); ) {
161- Integer pathIndex = (Integer ) it .next ();
157+ for (Integer pathIndex : path ) {
162158 index [i ++] = pathIndex .intValue ();
163159 }
164160 return index ;
0 commit comments