![](/img/trans.png)
[英]Spatial Join between pyspark dataframe and polygons (geopandas)
[英]Spatial join in geopandas when overlapping polygons?
我有兩個數據集,一個是點(商店),一個是多邊形(區)。
Districts 數據集有時具有重疊的多邊形(因為我已經緩沖了它們)。
我想知道每個多邊形是否有匹配點?
joined = geopandas.sjoin(districts,shops, op='contains', how='inner')
joined
上面的代碼可能只給了我一個匹配的多邊形。 如何檢查每個多邊形?
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 個區( d1 、 d2和d3 )。 兩個區重疊( d1和d2 )。 我們有4家商店。 s1在d1區內, s2在d2區內。 s12位於d1和d2區內。 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")
。
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 填充區列。
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 填充商店列。
gpd.sjoin(shops, districts, how="inner", op="contains")
幾何學 | index_right |
---|
這將返回一個空數據框,因為點不能包含多邊形。
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)) |
南 |
保持地區作為索引。
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) |
保持商店作為索引。
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.