I need to find distance in meters between some point with given latitude and longitude and line (on Earth) defined by several points with lat/lon coordinates.
I read these questions:
- Using Linestring distance in meters with GeoTools?
- Finding Perpendicular Distance and Minimum Perpendicular Vector between Point and Line using GeoTools and JTS?
In the second it is said that this is impossible with JTS and author decided to use some other utility.
If so, could you recommend any other java library for such problems?
In first, code in the accepted answer doesn't work for me, may be because of that I don't understand which transformation I should choose (and point can be on a big distance from the line, so the transformation may produce not accurate results).
My code for testing code from the first question:
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
Point spb = geometryFactory.createPoint(new Coordinate(30.3, 59.95));
Point msk = geometryFactory.createPoint(new Coordinate(37.6167, 55.75));
Point khabarovsk = geometryFactory.createPoint(new Coordinate(135.0667, 48.4833));
Point syktyvkar = geometryFactory.createPoint(new Coordinate(50.8167, 61.6667));
Coordinate[] coords = new Coordinate[] {
spb.getCoordinate(),
msk.getCoordinate(),
khabarovsk.getCoordinate()
};
LineString route = geometryFactory.createLineString(coords);
System.out.println(route.distance(syktyvkar)); //result: 6.881897419667442
try {
CoordinateReferenceSystem auto = CRS.decode("AUTO:42001,13.45,52.3");
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84,
auto);
Geometry g3 = JTS.transform(route, transform);
Geometry g4 = JTS.transform(syktyvkar, transform);
System.out.println(g3.distance(g4)); //result: 619842.1091396079
}
catch(Exception e) {}
}
The result must be quite close to 170 000 meters.