我希望使用Geopandas / Shapely实现与ArcPy Generate Near Table相当的功能。我对Geopandas和Shapely非常陌生,并且已经开发出了一种有效的方法,但我想知道是否有更有效的方法来做到这一点。
我有两个点文件数据集-人口普查块质心和餐馆。我正在寻找,对于每个人口普查区的质心,到它最近的餐厅的距离。对于同一家餐厅在多个街区是最近的餐厅,没有任何限制。
对我来说,这变得有点复杂的原因是因为Geopandas Distance function是按元素计算的,基于索引进行匹配。因此,我的一般方法是将Restaurants文件转换为多点文件,然后将块文件的索引设置为相同的值。那么所有的街区质心和餐馆都有相同的索引值。
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon, Point, MultiPoint现在阅读Block Centroid和Restaurant Shapefiles:
Blocks=gpd.read_file(BlockShp)
Restaurants=gpd.read_file(RestaurantShp)由于Geopandas distance函数按元素计算距离,因此我将餐厅GeoSeries转换为MultiPoint GeoSeries:
RestMulti=gpd.GeoSeries(Restaurants.unary_union)
RestMulti.crs=Restaurants.crs
RestMulti.reset_index(drop=True)然后,我将块的索引设置为等于0(与Restaurants多点的值相同),作为逐个元素计算的变通方法。
Blocks.index=[0]*len(Blocks)最后,我使用Geopandas距离函数来计算每个Block质心到最近餐厅的距离。
Blocks['Distance']=Blocks.distance(RestMulti)请就如何改进这一方面提供任何建议。我并不局限于使用Geopandas或Shapely,但我希望学习ArcPy的替代品。
谢谢你的帮助!
发布于 2018-12-06 01:36:02
你的代码缺少一个细节,args = (df_lines)
def min_distance(point, lines):
return lines.distance(point).min()
df_points['min_dist_to_lines'] = df_points.geometry.apply(min_distance, args=(df_lines,))# Notice the change to this linehttps://stackoverflow.com/questions/30740046
复制相似问题