点到多边形最近边的距离
Posted
技术标签:
【中文标题】点到多边形最近边的距离【英文标题】:distance from a point to the nearest edge of a polygon 【发布时间】:2021-12-06 01:25:58 【问题描述】:在下面的代码中,我想计算一个点到多边形最近边缘的距离。如下面的结果部分所示,提供了坐标。下面发布的代码显示了我如何找到一个点的距离到多边形的最整齐的边缘。 在运行时,如下面的结果部分所示,对于给定点和几何图形,与 postgis 的距离等于 4.32797817574802,而从 geopandas 计算得到的距离为 3.8954865274727614e-05
请告诉我如何求一个。
代码
poly = wkt.loads(fieldCoordinatesAsTextInWKTInEPSG25832)
pt = wkt.loads(centerPointointAsTextInWKTInEPSG25832)
print(poly.distance(pt)))
结果:
queryPostgreSQLForDistancesFromPointsToPolygon:4.32797817574802#result from postgis using st_distance operator
centerPointointAsTextInWKTInEPSG4326:POINT(6.7419520458647835 51.08427961641239)
centerPointointAsTextInWKTInEPSG25832:POINT(341849.5 5661622.5)
centerPointointAsTextInWKTInEPSG4326:POINT(6.7419520458647835 51.08427961641239)
fieldCoordinatesAsTextInWKTInEPSG25832:POLYGON ((5622486.93624152 1003060.89945681,5622079.52632924 1003170.95198635,5622126.00418918 1003781.73122161,5622444.73987453 1003694.55868486,5622486.93624152 1003060.89945681))
fieldCoordinatesAsTextInWKTInEPSG4326:POLYGON((6.741879696309871 51.08423775429969,6.742907378503366 51.08158745820981,6.746964018740842 51.08233499299334,6.746152690693346 51.08440763989611,6.741879696309871 51.08423775429969))
poly.distance(pt):3.8954865274727614e-05#result from geopandas
【问题讨论】:
请编辑您的问题中的代码,简化为minimal reproducible example - 任何人都应该能够复制/粘贴到文件中并不添加任何内容运行它看到与您相同的问题,因此请确保代码包含所有需要的导入和一些简单多边形的数据初始化以及显示问题的点坐标。 【参考方案1】: 您的代码有效。从比利时到埃塞俄比亚大约 7000 公里 您确定您的数据正确吗?构建了一个 plotly 图来显示缓冲多边形、多边形质心和点在 EPSG:4326 CRS 中的位置from shapely import wkt
import geopandas as gpd
import plotly.express as px
import json
# queryPostgreSQLForDistancesFromPointsToPolygon:4.32797817574802#result from postgis using st_distance operator
centerPointointAsTextInWKTInEPSG4326 = "POINT(6.7419520458647835 51.08427961641239)"
centerPointointAsTextInWKTInEPSG25832 = "POINT(341849.5 5661622.5)"
centerPointointAsTextInWKTInEPSG4326 = "POINT(6.7419520458647835 51.08427961641239)"
fieldCoordinatesAsTextInWKTInEPSG25832 = "POLYGON ((5622486.93624152 1003060.89945681,5622079.52632924 1003170.95198635,5622126.00418918 1003781.73122161,5622444.73987453 1003694.55868486,5622486.93624152 1003060.89945681))"
fieldCoordinatesAsTextInWKTInEPSG4326 = "POLYGON((6.741879696309871 51.08423775429969,6.742907378503366 51.08158745820981,6.746964018740842 51.08233499299334,6.746152690693346 51.08440763989611,6.741879696309871 51.08423775429969))"
# poly.distance(pt):3.8954865274727614e-05#result from geopandas
poly = wkt.loads(fieldCoordinatesAsTextInWKTInEPSG25832)
pt = wkt.loads(centerPointointAsTextInWKTInEPSG25832)
print(poly.distance(pt)/10**3)
# let's visualize it....
gpoly = (
gpd.GeoDataFrame(geometry=[poly], crs="EPSG:25832")
.buffer(10 ** 6)
.to_crs("EPSG:4326")
)
gpoly.plot()
gpt = gpd.GeoDataFrame(geometry=[pt, poly.centroid], crs="EPSG:25832").to_crs(
"EPSG:4326"
)
px.scatter_mapbox(
gpt.assign(dist=poly.distance(pt)/10**3),
lat=gpt.geometry.y,
lon=gpt.geometry.x,
hover_data="dist":":.0f",
).update_layout(
mapbox=
"style": "carto-positron",
"zoom": 4,
"layers": [
"source": json.loads(gpoly.to_json()),
"below": "traces",
"type": "fill",
"color": "red",
],
)
【讨论】:
以上是关于点到多边形最近边的距离的主要内容,如果未能解决你的问题,请参考以下文章