Dissolving shapes can take two different forms. You can combine a group of adjoining shapes by the outermost boundary of the entire dataset, or you can also group the adjoining shapes with the same attribute value.
Download the GIS census tract shapefile, which contains tracts for several counties from https://geospatialpython.googlecode.com/files/GIS_CensusTract.zip.
Extract it to your /qgis_data
directory, in a directory called census
.
We will use the Processing Toolbox for this recipe and specifically a native QGIS algorithm called dissolve
, as follows:
processing
module:import processing
dissolve
algorithm, specifying the input data—False
to specify that we don't want to dissolve all the shapes into one but to use an attribute instead—the attribute we want to use, and the output filename:processing.runandload("qgis:dissolve","/qgis_data/census/GIS_CensusTract_poly.shp",False,"COUNTY_8","/qgis_data/census/dissovle.shp")
By only changing the boolean in the statement to True
, we can dissolve all adjoining shapes into one. It is also important to note that QGIS will assign the fields of the first shape it encounters in each group to the final shape. In most cases, this will make the attributes virtually useless. This operation is primarily a spatial task.
You can see that 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: