Wednesday 17 January 2018

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.



Answer



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


enter image description here


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


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...