I have single LineString and some Point (outside the line), I snapped it to the Line (using DistanceOp
so as far as I understood the snapped point should lay on the line). How can I calculate length along the line from start vertex of the LineString up to the snapped point on the line?
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.operation.distance.DistanceOp;
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
Point outsidePoint = gf.createPoint(new Coordinate(outside.lat, outside.lon));
Coordinate snappedPoint = DistanceOp.nearestPoints(outsidePoint, LineString)[1];
I though about this: LineString
is a List
of coordinates, Find the index of the vertex, closest to the snapped point, and replace it with the snapped point. Then construct a LineString
from start vertex up to the index found. Then sum length calculated between each consecutive vertices of the line.