I'm working on groups of shapes (think groups of city blocks) that are almost contiguous, like this:
What I'd like to do is to get the shape of the outer perimeter of this group. With more regular forms, this was almost working:
per_df = gpd.GeoSeries(df.geometry.unary_union.convex_hull).boundary
Bringing something like this:
But on an irregular shape it brings this:
Is there some way to "fuse" or join my shapes into one so it is easier to calculate its perimeter/boundary?
Here's a simpler reproducible example:
p1=Polygon([(0,0),(10,0),(10,9.8),(0,9.8)])
p2=Polygon([(10.2,10),(20,10),(20,20),(10.2,20)])
p3=Polygon([(10,10),(9.8,20),(0,10)])
df=gpd.GeoDataFrame(geometry=[p1,p2,p3])
per_df = gpd.GeoSeries(df.geometry.unary_union.convex_hull).boundary
ax = df.plot()
I'd like to get the perimeter of this group of shapes, even though there's a bit of separation between them:
Thanks!
You can go with:
buf = df.geometry.buffer(10) #or a value that suits you better
unary = buf.unary_union
print(unary.length)
You can also buffer with -10 (or the chosen value) the unary union to be closer to the real shape before calculating the length.