如何使用 GeoPandas/Geo-Python/GIS 进行最近邻分析?
How to do Nearest Neighbour Analysis using GeoPandas/Geo-Python/GIS?
我正在学习本教程的最近邻分析:
https://automating-gis-processes.github.io/2017/lessons/L3/nearest-neighbour.html
我收到这个错误:
('id', 'occurred at index 0')
在我 运行 之后:
def nearest (row, geom_union, df1, df2, geom1_col='geometry', geom2_col='geometry', src_column=None):
# Find the nearest point and return the corresponding value from specified column.
# Find the geometry that is closest
nearest= df2[geom2_col] == nearest_points(row[geom1_col], geom_union)[1]
#Get the corresponding value from df2 (matching is based on the geometry)
value = df2[nearest][src_column].get_values()[0]
return value
df1['nearest_id'] = df1.apply(nearest, geom_union=unary_union, df1=df1, df2=df2, geom1_col='centroid', src_column='id', axis=1)
我为此使用了自己的数据。它类似于示例中给出的那个。但我在 shp 文件中有地址、几何图形、纬度和经度。所以我没有使用 .kml 文件。我无法弄清楚这个错误。
你是按照字面意思看代码的,
df1['nearest_id'] = df1.apply(nearest, geom_union=unary_union,
df1=df1, df2=df2, geom1_col='centroid',
src_column='id', axis=1)
那么问题很可能是 src_column
- 函数 returns 列 src_column
参数的值,由示例代码给定值 id
。如果您遇到列 id
的问题,很可能您没有具有此类名称的列,应该提供数据集中现有列的名称。
我正在学习本教程的最近邻分析: https://automating-gis-processes.github.io/2017/lessons/L3/nearest-neighbour.html
我收到这个错误:
('id', 'occurred at index 0')
在我 运行 之后:
def nearest (row, geom_union, df1, df2, geom1_col='geometry', geom2_col='geometry', src_column=None):
# Find the nearest point and return the corresponding value from specified column.
# Find the geometry that is closest
nearest= df2[geom2_col] == nearest_points(row[geom1_col], geom_union)[1]
#Get the corresponding value from df2 (matching is based on the geometry)
value = df2[nearest][src_column].get_values()[0]
return value
df1['nearest_id'] = df1.apply(nearest, geom_union=unary_union, df1=df1, df2=df2, geom1_col='centroid', src_column='id', axis=1)
我为此使用了自己的数据。它类似于示例中给出的那个。但我在 shp 文件中有地址、几何图形、纬度和经度。所以我没有使用 .kml 文件。我无法弄清楚这个错误。
你是按照字面意思看代码的,
df1['nearest_id'] = df1.apply(nearest, geom_union=unary_union, df1=df1, df2=df2, geom1_col='centroid', src_column='id', axis=1)
那么问题很可能是 src_column
- 函数 returns 列 src_column
参数的值,由示例代码给定值 id
。如果您遇到列 id
的问题,很可能您没有具有此类名称的列,应该提供数据集中现有列的名称。