abstract background with bright blots of paint
|

PyQGIS: Dissolve Vector Features with Python and QGIS

The QGIS Python API (PyQGIS) makes it really simple automate a feature dissolve. PyQGIS gives access to all the QGIS processing tools directly from the Python interface. This tutorial will demonstrate three different dissolve examples with Python in QGIS.

Basic Dissolve with Python

First, lets dissolve polygon features into a single polygon, this is the most simple buffer operation. This operation is useful when you have created a buffer for multiple stream segments but want the buffer to outline the entire stream network and not individual segments. You would want to perform a similar result if you had buffered a road network and had individual polygons representing the buffer around each road.

Define the path to the input vector file (file with multiple features that should be dissolved) and define a path for the output file (new path that will be created by the operation. Preforming the dissolve is as simple as calling the processing tool and specifying the inputs. With PyQGIS use of a processing tool is specified with processing.run(), then specify the processing tool, and input arguments. In this case the tool is the QGIS native dissolve tool ("native:dissolve"). Input arguments to the dissolve tool are passed as a Python dictionary, as shown in the code block below.

import processing

#input and output file names
infn = "path/to/dissolve"
outfn = "output/path/basic/dissolve"

#basic dissolve: dissolve all features into a single feature
processing.run("native:dissolve", {'INPUT':infn, 'OUTPUT':outfn})

Dissolve by Feature Values

A more advanced is example is dissolving features based on an attribute value. For example, with a buffered road network we may want all the interstate buffers to be one feature, all the state highways to be another, and municipal roads another. This is accomplished by specifying the FIELD argument in the input dictionary. Otherwise, the Python code for the dissolve remains the same.

import processing

infn = "path/to/dissolve"
outfn2 = "output/path/value/dissolve"

#dissolve features based on values in an attribute table field
processing.run("native:dissolve", {'INPUT':infn, FIELD':['GRID_CODE'], 'OUTPUT':outfn2})

Dissolve During Buffer Calculation

To save a processing step, write less code. You can use Python to to call the buffer processing tool and dissolve the buffer result. To do this simply specify the DISSOLVE input argument to True in the Python dictionary. Now the output will be dissolved and give you the result of a simple dissolve without running another tool.

import processing

#input and output file names
infn2 = "path/to/buffer/then/dissolve"
outfn3 = "output/path/buffer/dissolve"

#dissolve buffer output into a single feature
processing.run("native:buffer", {'INPUT':infn2, 'DISTANCE':20, 'DISSOLVE':True, 'OUTPUT':outfn3})

Conclusion

The PyQGIS API makes it very simple to automate complicated algorithms and processing tools using Python. Dissolving with Python in QGIS is especially flexible and can be done as a standalone dissolve, or incorporated into buffer processing. Below is the full script to accomplish all three examples from this article.

import processing

#input and output file names
infn = "path/to/dissolve"
infn2 = "path/to/buffer/then/dissolve"
outfn = "output/path/basic/dissolve"
outfn2 = "output/path/value/dissolve"
outfn3 = "output/path/buffer/dissolve"

#basic dissolve: dissolve all features into a single feature
processing.run("native:dissolve", {'INPUT':infn, 'OUTPUT':outfn})

#dissolve features based on values in an attribute table field
processing.run("native:dissolve", {'INPUT':infn, \
FIELD':['GRID_CODE'], 'OUTPUT':outfn2})

#dissolve buffer output into a single feature
processing.run("native:buffer", {'INPUT':infn2, 'DISTANCE':20, \
'DISSOLVE':True, 'OUTPUT':outfn3})

#add layer to the QGIS interface
iface.addVectorLayer(outfn3, '', 'ogr')

Similar Posts