【发布时间】:2019-10-24 12:33:06
【问题描述】:
我有一个显示约 25 个位置的地理数据框,表示为点几何。我正在尝试编写一个脚本来遍历每个点,识别最近的位置并返回最近位置的名称和距离。
如果我使用 shapely.ops 库中的最近点(geom1,geom2)有不同的地理数据框,我可以轻松地做到这一点。但是,我所有的位置都存储在一个地理数据框中。我正在尝试循环,这就是我遇到麻烦的地方
这是我的示例文件:
geofile = gpd.GeoDataFrame([[0, 'location A', Point(55, 55)],
[1, 'location B', Point(66, 66)],
[2, 'Location C', Point(99, 99)],
[3, 'Location D', Point(11, 11)]],
columns=['ID','Location','geometry'])
这是我创建的循环,但无济于事。
for index, row in geofile.iterrows():
nearest_geoms=nearest_points(row, geofile)
print('location:' + nearest_geoms[0])
print('nearest:' + nearest_geoms[1])
print('-------')
我收到此错误:
AttributeError: 'Series' object has no attribute '_geom'
但是我认为我的问题超出了错误原因,我必须排除我正在循环的行,因为它会自动返回为最近的位置,因为它就是那个位置。
我对一个位置的最终结果如下:
([0,'location A','location B', '5 miles', Point(55,55)], columns=['ID','Location','Nearest', 'Distance',geometry'])
【问题讨论】:
标签: python gis geopandas shapely