[GIS] Python Shapely – filling small gaps between multi polygons

buffergeopandaspolygonpythonshapely

I have a set of rectangular polygons, which currently form a directional path.
The library I am using is Python Shapely / GeoPandas.

The issue I am having is as the path changes direction, there are small gaps and overlaps between the polygons.

enter image description here

I could iterate through all my polygon objects and calculate the angle in between each intersecting point and generate a new set of polygons but I figure there is a much better way.

I am fairly close – using the shapely buffer function, as seen in the following code snippet. I am able to remove the gaps, but it ends up merging my polygons into 1x. But I require them to remain 2x seperate polygons.

polygons = MultiPolygon([Polygon([(0, 0.5), (0, 2.5), (1, 2.5),(1,0.5)]),Polygon([(0, 0), (1, 1), (2,0),(1,-1)])])
polygons = polygons.buffer(2).buffer(-2)

enter image description here

Best Answer

Here one code that can help you.

from shapely.geometry import MultiPolygon, Polygon
import matplotlib.pyplot as plt
import geopandas as gpd
polygons = MultiPolygon([Polygon([(0, 0.5), (0, 2.5), (1, 2.5),(1,0.5)]),Polygon([(0, 0), (1, 1), (2,0),(1,-1)])])

polygons = gpd.GeoSeries(polygons)

polygons_hull = polygons.convex_hull

plt.plot(*polygons_hull.exterior.xy)