2

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

  • 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 8 '16 at 8:56
2

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

polygons_hull.plot()
plt.show()
  • 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 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.