如何使用地geopandas判断多边形的某个半径内是否在某个半径内?
我有两个文件(.shp):第一个包含城市作为多边形,第二个包含公园作为点。我必须找到距离城市距离的距离有多少公园。 我在想使用缓冲区将多边形的区域扩展到距离,然后在多边形上迭代,然后检查该区域的哪个公园(点)。请问我应该如何继续吗?
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Polygon, Point
cities_shape = gpd.read_file('geo_cities_f.shp')
parks_shape = gpd.read_file('geo_parks_f.shp')
fig, ax = plt.subplots(figsize=(14,14))
cities_shape.buffer(0.002).plot(ax = ax, color='blue', edgecolor='black')
parks_shape.plot(ax = ax, color='red', edgecolor='black')
cities_shape['geometry'].buffer(0.0004).plot(figsize=(14,14))
**parks(points)**
SRID geometry
0 SRID=4326 POINT (34.79473 32.07580)
1 SRID=4326 POINT (34.80149 32.12502)
2 SRID=4326 POINT (34.76660 32.07581)
3 SRID=4326 POINT (34.78834 32.06583)
4 SRID=4326 POINT (34.78338 32.06643)
**polygons**
SRID geometry
0 SRID=4326 POLYGON ((34.80707 32.05355, 34.80704 32.05350...
1 SRID=4326 POLYGON ((34.80707 32.05355, 34.80704 32.05350...
2 SRID=4326 POLYGON ((34.80712 32.05342, 34.80713 32.05341...
3 SRID=4326 POLYGON ((34.80712 32.05342, 34.80713 32.05341...
4 SRID=4326 POLYGON ((34.80712 32.05337, 34.80715 32.05336...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您的数据很大,而Pandas是这里的瓶颈。尝试使用
dask-geopandas
,它使用dask DataFrames而不是PANDAS DataFrames。只需使用pip安装dask-geopandas
安装软件包,然后将行更改为
It sounds like your data is large, and pandas is the bottleneck here. Try using
dask-geopandas
, which uses dask dataframes instead of pandas dataframes. Just install the package withpip install dask-geopandas
then change the lineto
您可能需要尝试使用您选择的
max_distance
参数geopandas.sjoin_nearest()
。我假设您的
geo_parks_f.shp
都有一个标识符列,例如park_id
You might want to try
geopandas.sjoin_nearest()
with themax_distance
argument that you choose.I'm assuming your
geo_parks_f.shp
has an identifier column for every park likepark_id
您评论说您的方法太慢了。缓冲后,您是如何检查多边形内部的?如果您还没有这样做,则应使用空间树进行此部分,而不是在每个几何形状上循环循环,以使其更具性能。缓冲后,使用Geopanda的 Intersect 或使用 shapely shapely 。
You comment that your approach was too slow. After buffering, how did you check if there was a point inside a polygon? You should do this part using a spatial tree instead of looping over each geometry if you hadn't done so already, in order to make it more performant. After buffering, use geopanda's
intersectspatial join or manually build a tree using shapely.