|
| 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 | +} |
0 commit comments