问题
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.
回答1:
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).
回答2:
In my opinion, you have to convert WGS84 coords to TM coords using geotools both point and linestring..Here is some example code of my blog about CRS class of geotools
http://blog.naver.com/tommybee/50154157186
... then calculate the distance between point and point on linestring. you can finally get the result by converting the original coordination reference system as a WGS84 from TM(Transverse Meractor)
https://github.com/geotools/geotools/blob/master/docs/src/main/java/org/geotools/jts/SnapToLine.java
And more discussion about distance you can check the geotools user site below..it's one of them
http://osgeo-org.1560.x6.nabble.com/position-on-LineString-that-is-closest-to-Point-td4322083.html
来源:https://stackoverflow.com/questions/24809534/shortest-distance-between-a-line-segment-and-a-point-in-wgs84-crs-using-geotools