4

In geotools you can find distance between two geometries using the distance function in Geometry class. There is a Point subclass of Geometry but no line segment subclass of Geometry. There is however LineSegment class which derives from LineString which is not subclass of Geometry class. I tried using JTS but it appears as JTS is only for Cartesian coordinate system.

Question: How to find shortest distance (in meters) between a LineSegment and a Point in WGS84 crs.

Please note: I know there are answers available on SO for doing this without using geotools. Since we are using geotools in our project and code maintainability is very important I want to do it in geotools.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
Haider
  • 938
  • 2
  • 11
  • 25

1 Answers1

0

The easy solution (assuming that your point is close to the line is to use JTS to find the closest point (ignore curvature of the earth for now) and then use GeodeticCalculator to find the distance between the two points. For short distances this should be fine.

If you need more accuracy you need to reproject you line(s) and point to a planar projection and do all the calculations in that projection using JTS directly.

PS LineString extends Geometry (check which sort of Geometry you have).

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Ian Turton
  • 10,018
  • 1
  • 28
  • 47