备注
此页面是从 `gallery/overlays.ipynb`_ _.
叠加层#
空间叠加允许您比较包含面或多面几何的两个GeoDataFrame,并使用表示空间组合的新几何创建新的GeoDataFrame and 合并的特性。这使您可以回答以下问题
高速公路周围1000英尺范围内的人口普查区域的人口统计数据是什么?
下图演示了基本思想,但请记住,叠加层在数据框级别运行,而不是在单个几何上运行,并且两者的属性都会保留
现在,我们可以加载两个包含(多)面几何…的GeoDataFrame
[1]:
%matplotlib inline
from shapely.geometry import Point
from geopandas import datasets, GeoDataFrame, read_file
from geopandas.tools import overlay
# NYC Boros
zippath = datasets.get_path('nybb')
polydf = read_file(zippath)
# Generate some circles
b = [int(x) for x in polydf.total_bounds]
N = 10
polydf2 = GeoDataFrame([
{'geometry': Point(x, y).buffer(10000), 'value1': x + y, 'value2': x - y}
for x, y in zip(range(b[0], b[2], int((b[2] - b[0]) / N)),
range(b[1], b[3], int((b[3] - b[1]) / N)))])
第一个数据帧包含纽约市Boros的多面体
[2]:
polydf.plot()
[2]:
<AxesSubplot:>

第二个GeoDataFrame是在相同地理空间中顺序生成的一组圆。我们将用一个 different color palette 。
[3]:
polydf2.plot(cmap='tab20b')
[3]:
<AxesSubplot:>

这个 geopandas.tools.overlay
函数有三个参数:
DF1
DF2
多么
哪里 how
可以是以下之一:
['intersection',
'union',
'identity',
'symmetric_difference',
'difference']
因此,让我们使用 overlay
方法。
[4]:
newdf = polydf.overlay(polydf2, how="intersection")
newdf.plot(cmap='tab20b')
/usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.
Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.
Left CRS: EPSG:2263
Right CRS: None
return geopandas.overlay(
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 newdf = polydf.overlay(polydf2, how="intersection")
2 newdf.plot(cmap='tab20b')
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245, in GeoDataFrame.overlay(self, right, how, keep_geom_type, make_valid)
2157 def overlay(self, right, how="intersection", keep_geom_type=None, make_valid=True):
2158 """Perform spatial overlay between GeoDataFrames.
2159
2160 Currently only supports data GeoDataFrames with uniform geometry types,
(...)
2243 dimension is not taken into account.
2244 """
-> 2245 return geopandas.overlay(
2246 self, right, how=how, keep_geom_type=keep_geom_type, make_valid=make_valid
2247 )
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:317, in overlay(df1, df2, how, keep_geom_type, make_valid)
315 result = _overlay_difference(df1, df2)
316 elif how == "intersection":
--> 317 result = _overlay_intersection(df1, df2)
318 elif how == "symmetric_difference":
319 result = _overlay_symmetric_diff(df1, df2)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:30, in _overlay_intersection(df1, df2)
26 """
27 Overlay Intersection operation used in overlay function
28 """
29 # Spatial Index to create intersections
---> 30 idx1, idx2 = df2.sindex.query_bulk(df1.geometry, predicate="intersects", sort=True)
31 # Create pairs of geometries in both dataframes to be intersected
32 if idx1.size > 0 and idx2.size > 0:
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/base.py:2706, in GeoPandasBase.sindex(self)
2655 @property
2656 def sindex(self):
2657 """Generate the spatial index
2658
2659 Creates R-tree spatial index based on ``pygeos.STRtree`` or
(...)
2704 [2]])
2705 """
-> 2706 return self.geometry.values.sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/array.py:291, in GeometryArray.sindex(self)
288 @property
289 def sindex(self):
290 if self._sindex is None:
--> 291 self._sindex = _get_sindex_class()(self.data)
292 return self._sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/sindex.py:21, in _get_sindex_class()
19 if compat.HAS_RTREE:
20 return RTreeIndex
---> 21 raise ImportError(
22 "Spatial indexes require either `rtree` or `pygeos`. "
23 "See installation instructions at https://geopandas.org/install.html"
24 )
ImportError: Spatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.html
然后看一下属性;我们看到来自两个原始GeoDataFrame的属性都被保留了。
[5]:
polydf.head()
[5]:
BoroCode | BoroName | Shape_Leng | Shape_Area | geometry | |
---|---|---|---|---|---|
0 | 5 | Staten Island | 330470.010332 | 1.623820e+09 | MULTIPOLYGON (((970217.022 145643.332, 970227.... |
1 | 4 | Queens | 896344.047763 | 3.045213e+09 | MULTIPOLYGON (((1029606.077 156073.814, 102957... |
2 | 3 | Brooklyn | 741080.523166 | 1.937479e+09 | MULTIPOLYGON (((1021176.479 151374.797, 102100... |
3 | 1 | Manhattan | 359299.096471 | 6.364715e+08 | MULTIPOLYGON (((981219.056 188655.316, 980940.... |
4 | 2 | Bronx | 464392.991824 | 1.186925e+09 | MULTIPOLYGON (((1012821.806 229228.265, 101278... |
[6]:
polydf2.head()
[6]:
geometry | value1 | value2 | |
---|---|---|---|
0 | POLYGON ((923175.000 120121.000, 923126.847 11... | 1033296 | 793054 |
1 | POLYGON ((938595.000 135393.000, 938546.847 13... | 1063988 | 793202 |
2 | POLYGON ((954015.000 150665.000, 953966.847 14... | 1094680 | 793350 |
3 | POLYGON ((969435.000 165937.000, 969386.847 16... | 1125372 | 793498 |
4 | POLYGON ((984855.000 181209.000, 984806.847 18... | 1156064 | 793646 |
[7]:
newdf.head()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 newdf.head()
NameError: name 'newdf' is not defined
现在让我们来看看另一个 how
运营:
[8]:
newdf = polydf.overlay(polydf2, how="union")
newdf.plot(cmap='tab20b')
/usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.
Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.
Left CRS: EPSG:2263
Right CRS: None
return geopandas.overlay(
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 newdf = polydf.overlay(polydf2, how="union")
2 newdf.plot(cmap='tab20b')
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245, in GeoDataFrame.overlay(self, right, how, keep_geom_type, make_valid)
2157 def overlay(self, right, how="intersection", keep_geom_type=None, make_valid=True):
2158 """Perform spatial overlay between GeoDataFrames.
2159
2160 Currently only supports data GeoDataFrames with uniform geometry types,
(...)
2243 dimension is not taken into account.
2244 """
-> 2245 return geopandas.overlay(
2246 self, right, how=how, keep_geom_type=keep_geom_type, make_valid=make_valid
2247 )
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:321, in overlay(df1, df2, how, keep_geom_type, make_valid)
319 result = _overlay_symmetric_diff(df1, df2)
320 elif how == "union":
--> 321 result = _overlay_union(df1, df2)
322 elif how == "identity":
323 dfunion = _overlay_union(df1, df2)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:136, in _overlay_union(df1, df2)
132 def _overlay_union(df1, df2):
133 """
134 Overlay Union operation used in overlay function
135 """
--> 136 dfinter = _overlay_intersection(df1, df2)
137 dfsym = _overlay_symmetric_diff(df1, df2)
138 dfunion = pd.concat([dfinter, dfsym], ignore_index=True, sort=False)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:30, in _overlay_intersection(df1, df2)
26 """
27 Overlay Intersection operation used in overlay function
28 """
29 # Spatial Index to create intersections
---> 30 idx1, idx2 = df2.sindex.query_bulk(df1.geometry, predicate="intersects", sort=True)
31 # Create pairs of geometries in both dataframes to be intersected
32 if idx1.size > 0 and idx2.size > 0:
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/base.py:2706, in GeoPandasBase.sindex(self)
2655 @property
2656 def sindex(self):
2657 """Generate the spatial index
2658
2659 Creates R-tree spatial index based on ``pygeos.STRtree`` or
(...)
2704 [2]])
2705 """
-> 2706 return self.geometry.values.sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/array.py:291, in GeometryArray.sindex(self)
288 @property
289 def sindex(self):
290 if self._sindex is None:
--> 291 self._sindex = _get_sindex_class()(self.data)
292 return self._sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/sindex.py:21, in _get_sindex_class()
19 if compat.HAS_RTREE:
20 return RTreeIndex
---> 21 raise ImportError(
22 "Spatial indexes require either `rtree` or `pygeos`. "
23 "See installation instructions at https://geopandas.org/install.html"
24 )
ImportError: Spatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.html
[9]:
newdf = polydf.overlay(polydf2, how="identity")
newdf.plot(cmap='tab20b')
/usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.
Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.
Left CRS: EPSG:2263
Right CRS: None
return geopandas.overlay(
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 newdf = polydf.overlay(polydf2, how="identity")
2 newdf.plot(cmap='tab20b')
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245, in GeoDataFrame.overlay(self, right, how, keep_geom_type, make_valid)
2157 def overlay(self, right, how="intersection", keep_geom_type=None, make_valid=True):
2158 """Perform spatial overlay between GeoDataFrames.
2159
2160 Currently only supports data GeoDataFrames with uniform geometry types,
(...)
2243 dimension is not taken into account.
2244 """
-> 2245 return geopandas.overlay(
2246 self, right, how=how, keep_geom_type=keep_geom_type, make_valid=make_valid
2247 )
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:323, in overlay(df1, df2, how, keep_geom_type, make_valid)
321 result = _overlay_union(df1, df2)
322 elif how == "identity":
--> 323 dfunion = _overlay_union(df1, df2)
324 result = dfunion[dfunion["__idx1"].notnull()].copy()
326 if how in ["intersection", "symmetric_difference", "union", "identity"]:
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:136, in _overlay_union(df1, df2)
132 def _overlay_union(df1, df2):
133 """
134 Overlay Union operation used in overlay function
135 """
--> 136 dfinter = _overlay_intersection(df1, df2)
137 dfsym = _overlay_symmetric_diff(df1, df2)
138 dfunion = pd.concat([dfinter, dfsym], ignore_index=True, sort=False)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:30, in _overlay_intersection(df1, df2)
26 """
27 Overlay Intersection operation used in overlay function
28 """
29 # Spatial Index to create intersections
---> 30 idx1, idx2 = df2.sindex.query_bulk(df1.geometry, predicate="intersects", sort=True)
31 # Create pairs of geometries in both dataframes to be intersected
32 if idx1.size > 0 and idx2.size > 0:
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/base.py:2706, in GeoPandasBase.sindex(self)
2655 @property
2656 def sindex(self):
2657 """Generate the spatial index
2658
2659 Creates R-tree spatial index based on ``pygeos.STRtree`` or
(...)
2704 [2]])
2705 """
-> 2706 return self.geometry.values.sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/array.py:291, in GeometryArray.sindex(self)
288 @property
289 def sindex(self):
290 if self._sindex is None:
--> 291 self._sindex = _get_sindex_class()(self.data)
292 return self._sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/sindex.py:21, in _get_sindex_class()
19 if compat.HAS_RTREE:
20 return RTreeIndex
---> 21 raise ImportError(
22 "Spatial indexes require either `rtree` or `pygeos`. "
23 "See installation instructions at https://geopandas.org/install.html"
24 )
ImportError: Spatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.html
[10]:
newdf = polydf.overlay(polydf2, how="symmetric_difference")
newdf.plot(cmap='tab20b')
/usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.
Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.
Left CRS: EPSG:2263
Right CRS: None
return geopandas.overlay(
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 newdf = polydf.overlay(polydf2, how="symmetric_difference")
2 newdf.plot(cmap='tab20b')
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245, in GeoDataFrame.overlay(self, right, how, keep_geom_type, make_valid)
2157 def overlay(self, right, how="intersection", keep_geom_type=None, make_valid=True):
2158 """Perform spatial overlay between GeoDataFrames.
2159
2160 Currently only supports data GeoDataFrames with uniform geometry types,
(...)
2243 dimension is not taken into account.
2244 """
-> 2245 return geopandas.overlay(
2246 self, right, how=how, keep_geom_type=keep_geom_type, make_valid=make_valid
2247 )
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:319, in overlay(df1, df2, how, keep_geom_type, make_valid)
317 result = _overlay_intersection(df1, df2)
318 elif how == "symmetric_difference":
--> 319 result = _overlay_symmetric_diff(df1, df2)
320 elif how == "union":
321 result = _overlay_union(df1, df2)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:107, in _overlay_symmetric_diff(df1, df2)
103 def _overlay_symmetric_diff(df1, df2):
104 """
105 Overlay Symmetric Difference operation used in overlay function
106 """
--> 107 dfdiff1 = _overlay_difference(df1, df2)
108 dfdiff2 = _overlay_difference(df2, df1)
109 dfdiff1["__idx1"] = range(len(dfdiff1))
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:80, in _overlay_difference(df1, df2)
76 """
77 Overlay Difference operation used in overlay function
78 """
79 # spatial index query to find intersections
---> 80 idx1, idx2 = df2.sindex.query_bulk(df1.geometry, predicate="intersects", sort=True)
81 idx1_unique, idx1_unique_indices = np.unique(idx1, return_index=True)
82 idx2_split = np.split(idx2, idx1_unique_indices[1:])
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/base.py:2706, in GeoPandasBase.sindex(self)
2655 @property
2656 def sindex(self):
2657 """Generate the spatial index
2658
2659 Creates R-tree spatial index based on ``pygeos.STRtree`` or
(...)
2704 [2]])
2705 """
-> 2706 return self.geometry.values.sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/array.py:291, in GeometryArray.sindex(self)
288 @property
289 def sindex(self):
290 if self._sindex is None:
--> 291 self._sindex = _get_sindex_class()(self.data)
292 return self._sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/sindex.py:21, in _get_sindex_class()
19 if compat.HAS_RTREE:
20 return RTreeIndex
---> 21 raise ImportError(
22 "Spatial indexes require either `rtree` or `pygeos`. "
23 "See installation instructions at https://geopandas.org/install.html"
24 )
ImportError: Spatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.html
[11]:
newdf = polydf.overlay(polydf2, how="difference")
newdf.plot(cmap='tab20b')
/usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.
Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.
Left CRS: EPSG:2263
Right CRS: None
return geopandas.overlay(
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [11], in <cell line: 1>()
----> 1 newdf = polydf.overlay(polydf2, how="difference")
2 newdf.plot(cmap='tab20b')
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:2245, in GeoDataFrame.overlay(self, right, how, keep_geom_type, make_valid)
2157 def overlay(self, right, how="intersection", keep_geom_type=None, make_valid=True):
2158 """Perform spatial overlay between GeoDataFrames.
2159
2160 Currently only supports data GeoDataFrames with uniform geometry types,
(...)
2243 dimension is not taken into account.
2244 """
-> 2245 return geopandas.overlay(
2246 self, right, how=how, keep_geom_type=keep_geom_type, make_valid=make_valid
2247 )
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:315, in overlay(df1, df2, how, keep_geom_type, make_valid)
313 warnings.filterwarnings("ignore", message="CRS mismatch between the CRS")
314 if how == "difference":
--> 315 result = _overlay_difference(df1, df2)
316 elif how == "intersection":
317 result = _overlay_intersection(df1, df2)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/tools/overlay.py:80, in _overlay_difference(df1, df2)
76 """
77 Overlay Difference operation used in overlay function
78 """
79 # spatial index query to find intersections
---> 80 idx1, idx2 = df2.sindex.query_bulk(df1.geometry, predicate="intersects", sort=True)
81 idx1_unique, idx1_unique_indices = np.unique(idx1, return_index=True)
82 idx2_split = np.split(idx2, idx1_unique_indices[1:])
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/base.py:2706, in GeoPandasBase.sindex(self)
2655 @property
2656 def sindex(self):
2657 """Generate the spatial index
2658
2659 Creates R-tree spatial index based on ``pygeos.STRtree`` or
(...)
2704 [2]])
2705 """
-> 2706 return self.geometry.values.sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/array.py:291, in GeometryArray.sindex(self)
288 @property
289 def sindex(self):
290 if self._sindex is None:
--> 291 self._sindex = _get_sindex_class()(self.data)
292 return self._sindex
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/sindex.py:21, in _get_sindex_class()
19 if compat.HAS_RTREE:
20 return RTreeIndex
---> 21 raise ImportError(
22 "Spatial indexes require either `rtree` or `pygeos`. "
23 "See installation instructions at https://geopandas.org/install.html"
24 )
ImportError: Spatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.html