查找一个地理数据框的特征,该地理数据框至少包含另一个地理数据框的一个特征
Finding features of one geodataframe that contain at least one feature from another geodataframe
我有两个多边形图层。一个是地块图层(名称为“parcl”),另一个是多边形图层(名称为“houses”)。我正在尝试从房屋数据框中找到至少包含一个特征的所有地块。我试图通过一个循环来做到这一点,如下所示:
same = np.empty((0,parcl.shape[1]))
for i in parcl.index:
for j in houses.index:
a = parcl.loc[i]
b = houses.loc[j]
if (a.contains(b))==True:
same.append(a)
不幸的是,我收到以下错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-36-cedb059e5a3b> in <module>()
5 a = parcl.loc[i]
6 b = houses.loc[j]
----> 7 if (a.contains(b))==True:
8 same.append(a)
/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __getattr__(self, name)
5139 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5140 return self[name]
-> 5141 return object.__getattribute__(self, name)
5142
5143 def __setattr__(self, name: str, value) -> None:
AttributeError: 'Series' object has no attribute 'contains'
如有任何帮助,我们将不胜感激! :)
这是sjoin()
的一个简单案例
- 模拟了一些房屋(矩形)
- 使用一些英国县定义作为包裹
gdf_contains
是 sjoin()
的结果,现在是包含至少一所房子的地块
- 可视化以证明它有效(绿色包裹)
import geopandas as gpd
import shapely.geometry
import numpy as np
import requests
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).loc[lambda d: d["iso_a3"].eq("GBR")]
BOXES = 25
a, b, c, d = world.total_bounds
# manufactuer some houses
gdf_houses = gpd.GeoDataFrame(
geometry=[
shapely.geometry.box(minx, miny, maxx, maxy)
for minx, maxx in zip(np.linspace(a, c, BOXES), np.linspace(a, c, BOXES)[1:])
for miny, maxy in zip(np.linspace(b, d, BOXES), np.linspace(b, d, BOXES)[1:])
],
crs="epsg:4326",
).sample(35)
gdf_houses.explore()
res = requests.get(
"https://opendata.arcgis.com/datasets/37363d379f4f40fa8c3f0d28eedfdd37_0.geojson"
)
gdf_parcl = gpd.GeoDataFrame.from_features(res.json(), crs="epsg:4326")
gdf_contains = gdf_parcl.sjoin(gdf_houses)
m = gdf_parcl.explore(color="grey")
m = gdf_contains.explore(m=m, color="green")
m = gdf_houses.explore(m=m, style_kwds=dict(fillOpacity=.9))
m
我有两个多边形图层。一个是地块图层(名称为“parcl”),另一个是多边形图层(名称为“houses”)。我正在尝试从房屋数据框中找到至少包含一个特征的所有地块。我试图通过一个循环来做到这一点,如下所示:
same = np.empty((0,parcl.shape[1]))
for i in parcl.index:
for j in houses.index:
a = parcl.loc[i]
b = houses.loc[j]
if (a.contains(b))==True:
same.append(a)
不幸的是,我收到以下错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-36-cedb059e5a3b> in <module>()
5 a = parcl.loc[i]
6 b = houses.loc[j]
----> 7 if (a.contains(b))==True:
8 same.append(a)
/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __getattr__(self, name)
5139 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5140 return self[name]
-> 5141 return object.__getattribute__(self, name)
5142
5143 def __setattr__(self, name: str, value) -> None:
AttributeError: 'Series' object has no attribute 'contains'
如有任何帮助,我们将不胜感激! :)
这是sjoin()
- 模拟了一些房屋(矩形)
- 使用一些英国县定义作为包裹
gdf_contains
是sjoin()
的结果,现在是包含至少一所房子的地块- 可视化以证明它有效(绿色包裹)
import geopandas as gpd
import shapely.geometry
import numpy as np
import requests
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).loc[lambda d: d["iso_a3"].eq("GBR")]
BOXES = 25
a, b, c, d = world.total_bounds
# manufactuer some houses
gdf_houses = gpd.GeoDataFrame(
geometry=[
shapely.geometry.box(minx, miny, maxx, maxy)
for minx, maxx in zip(np.linspace(a, c, BOXES), np.linspace(a, c, BOXES)[1:])
for miny, maxy in zip(np.linspace(b, d, BOXES), np.linspace(b, d, BOXES)[1:])
],
crs="epsg:4326",
).sample(35)
gdf_houses.explore()
res = requests.get(
"https://opendata.arcgis.com/datasets/37363d379f4f40fa8c3f0d28eedfdd37_0.geojson"
)
gdf_parcl = gpd.GeoDataFrame.from_features(res.json(), crs="epsg:4326")
gdf_contains = gdf_parcl.sjoin(gdf_houses)
m = gdf_parcl.explore(color="grey")
m = gdf_contains.explore(m=m, color="green")
m = gdf_houses.explore(m=m, style_kwds=dict(fillOpacity=.9))
m