Search code examples
pythongeopandasshapely

Get perimeter out of irregular group of shapes


I'm working on groups of shapes (think groups of city blocks) that are almost contiguous, like this:

enter image description here

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:

enter image description here

But on an irregular shape it brings this:

enter image description here

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: enter image description here

Thanks!


Solution

  • 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.