geopandas.GeoSeries.make_valid#
- GeoSeries.make_valid(*, method='linework', keep_collapsed=True)[source]#
Repairs invalid geometries.
Returns a
GeoSerieswith valid geometries.If the input geometry is already valid, then it will be preserved. In many cases, in order to create a valid geometry, the input geometry must be split into multiple parts or multiple geometries. If the geometry must be split into multiple parts of the same type to be made valid, then a multi-part geometry will be returned (e.g. a MultiPolygon). If the geometry must be split into multiple parts of different types to be made valid, then a GeometryCollection will be returned.
Two
methodsare available:the ‘linework’ algorithm tries to preserve every edge and vertex in the input. It combines all rings into a set of noded lines and then extracts valid polygons from that linework. An alternating even-odd strategy is used to assign areas as interior or exterior. A disadvantage is that for some relatively simple invalid geometries this produces rather complex results.
the ‘structure’ algorithm tries to reason from the structure of the input to find the ‘correct’ repair: exterior rings bound area, interior holes exclude area. It first makes all rings valid, then shells are merged and holes are subtracted from the shells to generate valid result. It assumes that holes and shells are correctly categorized in the input geometry.
- Parameters:
- method{‘linework’, ‘structure’}, default ‘linework’
Algorithm to use when repairing geometry. ‘structure’ requires GEOS >= 3.10 and shapely >= 2.1.
Added in version 1.1.0.
- keep_collapsedbool, default True
For the ‘structure’ method, True will keep components that have collapsed into a lower dimensionality. For example, a ring collapsing to a line, or a line collapsing to a point. Must be True for the ‘linework’ method.
Added in version 1.1.0.
Examples
>>> from shapely.geometry import MultiPolygon, Polygon, LineString, Point >>> s = geopandas.GeoSeries( ... [ ... Polygon([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]), ... Polygon([(0, 2), (0, 1), (2, 0), (0, 0), (0, 2)]), ... LineString([(0, 0), (1, 1), (1, 0)]), ... ], ... ) >>> s 0 POLYGON ((0 0, 0 2, 1 1, 2 2, 2 0, 1 1, 0 0)) 1 POLYGON ((0 2, 0 1, 2 0, 0 0, 0 2)) 2 LINESTRING (0 0, 1 1, 1 0) dtype: geometry
>>> s.make_valid() 0 MULTIPOLYGON (((1 1, 0 0, 0 2, 1 1)), ((2 0, 1... 1 GEOMETRYCOLLECTION (POLYGON ((2 0, 0 0, 0 1, 2... 2 LINESTRING (0 0, 1 1, 1 0) dtype: geometry