Dissolving vector shapes

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.

Getting ready

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.

How to do it...

We will use the Processing Toolbox for this recipe and specifically a native QGIS algorithm called dissolve, as follows:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. Import the processing module:
    import processing
    
  4. Next, run the 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")
    

How it works...

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:

How it works...

Once the shapes are dissolved, you are left with only the county boundaries, as shown in this image:

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset