Set-Operations with Overlay¶
When working with multiple spatial datasets – especially multiple polygon or
line datasets – users often wish to create new shapes based on places where
those datasets overlap (or don’t overlap). These manipulations are often
referred using the language of sets – intersections, unions, and differences.
These types of operations are made available in the geopandas library through
the overlay
function.
The basic idea is demonstrated by the graphic below but keep in mind that overlays operate at the DataFrame level, not on individual geometries, and the properties from both are retained. In effect, for every shape in the first GeoDataFrame, this operation is executed against every other shape in the other GeoDataFrame:

Source: QGIS Documentation
(Note to users familiar with the shapely library: overlay
can be thought
of as offering versions of the standard shapely set-operations that deal with
the complexities of applying set operations to two GeoSeries. The standard
shapely set-operations are also available as GeoSeries
methods.)
The different Overlay operations¶
First, we create some example data:
In [1]: from shapely.geometry import Polygon
In [2]: polys1 = geopandas.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
...: Polygon([(2,2), (4,2), (4,4), (2,4)])])
...:
In [3]: polys2 = geopandas.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
...: Polygon([(3,3), (5,3), (5,5), (3,5)])])
...:
In [4]: df1 = geopandas.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
In [5]: df2 = geopandas.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})
These two GeoDataFrames have some overlapping areas:
In [6]: ax = df1.plot(color='red');
In [7]: df2.plot(ax=ax, color='green', alpha=0.5);

We illustrate the different overlay modes with the above example.
The overlay
function will determine the set of all individual geometries
from overlaying the two input GeoDataFrames. This result covers the area covered
by the two input GeoDataFrames, and also preserves all unique regions defined by
the combined boundaries of the two GeoDataFrames.
When using how='union'
, all those possible geometries are returned:
In [8]: res_union = geopandas.overlay(df1, df2, how='union')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-08d89f109843> in <module>()
----> 1 res_union = geopandas.overlay(df1, df2, how='union')
/build/python-geopandas-0.4.0/geopandas/tools/overlay.py in overlay(df1, df2, how, make_valid, use_sindex)
369 result = _overlay_symmetric_diff(df1, df2)
370 elif how == 'union':
--> 371 result = _overlay_union(df1, df2)
372 elif how == 'identity':
373 dfunion = _overlay_union(df1, df2)
/build/python-geopandas-0.4.0/geopandas/tools/overlay.py in _overlay_union(df1, df2)
296 Overlay Union operation used in overlay function
297 """
--> 298 dfinter = _overlay_intersection(df1, df2)
299 dfsym = _overlay_symmetric_diff(df1, df2)
300 dfunion = pd.concat([dfinter, dfsym], ignore_index=True, **CONCAT_KWARGS)
/build/python-geopandas-0.4.0/geopandas/tools/overlay.py in _overlay_intersection(df1, df2)
210 spatial_index = df2.sindex
211 bbox = df1.geometry.apply(lambda x: x.bounds)
--> 212 sidx = bbox.apply(lambda x: list(spatial_index.intersection(x)))
213 # Create pairs of geometries in both dataframes to be intersected
214 nei = []
/usr/lib/python3/dist-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
/build/python-geopandas-0.4.0/geopandas/tools/overlay.py in <lambda>(x)
210 spatial_index = df2.sindex
211 bbox = df1.geometry.apply(lambda x: x.bounds)
--> 212 sidx = bbox.apply(lambda x: list(spatial_index.intersection(x)))
213 # Create pairs of geometries in both dataframes to be intersected
214 nei = []
AttributeError: 'NoneType' object has no attribute 'intersection'
In [9]: res_union