簡體   English   中英

重疊多邊形時在geopandas中的空間連接?

[英]Spatial join in geopandas when overlapping polygons?

我有兩個數據集,一個是點(商店),一個是多邊形(區)。

Districts 數據集有時具有重疊的多邊形(因為我已經緩沖了它們)。

我想知道每個多邊形是否有匹配點?

joined = geopandas.sjoin(districts,shops, op='contains', how='inner')
joined

上面的代碼可能只給了我一個匹配的多邊形。 如何檢查每個多邊形?

TL/DR

gpd.sjoin(districts, shops, how="left", op="contains") \
.reset_index()\
.rename(columns={"index": "districts"})\
.groupby(["districts"])\
.agg(nshops=("index_right", "nunique"), lshops=("index_right", "unique"))\
.astype(str)\
.replace("[nan]", "")

說明

問題設置

假設我們有 3 個區( d1d2d3 )。 兩個區重疊( d1d2 )。 我們有4家商店。 s1d1區內, s2d2區內。 s12位於d1d2區內。 s3不在任何區。

我們使用shapely在 python 中生成這個幾何:

from shapely.geometry import Point
from shapely.geometry import Polygon
import matplotlib.pyplot as plt
# Create Polygons for the districts
d1 = Polygon([(0, 0), (3, 0), (3, 3), (0, 3)])
d2 = Polygon([(1, 1), (4, 1), (4, 4), (1, 4)])
d3 = Polygon([(5, 2), (6, 2), (6, 3), (5, 3)])
# Create Points for the shops
s1 = Point(0.5, 0.5)
s2 = Point(3.5, 3.5)
s3 = Point(4.5, 2)
# This shop is in distric 1 and distric 2.
s12 = Point(2, 2)

將幾何圖形保存到 GeoPandas DataFrame 中並使用 matplotlib 我們可以查看配置:

import geopandas as gpd
import matplotlib.pyplot as plt
districts = gpd.GeoDataFrame(index=['d1', 'd2', 'd3'], geometry=[d1, d2, d3])
shops = gpd.GeoDataFrame(index=['s1', 's12', 's2', 's3'], geometry=[s1, s12, s2, s3])
ax = districts.boundary.plot()
shops.plot(ax=ax, color='red')
plt.show()

在此處輸入圖片說明

現在讓我們看看空間連接在 GeoPandas 中是如何工作的。 我們必須小心數據幀的順序,因為操作不是可交換的。 意思是gpd.sjoin(shops, districts, how="inner", op="contains")不等於gpd.sjoin(districts, shops, how="inner", op="contains")

現在讓我們來看看六種排列:

1 gpd.sjoin(shops, districts, how="left", op="contains")

幾何學 index_right
s1 積分 (0.5 0.5)
s12 點 (2 2)
s2 積分 (3.5 3.5)
s3 積分 (4.5 2)

將商店作為索引並用 NaN 填充區列。

2 gpd.sjoin(shops, districts, how="right", op="contains")

index_left 幾何學
d1 POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0))
d2 POLYGON ((1 1, 4 1, 4 4, 1 4, 1 1))
d3 POLYGON ((5 2, 6 2, 6 3, 5 3, 5 2))

將地區作為索引並用 NaN 填充商店列。

3 gpd.sjoin(shops, districts, how="inner", op="contains")

幾何學 index_right

這將返回一個空數據框,因為點不能包含多邊形。

4 gpd.sjoin(districts, shops, how="left", op="contains")

幾何學 index_right
d1 POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0)) s1
d1 POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0)) s12
d2 POLYGON ((1 1, 4 1, 4 4, 1 4, 1 1)) s12
d2 POLYGON ((1 1, 4 1, 4 4, 1 4, 1 1)) s2
d3 POLYGON ((5 2, 6 2, 6 3, 5 3, 5 2))

保持地區作為索引。

5 gpd.sjoin(districts, shops, how="right", op="contains")

index_left 幾何學
s1 d1 積分 (0.5 0.5)
s12 d1 點 (2 2)
s12 d2 點 (2 2)
s2 d2 積分 (3.5 3.5)
s3 積分 (4.5 2)

保持商店作為索引。

6 gpd.sjoin(districts, shops, how="inner", op="contains")

幾何學 index_right
d1 POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0)) s1
d1 POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0)) s12
d2 POLYGON ((1 1, 4 1, 4 4, 1 4, 1 1)) s12
d2 POLYGON ((1 1, 4 1, 4 4, 1 4, 1 1)) s2

接近我們得到的左邊,保留區作為索引,但刪除 NaN。

回答問題

    gpd.sjoin(districts, shops, how="left", op="contains") \
    .reset_index()\
    .rename(columns={"index": "districts"})\
    .groupby(["districts"])\
    .agg(nshops=("index_right", "nunique"), lshops=("index_right", "unique"))\
    .astype(str)\
    .replace("[nan]", "")
商店 商店
d1 2 ['s1''s12']
d2 2 ['s12''s2']
d3 0

這樣我們就可以知道每個多邊形是否有任何匹配點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM