2

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?

  • does it work with an empty string or a True as value for by maybe? – bugmenot123 Sep 21 '18 at 13:09
6

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])

)

  • 1
    @BERA, your answers are always a gold source! – Taras Sep 21 '18 at 12:27

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.