|
From: Schuster S. <ste...@ge...> - 2010-03-04 14:48:34
|
Hi Michael,
first at all, thanks for your answer!
A short test of the distance function let me assume that is DOES calculate
the perpendicular distance:
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
WKTReader reader = new WKTReader(gf);
Geometry line = reader.read("LINESTRING(0 0, 10 10)");
Geometry point = reader.read("POINT(0 10)");
double distance = line.distance(point);
System.out.println(distance);
OUTPUT: 7.0710678118654755 (=sqrt(50))
Anyway, your solution looks more beautiful than the way I tried it before.
But when copy/paste your Code to my project, I can't find the class
PointPairDistance. Which jars do I need?
Thanks also for the advice with multiple closest points, I'll be aware of
that in my further calculations!
Stefan
-----Ursprüngliche Nachricht-----
Von: Michael Bedward [mailto:mic...@gm...]
Gesendet: Donnerstag, 4. März 2010 15:09
An: Schuster Stefan; geo...@li...
Betreff: Re: [Geotools-gt2-users] position on LineString that is closest to
Point
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
|