如何求点到多边形最近边的距离

2024-06-17 15:05:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想计算从一个点到多边形最近边的距离。下面的代码显示了我是如何计算距离的。我从poly.boundary.distance(pt)得到的距离与我从postgis得到的距离不同。 postgis的结果更加合理和正确。在下面的结果部分,我显示了我从postgis和geopandas获得的距离。 请让我知道如何找到从一个点到多边形最近边的距离,以及应该使用哪个坐标系

代码

fieldCoordinatesAsTextInWKTInEPSG25832:POLYGON ((5622486.93624152 1003060.89945681,5622079.52632924 1003170.95198635,5622126.00418918 1003781.73122161,5622444.73987453 1003694.55868486,5622486.93624152 1003060.89945681))
centerPointointAsTextInWKTInEPSG25832:POINT(341849.5 5661622.5) #sample point just for the sake of this question for clarification
poly = wkt.loads(fieldCoordinatesAsTextInWKTInEPSG25832)
pt = wkt.loads(centerPointointAsTextInWKTInEPSG25832)
print(poly.boundary.distance(pt)))

结果

fieldCoordinatesAsTextInWKTInEPSG25832:POLYGON ((5622486.93624152 1003060.89945681,5622079.52632924 1003170.95198635,5622126.00418918 1003781.73122161,5622444.73987453 1003694.55868486,5622486.93624152 1003060.89945681))
centerPointointAsTextInWKTInEPSG25832:POINT(341849.5 5661622.5)
distance:4.32797817574802
poly.boundary.distance(pt):7041079.504450037
centerPointointAsTextInWKTInEPSG25832:POINT(341859.5 5661622.5)
distance:4.00406202746054
poly.boundary.distance(pt):7041072.005210307
centerPointointAsTextInWKTInEPSG25832:POINT(341869.5 5661622.5)
distance:3.68014587917116
poly.boundary.distance(pt):7041064.5059767915
centerPointointAsTextInWKTInEPSG25832:POINT(341879.5 5661622.5)
distance:3.35622973088368
poly.boundary.distance(pt):7041057.006749493
centerPointointAsTextInWKTInEPSG25832:POINT(341889.5 5661622.5)
distance:3.03231358166347
poly.boundary.distance(pt):7041049.507528408
centerPointointAsTextInWKTInEPSG25832:POINT(341899.5 5661622.5)
distance:2.70839743337599
poly.boundary.distance(pt):7041042.0083135385
centerPointointAsTextInWKTInEPSG25832:POINT(341909.5 5661622.5)
distance:2.38448128508661
poly.boundary.distance(pt):7041034.509104885
centerPointointAsTextInWKTInEPSG25832:POINT(341919.5 5661622.5)
distance:2.06056513679913
poly.boundary.distance(pt):7041027.0099024465
centerPointointAsTextInWKTInEPSG25832:POINT(341929.5 5661622.5)
distance:1.73664898850976
poly.boundary.distance(pt):7041019.510706224
centerPointointAsTextInWKTInEPSG25832:POINT(341939.5 5661622.5)
distance:1.41273284022227
poly.boundary.distance(pt):7041012.011516215
centerPointointAsTextInWKTInEPSG25832:POINT(341949.5 5661622.5)
distance:1.0888166919329
poly.boundary.distance(pt):7041004.512332423
centerPointointAsTextInWKTInEPSG25832:POINT(341959.5 5661622.5)
distance:0.764900543645413
poly.boundary.distance(pt):7040997.013154846

Tags: 代码pt距离for多边形wktdistancepoint