Dissolving polygons based on attributes with Python (shapely, fiona)?

I have been trying to create a function that does basically the same thing that the QGIS "dissolve" function. I thought it would be super easy but well apparently not. So from what I have gathered around, the use of fiona with shapely should be the best option here. I just began to mess around with vector files so this world is pretty new to me and python as well.

For these example, I am working with a county shapefile founded here http://tinyurl.com/odfbanu So here are some piece of code I gathered but can't find a way to make them work together

For now my best method is the following based on : https://sgillies.net/2009/01/27/a-more-perfect-union-continued.html. It works fine and I get a list of the 52 states as Shapely geometry. Please feel free to comment if there is a more straight forward way to do this part.

``````from osgeo import ogr
from shapely.wkb import loads
from numpy import asarray
from shapely.ops import cascaded_union

ds = ogr.Open('counties.shp')
layer = ds.GetLayer(0)

#create a list of unique states identifier to be able
#to loop through them later
STATEFP_list = []
for i in range(0 , layer.GetFeatureCount()) :
feature = layer.GetFeature(i)
statefp = feature.GetField('STATEFP')
STATEFP_list.append(statefp)

STATEFP_list = set(STATEFP_list)

#Create a list of merged polygons = states
#to be written to file
polygons = []

#do the actual dissolving based on STATEFP
#and append polygons
for i in STATEFP_list :
county_to_merge = []
layer.SetAttributeFilter("STATEFP = '%s'" %i )
#I am not too sure why "while 1" but it works
while 1:
f = layer.GetNextFeature()
if f is None: break
g = f.geometry()
county_to_merge.append(loads(g.ExportToWkb()))

u = cascaded_union(county_to_merge)
polygons.append(u)

#And now I am totally stuck, I have no idea how to write
#this list of shapely geometry into a shapefile using the
#same properties that my source.
``````

So the writing is really not straight forward from what I have seen, I really just want the same shapefile only with the country dissolve into states, I don't even need much of the attribute table but I am curious to see how you can pass it on from the source to the new created shapefile.

I found many pieces of code for writing with fiona but I am never able to make it work with my data. Example from How to write Shapely geometries to shapefiles? :

``````from shapely.geometry import mapping, Polygon
import fiona

# Here's an example Shapely geometry
poly = Polygon([(0, 0), (0, 1), (1, 1), (0, 0)])

# Define a polygon feature geometry with one attribute
schema = {
'geometry': 'Polygon',
'properties': {'id': 'int'},
}

# Write a new Shapefile
with fiona.open('my_shp2.shp', 'w', 'ESRI Shapefile', schema) as c:
## If there are multiple geometries, put the "for" loop here
c.write({
'geometry': mapping(poly),
'properties': {'id': 123},
})
``````

The problem here is how to do the same with a list of geometry and how to recreate the same properties than the source.

3 Answers

The question is about Fiona and Shapely and the other answer using GeoPandas requires to also know Pandas. Moreover GeoPandas uses Fiona to read/write shapefiles.

I do not question here the utility of GeoPandas, but you can do it directly with Fiona using the standard module itertools, specially with the command groupby ("In a nutshell, groupby takes an iterator and breaks it up into sub-iterators based on changes in the "key" of the main iterator. This is of course done without reading the entire source iterator into memory", itertools.groupby).

Original Shapefile coloured by the STATEFP field

``````from shapely.geometry import shape, mapping
from shapely.ops import unary_union
import fiona
import itertools
with fiona.open('cb_2013_us_county_20m.shp') as input:
# preserve the schema of the original shapefile, including the crs
meta = input.meta
with fiona.open('dissolve.shp', 'w', **meta) as output:
# groupby clusters consecutive elements of an iterable which have the same key so you must first sort the features by the 'STATEFP' field
e = sorted(input, key=lambda k: k['properties']['STATEFP'])
# group by the 'STATEFP' field
for key, group in itertools.groupby(e, key=lambda x:x['properties']['STATEFP']):
properties, geom = zip(*[(feature['properties'],shape(feature['geometry'])) for feature in group])
# write the feature, computing the unary_union of the elements in the group with the properties of the first element in the group
output.write({'geometry': mapping(unary_union(geom)), 'properties': properties[0]})
``````

Result

• Nice one too, hard to choose between the both. Nice to see different methods thank you! – User18981898198119 Jun 8 '15 at 17:56

I highly recommend GeoPandas for dealing with large assortments of features and performing bulk operations.

It extends Pandas dataframes, and uses shapely under the hood.

``````from geopandas import GeoSeries, GeoDataFrame

# define your directories and file names
dir_input = '/path/to/your/file/'
name_in = 'cb_2013_us_county_20m.shp'
dir_output = '/path/to/your/file/'
name_out = 'states.shp'

# create a dictionary
states = {}
# open your file with geopandas
counties = GeoDataFrame.from_file(dir_input + name_in)

for i in range(len(counties)):
state_id = counties.at[i, 'STATEFP']
county_geometry = counties.at[i, 'geometry']
# if the feature's state doesn't yet exist, create it and assign a list
if state_id not in states:
states[state_id] = []
# append the feature to the list of features
states[state_id].append(county_geometry)

# create a geopandas geodataframe, with columns for state and geometry
states_dissolved = GeoDataFrame(columns=['state', 'geometry'], crs=counties.crs)

# iterate your dictionary
for state, county_list in states.items():
# create a geoseries from the list of features
geometry = GeoSeries(county_list)
# use unary_union to join them, thus returning polygon or multi-polygon
geometry = geometry.unary_union
# set your state and geometry values
states_dissolved.set_value(state, 'state', state)
states_dissolved.set_value(state, 'geometry', geometry)

# save to file
states_dissolved.to_file(dir_output + name_out, driver="ESRI Shapefile")
``````
• That is wayy more elegant than my hacky weird thing. thanks ! Is there a way to pass on the spatial reference system? – User18981898198119 Jun 5 '15 at 23:57
• I've edited my post to show how to transfer the crs from the source file to the new file. This happens at the line where the states_dissolved GeoDataFrame is created. Regarding the Schema, I'd suggest just creating one manually (i.e. using columns property of same line) which are then written to the properties of the new file. i.e. when you create your states dictionary, just add any other properties as you go and assign them to a column in the new data frame. – songololo Jun 6 '15 at 9:50

As an addendum to @gene's answer, I needed to dissolve by more than one field so modified his code to handle multiple fields. The code below utilizes `operator.itemgetter` to group by multiple fields:

``````# Modified from https://gis.stackexchange.com/a/150001/2856
from shapely.geometry import shape, mapping
from shapely.ops import unary_union
import fiona
import itertools
from operator import itemgetter

def dissolve(input, output, fields):
with fiona.open(input) as input:
with fiona.open(output, 'w', **input.meta) as output:
grouper = itemgetter(*fields)
key = lambda k: grouper(k['properties'])
for k, group in itertools.groupby(sorted(input, key=key), key):
properties, geom = zip(*[(feature['properties'], shape(feature['geometry'])) for feature in group])
output.write({'geometry': mapping(unary_union(geom)), 'properties': properties[0]})

if __name__ == '__main__':
dissolve('input.shp', 'input_dissolved.shp', ["FIELD1", "FIELD2", "FIELDN"))
``````