@@ -27,6 +27,7 @@ public static Geometry extractPoint(Geometry g, double index)
2727 Coordinate p = ll .extractPoint (index );
2828 return g .getFactory ().createPoint (p );
2929 }
30+
3031 public static Geometry extractLine (Geometry g ,
3132 @ Metadata (title ="Start length" )
3233 double start ,
@@ -36,28 +37,48 @@ public static Geometry extractLine(Geometry g,
3637 LengthIndexedLine ll = new LengthIndexedLine (g );
3738 return ll .extractLine (start , end );
3839 }
39- public static Geometry project (Geometry g , Geometry g2 )
40+
41+ public static Geometry project (Geometry line , Geometry geom )
4042 {
41- LengthIndexedLine ll = new LengthIndexedLine (g );
42- if (g2 .getDimension () == 1 ) {
43- LineString line = (LineString ) g2 .getGeometryN (0 );
44- Coordinate pStart = line .getCoordinateN (0 );
45- Coordinate pEnd = line .getCoordinateN (line .getNumPoints () - 1 );
46- double indexStart = ll .project (pStart );
47- double indexEnd = ll .project (pEnd );
48- Geometry lineProj = ll .extractLine (indexStart , indexEnd );
49- return lineProj ;
43+ LengthIndexedLine ll = new LengthIndexedLine (line );
44+ if (geom .getDimension () == 0 ) {
45+ Coordinate [] projPts = new Coordinate [geom .getNumPoints ()];
46+ for (int i = 0 ; i < geom .getNumPoints (); i ++) {
47+ Coordinate pt = geom .getGeometryN (i ).getCoordinate ();
48+ double index = ll .project (pt );
49+ Coordinate p = ll .extractPoint (index );
50+ }
51+ if (projPts .length == 1 ) {
52+ return geom .getFactory ().createPoint (projPts [0 ]);
53+ }
54+ return geom .getFactory ().createMultiPointFromCoords (projPts );
5055 }
5156 else {
52- double index = ll .project (g2 .getCoordinate ());
53- Coordinate p = ll .extractPoint (index );
54- return g .getFactory ().createPoint (p );
57+ return projectOnLine (line , geom );
58+ }
59+ }
60+
61+ private static Geometry projectOnLine (Geometry line , Geometry geom ) {
62+ Coordinate [] bPts = geom .getCoordinates ();
63+
64+ LengthIndexedLine aLR = new LengthIndexedLine ((LineString ) line );
65+
66+ double locStart = -1.0 ;
67+ double locEnd = -1.0 ;
68+ for (int i = 0 ; i < bPts .length ; i ++) {
69+ Coordinate maskPt = bPts [i ];
70+ double loc = aLR .indexOf (maskPt );
71+ if (locStart < 0 || loc < locStart ) locStart = loc ;
72+ if (loc < 0 || loc > locEnd ) locEnd = loc ;
5573 }
74+
75+ return aLR .extractLine (locStart , locEnd );
5676 }
57- public static double projectIndex (Geometry g , Geometry g2 )
77+
78+ public static double projectIndex (Geometry line , Geometry geom )
5879 {
59- LengthIndexedLine ll = new LengthIndexedLine (g );
60- return ll .project (g2 .getCoordinate ());
80+ LengthIndexedLine ll = new LengthIndexedLine (line );
81+ return ll .project (geom .getCoordinate ());
6182 }
6283
6384}
0 commit comments