2

Assuming the following:

from shapely.affinity import translate
from shapely.geometry import Polygon
import geopandas

p0 = Polygon( [(0,0), (2,0), (2,2), (0,2)] )
p1 = translate(p0, xoff=1)
p2 = translate(p0, xoff=4)

df = geopandas.GeoDataFrame(geometry=[
    p0, p1, p2
])
df.plot(cmap='cividis', alpha=0.7, edgecolor='black')

input

How can I get an output that dissolves polygons that overlap?

I found this answer but I am not convinced because when I replicate the answer, I get a Dataframe with 3 rows, but it turns out that the rows contain multiple Polygon objects in each row/geometry cell, so there is no real merge done.

My desired output is something like:

p3 = Polygon( [(0,0), (3,0), (3,2), (0,2), (0,0) ])
p4 = Polygon( [(4,0), (6,0), (6,2), (4,2), (4,0) ])

dfout = geopandas.GeoDataFrame(geometry=[
    p3, p4
])
dfout.plot(cmap="cividis", alpha=0.7, edgecolor="black")

output

1

If you are interested only in polygons (do not care about its attributes), you can use unary_union to merge all polygons and then explode resulting multipolygon.

from shapely.affinity import translate
from shapely.geometry import Polygon
import geopandas

p0 = Polygon( [(0,0), (2,0), (2,2), (0,2)] )
p1 = translate(p0, xoff=1)
p2 = translate(p0, xoff=4)

df = geopandas.GeoDataFrame(geometry=[
    p0, p1, p2
])

geoms = df.geometry.unary_union
df = geopandas.GeoDataFrame(geometry=[geoms])

df = df.explode().reset_index(drop=True)

df.plot(cmap='cividis', alpha=0.7, edgecolor='black')

figure

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.