14

I would like to dissolve a shapefile regardless of attribute content using geopandas. The regular dissolve can be accomplished with something like this:

import geopandas

all = geopandas.read_file(r'Inputs\All.shp')    
all = all.dissolve(by='topology')    
all.to_file(driver='ESRI Shapefile', filename=r'Intermediate\All_dissolved.shp')

But the above is using the 'topology' field to aggregate. I have also noticed that the all.dissolve() function exits with an error, indicating that the by parameter is required.

How can I dissolve regardless of the attribute in GeoPandas?

1
  • does it work with an empty string or a True as value for by maybe? Sep 21, 2018 at 13:09

1 Answer 1

25

You will lose all attributes but unary_union will return one big multipolygon which you can turn into singleparts with .geoms:

import geopandas as gpd
df = gpd.read_file(r"C:\Test\New_Shapefile.shp")
df.plot(cmap='cool')

enter image description here

df2 = gpd.geoseries.GeoSeries([geom for geom in df.unary_union.geoms])
df2.plot(cmap='cool')

enter image description here

(You can also add a column, fill with some value and then dissolve by

df['dissolvefield'] = 1
df2 = df.dissolve(by='dissolvefield')
df3 = gpd.geoseries.GeoSeries([geom for geom in df2.geometry.iloc[0].geoms])

)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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