Overlay

Overlay function that avoids a GEOSException from geopandas.overlay.

This module includes the function ‘clean_overlay’, which bypasses a GEOSException from the regular geopandas.overlay. The function is a generalized version of the solution from GH 2792.

‘clean_overlay’ also includes the overlay type “update”, which can be specified in the “how” parameter, in addition to the five native geopandas how-s.

clean_overlay(df1, df2, how='intersection', keep_geom_type=None, geom_type=None, predicate='intersects', grid_size=None, lsuffix='_1', rsuffix='_2', n_jobs=1, rtree_runner=None, union_runner=None, overlay_runner=None)[source]

Fixes and explodes geometries before doing a shapely overlay, then cleans up.

Fixes geometries, then does a shapely overlay operation that dodges a GEOSException raised in the regular geopandas.overlay. The function is a generalised version of a solution from GH 2792.

Parameters:
  • df1 (GeoDataFrame) – GeoDataFrame

  • df2 (GeoDataFrame) – GeoDataFrame

  • how (str) – Method of spatial overlay. Includes the ‘update’ method, plus the five native geopandas methods ‘intersection’, ‘union’, ‘identity’, ‘symmetric_difference’ and ‘difference’.

  • keep_geom_type (bool | None) – If True (default), return only geometries of the same geometry type as df1 has, if False, return all resulting geometries.

  • geom_type (str | None) – optionally specify what geometry type to keep before the overlay, if there are mixed geometry types. Must be either “polygon”, “line” or “point”.

  • grid_size (float | None) – Precision grid size to round the geometries. Will use the highest precision of the inputs by default.

  • predicate (str | None) – Spatial predicate in the spatial tree.

  • lsuffix (str) – Suffix of columns in df1 that are also in df2.

  • rsuffix (str) – Suffix of columns in df2 that are also in df1.

  • n_jobs (int) – number of jobs. Defaults to 1.

  • union_runner (UnionRunner | None) – Optionally debug/manipulate the spatial union operations. See the ‘runners’ module for example implementations.

  • rtree_runner (RTreeQueryRunner | None) – Optionally debug/manipulate the spatial indexing operations. See the ‘runners’ module for example implementations.

  • overlay_runner (OverlayRunner | None) – Optionally debug/manipulate the spatial overlay operations. See the ‘runners’ module for example implementations.

Return type:

GeoDataFrame

Returns:

GeoDataFrame with overlayed and fixed geometries and columns from both GeoDataFrames.

Raises:

ValueError – If ‘how’ is not one of ‘intersection’, ‘union’, ‘identity’, ‘symmetric_difference’, ‘difference’ or ‘update’.