Skip to content

Commit

Permalink
Add a method to compute a distance between 2 points along a polyline
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaillandier committed Apr 14, 2021
1 parent e1521bb commit f58e057
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions msi.gama.core/src/msi/gama/common/geometry/GeometryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
********************************************************************************************************/
package msi.gama.common.geometry;

import static org.locationtech.jts.algorithm.CGAlgorithms.distancePointLine;
import static msi.gama.metamodel.shape.IShape.Type.LINESTRING;
import static msi.gama.metamodel.shape.IShape.Type.MULTILINESTRING;
import static msi.gama.metamodel.shape.IShape.Type.MULTIPOINT;
Expand All @@ -26,7 +25,7 @@
import java.util.function.Consumer;

import org.geotools.geometry.jts.JTS;

import org.locationtech.jts.algorithm.Distance;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
Expand Down Expand Up @@ -101,6 +100,52 @@ public static void addEnvelopeComputer(final IEnvelopeComputer ec) {
public static GamaGeometryFactory GEOMETRY_FACTORY = new GamaGeometryFactory();
public static PreparedGeometryFactory PREPARED_GEOMETRY_FACTORY = new PreparedGeometryFactory();

public static Double distanceOnPolyline(final IShape line, GamaPoint pt1, GamaPoint pt2) {
int indexS = 0;
int indexT = 0;

IList<GamaPoint> points = (IList<GamaPoint>) line.getPoints();
int nbSp = points.size();
if (nbSp == 2) {
return pt1.euclidianDistanceTo(pt2);
} else {

double distanceS = Double.MAX_VALUE;
double distanceT = Double.MAX_VALUE;
for (int i = 0; i < nbSp - 1; i++) {
final double distS = Distance.pointToSegment(pt1, points.get(i), points.get(i + 1));
final double distT = Distance.pointToSegment(pt2, points.get(i), points.get(i + 1));
if (distS < distanceS) {
distanceS = distS;
indexS = i;

}
if (distT < distanceT) {
distanceT = distT;
indexT = i;
}

}
}
if (indexS == indexT) return pt1.euclidianDistanceTo(pt2);
double distance = 0;
int minI, maxI;
GamaPoint source, target;

if (indexT > indexS) {
minI = indexS +1; maxI = indexT; source = pt1; target = pt2;
} else {
minI = indexT + 1; maxI = indexS;source = pt2; target = pt1;
}
distance = source.euclidianDistanceTo(points.get(minI));
for (int i = minI; i < maxI - 1; i++) {
GamaPoint pt = points.get(i);
distance += source.euclidianDistanceTo(pt);
source = pt;
}
distance += source.euclidianDistanceTo(target);
return distance;
}
public static GamaPoint pointInGeom(final Geometry geom, final RandomUtils rand) {
// WARNING Only in 2D for Polygons !
if (geom == null || geom.getCoordinate() == null) { return null; }
Expand Down Expand Up @@ -867,7 +912,7 @@ private static IList<IShape> split_at(final Geometry g, final GamaPoint pt) {
int indexTarget = -1;
double distanceT = Double.MAX_VALUE;
for (int i = 0; i < coords.length - 1; i++) {
final double distT = distancePointLine(pt, coords[i], coords[i + 1]);
final double distT = Distance.pointToSegment(pt, coords[i], coords[i + 1]);
if (distT < distanceT) {
distanceT = distT;
indexTarget = i;
Expand Down

0 comments on commit f58e057

Please sign in to comment.