3

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

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Matt
  • 39
  • 1
  • 2
  • 1
    Does shapely have a convex hull method? Which would essentially do what you want. To split them you'll need to create a line at the desired location and difference the line from the poly. – songololo Jul 08 '16 at 08:56

1 Answers1

4

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)
decodering
  • 133
  • 6
Diogo Caribé
  • 1,960
  • 1
  • 23
  • 37
  • Thanks! This also pointed me in the right direction. Just a minor note: it is also possible to use convex_hull function from shapely (e.g. import shapely.geometry as geometry and then using polygon = multi_polygon.convex_hull on a geometry.multipolygon.MultiPolygon – Alex Jan 12 '18 at 07:31