|
From: Michael B. <mic...@gm...> - 2010-03-04 14:08:45
|
Hi Stefan,
> Assume I have a Point p and a LineString ls.
>
> By the statement
>
> double dist = ls.distance(p)
>
> I easily can figure out the distance between them.
Not quite. I think that will give you the distance between the point
and the closest vertex of the LineString NOT the perpendicular
distance. In JTS 1.10 there is a class called EuclideanDistanceToPoint
that will give you the perp distance and the point that you are
calling p_cut...
import com.vividsolutions.jts.algorithm.distance.EuclideanDistanceToPoint;
import com.vividsolutions.jts.algorithm.distance.PointPairDistance;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.WKTReader;
import org.geotools.geometry.jts.JTSFactoryFinder;
public class Foo {
public static void main(String[] args) throws Exception {
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
WKTReader reader = new WKTReader(gf);
Geometry line = reader.read("LINESTRING(0 0, 10 0, 10 10, 20 10)");
Coordinate c = new Coordinate(5, 5);
PointPairDistance ppd = new PointPairDistance();
EuclideanDistanceToPoint.computeDistance(line, c, ppd);
System.out.println(ppd.getDistance());
for (Coordinate cc : ppd.getCoordinates()) {
System.out.println(cc);
}
}
}
Keep in mind there can be multiple points on your line that are equal
in distance from the reference point. If you run the example above you
will get p_cut = (5, 0) with distance = 5 but it could also have been
(10, 5).
Michael
|