Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def resolve_overlaps(geometries, relative_threshold=0.1):
"""For any pair of overlapping geometries, assigns the overlapping area to the
geometry that shares the most perimeter with the overlap. Returns the GeoSeries
of geometries, which will have no overlaps.
If the ratio of the overlap's area to either of the overlapping geometries'
areas is greater than `relative_threshold`, then the overlap is ignored.
The default `relative_threshold` is `0.1`. This default is chosen to include
tiny overlaps that can be safely auto-fixed while preserving major overlaps
that might indicate deeper issues and should be handled on a case-by-case
basis. Set `relative_threshold=None` to resolve all overlaps.
"""
geometries = get_geometries(geometries)
inters = adjacencies(geometries, warn_for_islands=False, warn_for_overlaps=False)
overlaps = inters[inters.area > 0].buffer(0)
if relative_threshold is not None:
left_areas, right_areas = split_by_level(geometries.area, overlaps.index)
under_threshold = ((overlaps.area / left_areas) < relative_threshold) & (
(overlaps.area / right_areas) < relative_threshold
)
overlaps = overlaps[under_threshold]
if len(overlaps) == 0:
return geometries
to_remove = GeoSeries(
pandas.concat([overlaps.droplevel(1), overlaps.droplevel(0)]), crs=overlaps.crs
)
def close_gaps(geometries, relative_threshold=0.1):
"""Closes gaps between geometries by assigning the hole to the polygon
that shares the most perimeter with the hole.
If the area of the gap is greater than `relative_threshold` times the
area of the polygon, then the gap is left alone. The default value
of `relative_threshold` is 0.1. This is intended to preserve intentional
gaps while closing the tiny gaps that can occur as artifacts of
geospatial operations. Set `relative_threshold=None` to close all gaps.
"""
geometries = get_geometries(geometries)
gaps = holes_of_union(geometries)
return absorb_by_shared_perimeter(
gaps, geometries, relative_threshold=relative_threshold
)
def holes_of_union(geometries):
"""Returns any holes in the union of the given geometries."""
geometries = get_geometries(geometries)
if not all(
isinstance(geometry, (Polygon, MultiPolygon)) for geometry in geometries
):
raise TypeError("all geometries must be Polygons or MultiPolygons")
union = unary_union(geometries)
series = holes(union)
series.crs = geometries.crs
return series
def get_geometries_with_range_index(geometries):
gdf = geopandas.GeoDataFrame({"geometry": get_geometries(geometries)}).set_index(
pandas.RangeIndex(len(geometries))
)
return gdf.geometry