Skip to content

Commit 7e9a424

Browse files
committed
Addd TestBuilder metric functions
1 parent 870db27 commit 7e9a424

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2025 Martin Davis.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
8+
* and the Eclipse Distribution License is available at
9+
*
10+
* http://www.eclipse.org/org/documents/edl-v10.php.
11+
*/
12+
package org.locationtech.jtstest.function;
13+
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
import org.locationtech.jts.geom.Coordinate;
19+
import org.locationtech.jts.geom.CoordinateSequence;
20+
import org.locationtech.jts.geom.CoordinateSequenceFilter;
21+
import org.locationtech.jts.geom.Geometry;
22+
import org.locationtech.jtstest.geomfunction.Metadata;
23+
24+
public class MetricFunctions {
25+
26+
/**
27+
* Returns a line graph of segment lengths.
28+
* Graph is scaled to maximum segment length.
29+
*
30+
* @param geom geometry to sample
31+
* @param numSamples number of points in line graph
32+
* @return line graph of segment lengths
33+
*/
34+
public static Geometry segmentLengths(final Geometry geom,
35+
@Metadata(title="# of samples")
36+
int numSamples) {
37+
38+
if (numSamples < 1)
39+
numSamples = 1;
40+
41+
List<Double> segLen = new ArrayList<Double>();
42+
CoordinateSequenceFilter segLenFilter = new CoordinateSequenceFilter() {
43+
44+
@Override
45+
public void filter(CoordinateSequence seq, int i) {
46+
if (i == 0) {
47+
segLen.add(0.0);
48+
return;
49+
}
50+
Coordinate p0 = seq.getCoordinate(i);
51+
Coordinate p1 = seq.getCoordinate(i-1);
52+
double len = p0.distance(p1);
53+
segLen.add(len);
54+
}
55+
56+
@Override
57+
public boolean isDone() {
58+
return false;
59+
}
60+
61+
@Override
62+
public boolean isGeometryChanged() {
63+
return false;
64+
}
65+
66+
};
67+
geom.apply(segLenFilter);
68+
Collections.sort(segLen);
69+
70+
double maxLen = segLen.get(segLen.size() - 1);
71+
Coordinate[] pts = new Coordinate[numSamples + 1];
72+
int breakSize = segLen.size() / numSamples + 1;
73+
double dx = maxLen / numSamples;
74+
for (int i = 0; i < numSamples + 1; i++) {
75+
76+
double x = (i >= numSamples) ? maxLen : i * dx;
77+
78+
int sampleIndex = i * breakSize;
79+
if (sampleIndex >= segLen.size())
80+
sampleIndex = segLen.size() - 1;
81+
double y = segLen.get(sampleIndex);
82+
pts[i] = new Coordinate(x, y);
83+
}
84+
85+
return geom.getFactory().createLineString(pts);
86+
}
87+
}

modules/app/src/main/java/org/locationtech/jtstest/geomfunction/GeometryFunctionRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.locationtech.jtstest.function.LineHandlingFunctions;
4444
import org.locationtech.jtstest.function.LineSegmentFunctions;
4545
import org.locationtech.jtstest.function.LinearReferencingFunctions;
46+
import org.locationtech.jtstest.function.MetricFunctions;
4647
import org.locationtech.jtstest.function.NodingFunctions;
4748
import org.locationtech.jtstest.function.OffsetCurveFunctions;
4849
import org.locationtech.jtstest.function.OrientationFPFunctions;
@@ -100,6 +101,7 @@ public static GeometryFunctionRegistry createTestBuilderRegistry()
100101
funcRegistry.add(HullFunctions.class);
101102
funcRegistry.add(LinearReferencingFunctions.class);
102103
funcRegistry.add(LineHandlingFunctions.class);
104+
funcRegistry.add(MetricFunctions.class);
103105
funcRegistry.add(NodingFunctions.class);
104106
funcRegistry.add(PolygonizeFunctions.class);
105107
funcRegistry.add(PrecisionFunctions.class);

0 commit comments

Comments
 (0)