[英]how to tell if a point is within a certain radius from a polygon using 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...
您評論說您的方法太慢了。 緩沖后,如何檢查多邊形內是否有一個點? 如果您還沒有這樣做,您應該使用空間樹而不是循環遍歷每個幾何圖形來完成這部分,以提高性能。 緩沖后,使用 geopanda 的
intersect
spatial join或使用shapely手動構建樹。
聽起來您的數據很大,而 pandas 是這里的瓶頸。 嘗試使用dask-geopandas
,它使用 dask 數據幀而不是 pandas 數據幀。 只需使用pip install dask-geopandas
安裝軟件包,然后更改行
import geopandas as gpd
至
import dask_geopandas as gpd
您可能想嘗試geopandas.sjoin_nearest()
與您選擇的max_distance
參數。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.