Skip to content

Commit 7044dd1

Browse files
committed
Add TesttBuilder functions for clipped and projected lines
1 parent 39eafec commit 7044dd1

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

modules/app/src/main/java/org/locationtech/jtstest/function/DistanceFunctions.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,18 @@ public static Geometry hausdorffDistanceLineDensify(Geometry a, Geometry b,
6565
}
6666

6767
@Metadata(description="Oriented Hausdorff distance from A to B")
68-
public static Geometry orientedHausdorffDistanceLine(Geometry a, Geometry b)
69-
{
68+
public static Geometry orientedHausdorffDistanceLine(Geometry a, Geometry b)
69+
{
7070
return DiscreteHausdorffDistance.orientedDistanceLine(a, b);
71-
}
71+
}
72+
73+
@Metadata(description="Oriented Hausdorff distance from A to B")
74+
public static Geometry clippedOrientedHausdorffDistanceLine(Geometry a, Geometry b)
75+
{
76+
//TODO: would this be more efficient done as part of DiscreteHausdorffDistance?
77+
Geometry clippedLine = LinearReferencingFunctions.project(a, b);
78+
return DiscreteHausdorffDistance.orientedDistanceLine(clippedLine, b);
79+
}
7280

7381
@Metadata(description="Oriented Hausdorff distance from A to B")
7482
public static double orientedHausdorffDistance(Geometry a, Geometry b)

modules/app/src/main/java/org/locationtech/jtstest/function/LineHandlingFunctions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.locationtech.jts.geom.util.LinearComponentExtracter;
2424
import org.locationtech.jts.operation.linemerge.LineMerger;
2525
import org.locationtech.jts.operation.linemerge.LineSequencer;
26+
import org.locationtech.jtstest.geomfunction.Metadata;
2627

2728
public class LineHandlingFunctions {
2829

@@ -90,4 +91,17 @@ public static Geometry dissolve(Geometry geom)
9091
return LineDissolver.dissolve(geom);
9192
}
9293

94+
/**
95+
* Clips line A to line B.
96+
* Can also be thought of as the projection of B onto A.
97+
*
98+
* @param a line to clip
99+
* @param b mask line
100+
* @return line A clipped to B
101+
*/
102+
@Metadata(description="Clip line A to line B")
103+
public static Geometry clip(Geometry a, Geometry b) {
104+
return LinearReferencingFunctions.project(a, b);
105+
}
106+
93107
}

modules/app/src/main/java/org/locationtech/jtstest/function/LinearReferencingFunctions.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)