Envelope or convex hull of set of geometries

I have a problem which involves grouping geometries that I am trying to solve. The idea is to group a number of geometrical objects into "sets" or large polygons. Basically, it means either finding the convex hull or the envelope of the union of these polygons as a set (which ever is easiest). Note that the union of disjoint objects exists.

Here is an example:

from shapely.geometry import Polygon, LineString, Point, MultiPoint
s = gpd.GeoSeries(
    [
        Polygon([(4, 4), (4, 2),(2,2), (2, 4)]),
        Polygon([(8, 8), (8, 6),(6,6), (6, 8)]),
        Polygon([(6, 2), (8, 2),(8,4), (6, 4)]),
        Polygon([(2, 6), (2, 8),(4,8), (4, 6)])
    ]
)

sdf = gpd.GeoDataFrame(s).reset_index()
sdf = sdf.rename(columns={0:'geometry'})

produces

enter image description here

I would like to be able to find either the polygon (multipolygon) that contains all the squares, alternatively only two of them. I guess the method would be the same in both cases.

I could obviously manually define that polygon, but this is just an example and 1) the shape of that polygon might not be as simple as the four outermost corners and 2) I want to automate this process.

Is there a way to use s.envelope to do so? The only thing I've managed to do is to find the envelope of individual polygons....which is (in my case) meaningless.

I've looked everywhere and not found anything. Ideally, I would be able to decide either how many polygons can be contained in the convex hull/envelope or define its maximal area.

Upvotes: 1

Views: 1113

Answers (1)

tjaqu787
tjaqu787

Reputation: 327

geopandas.envelope(df) will give you a bounding box of everything

df['new_col']=0
df=df.dissolve(by='new_col')

Will merge everything into a multigon, you can use another feature in 'by' to merge into len(unique(feature)) polygons.

Upvotes: 2

Related Questions