geotools:Shortest distance between a line segment and a point in WGS84 crs using geotools api


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.

在geotools中,可以使用Geometry类中的distance函数查找两个几何图形之间的距离。有一个几何体的点子类,但没有几何体的线段子类。然而,有一个LineSegment类派生自LineString,它不是Geometry类的子类。我尝试使用JTS,但似乎JTS只适用于笛卡尔坐标系。

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.

https://stackoverflow.com/questions/24809534/shortest-distance-between-a-line-segment-and-a-point-in-wgs84-crs-using-geotools?r=SearchResults

 之前曾经求过已知p1,p2,p4,和p3-p4的距离,求p3的坐标。。。

如今已知p1,p2,p3,求p4。我们得知隐含逻辑,p4在p1-p2所在的直线上,p3p4垂直于p1p2。

由于已知p1,p2。可得p1-p2直线方程,y-y1=k(x-x1)。其中k=(y2-y1)/(x2-x1)。写成一般形式:Ax+By+C=0的话,就是kx-y+y1+kx1=0。

>>求点到直线的最短距离及垂足: https://wenku.baidu.com/view/5c511ffd6c1aff00bed5b9f3f90f76c661374c38.html

方法不止一种。。。我使用的是自己推导的:

条件一(垂直定理,斜率乘积为负一)方程:(y4-y3)/(x4-x3)*(y2-y1)/(x2-x1)=-1      

条件二(共线定理,点4在点12所在的直线)方程:(y2-y1)/(x2-x1)=(y4-y1)/(x4-x1)

由于k=(y2-y1)/(x2-x1)。所以上面两个方程可以简写成:

方程一:(y4-y3)/(x4-x3)*k=-1

方程二:k=(y4-y1)/(x4-x1)

同时把y4提到等式左边可得:

方程一:y4=y3-(x4-x3)/k

方程二:y4=y1+(x4-x1)*k

所以可以求得:

x4 = (y3+x3/k-y1+k*x1)/(k+1/k)

y4=y1+k*(x4-x1)

要不要先判断该点是否在线上。。。

>>Snap a Point to a Line:https://docs.geotools.org/latest/userguide/library/jts/snap.html

>>GeodeticCalculator:https://docs.geotools.org/stable/userguide/library/referencing/calculator.html

>>JTS Java空间几何计算、距离、最近点、subLine等 稳健的一比,持续更新中:https://blog.csdn.net/abu935009066/article/details/115304685

>>JTS计算空间坐标梳理:https://blog.csdn.net/ZHANGHUI3239619/article/details/120885305