多边形中到最边缘的地理点

问题描述 投票:0回答:1

所以我有大约10,000行的geopandas数据框,像这样。每个点都在多边形内(我已经确定了)。

point                         name      field_id    geometry
POINT(-0.1618445 51.5103873)  polygon1  1           POLYGON ((-0.1642799 51.5113756, -0.1639581 51.5089851, -0.1593661 51.5096729, -0.1606536 51.5115358, -0.1642799 51.5113756))

我想添加一个新列distance_to_nearest_edge。从point到多边形的最近边界的距离。

有一个计算我想要的形状的函数:

from shapely import wkt
poly = wkt.loads('POLYGON ((-0.1642799 51.5113756, -0.1639581 51.5089851, -0.1593661 51.5096729, -0.1606536 51.5115358, -0.1642799 51.5113756))')
pt = wkt.loads('POINT(-0.1618445 51.5103873)')
dist = poly.boundary.distance(pt)
---
dist = 0.0010736436340879488

但是我正在努力将其应用于1万行。

我尝试创建一个函数,但是我不断收到错误("'Polygon' object has no attribute 'encode'", 'occurred at index 0')

例如:

def fxy(x, y):
    poly = wkt.loads(x)
    pt = wkt.loads(y)
    return poly.exterior.distance(pt)

感谢任何帮助!

python pandas geopandas
1个回答
0
投票

我认为您的数据缺少值。你可以试试这个:

df['distance'] = df.apply(lambda row : row['point'].distance(row['geometry'].boundary) if pd.notnull(row['point']) & pd.notnull(row['geometry']) else np.nan, axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.