Dissolving vector features
Dissolving a group's geometry allows you to produce a single geometry with common attributes. In this recipe, we'll dissolve some census data into a single dataset.
Getting ready
Download the GIS census tract shapefile, which contains tracts for several counties from the following URL:
https://github.com/GeospatialPython/Learn/raw/master/GIS_CensusTract.zip
Extract it to your /qgis_data
directory in a directory called census
.
How to do it...
We will use the processing toolbox for this recipe and specifically a native QGIS algorithm called dissolve
:
- Start QGIS.
- From the Plugins menu, select Python Console.
- We must import the
processing
module:import processing
- Next, we run the dissolve algorithm specifying the input data
False
to specify we don't want to dissolve all shapes into one but to use an attribute instead, the attribute we want to use, and the output file name:processing.runandload("qgis:dissolve","/qgis_data/census/ GIS_CensusTract_poly.shp",False,"COUNTY_8", "/qgis_data/census/dissovle.shp")
How it works...
By changing the only boolean in the statement to True,
we could dissolve all adjoining features into one. It is also very important to note that QGIS will assign the fields of the first features it encounters in each group to the final shape. In most cases, that will make the attributes virtually useless. This operation is primarily a spatial task.
You can see each county boundary has a number of census tracts in the original layer, as shown in the following image:
Once the shapes are dissolved, you are left with only the county boundaries, as shown in this image: