如何使用地geopandas判断多边形的某个半径内是否在某个半径内?

发布于 2025-02-05 01:28:15 字数 1212 浏览 2 评论 0 原文

我有两个文件(.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...

I have two files(.shp): the first containing cities as polygons and the second containing parks as points. I have to find how many parks are there in a given distance from a city.
I was thinking using buffer to extend the area of the polygon by the distance and then iterate over the polygons and check which park(point) is in this area. Any ideas on how should I proceed, please?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱你是孤单的心事 2025-02-12 01:28:15

听起来您的数据很大,而Pandas是这里的瓶颈。尝试使用 dask-geopandas ,它使用dask DataFrames而不是PANDAS DataFrames。只需使用 pip安装dask-geopandas 安装软件包,然后将行更改

import geopandas as gpd

import dask_geopandas as gpd

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 with pip install dask-geopandas then change the line

import geopandas as gpd

to

import dask_geopandas as gpd
别理我 2025-02-12 01:28:15

您可能需要尝试使用您选择的 max_distance 参数 geopandas.sjoin_nearest()

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')

park_city_pairs = parks_shape.sjoin_nearest(cities_shape, max_distance=1) #whatever distance you choose

number_of_parks = len(park_city_pairs['park_id'].drop_duplicates())

我假设您的 geo_parks_f.shp 都有一个标识符列,例如 park_id

You might want to try geopandas.sjoin_nearest() with the max_distance argument that you choose.

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')

park_city_pairs = parks_shape.sjoin_nearest(cities_shape, max_distance=1) #whatever distance you choose

number_of_parks = len(park_city_pairs['park_id'].drop_duplicates())

I'm assuming your geo_parks_f.shp has an identifier column for every park like park_id

有深☉意 2025-02-12 01:28:15

您评论说您的方法太慢了。缓冲后,您是如何检查多边形内部的?如果您还没有这样做,则应使用空间树进行此部分,而不是在每个几何形状上循环循环,以使其更具性能。缓冲后,使用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 intersect spatial join or manually build a tree using shapely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文