Generalizing a vector layer

Generalizing the geometry, also known as simplifying, removes points from a vector layer to reduce the space required to store the data on disk, the bandwidth needed to move it over a network, and the processing power required to perform analysis with it or display it in QGIS. In many cases, the geometry of a layer contains redundant points along with straight lines that can be removed without changing the spatial properties of a layer, with the exception of topology constraints.

Getting ready

For this recipe, we will use a boundary file for the state of Mississippi, which you can download from https://geospatialpython.googlecode.com/files/Mississippi.zip.

Extract the zipped shapefile to a directory named /qgis_data/ms.

How to do it...

Generalizing is native to QGIS, but we will access it in PyQGIS through the Processing Toolbox using the qgis:simplifygeometries algorithm, as follows:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. Import the processing module:
    import processing
    
  4. Now, run the processing algorithm, specifying the algorithm name, input data, tolerance value, spacing between points — which defines how close two points are in map units before one is deleted — and the output dataset's name:
    processing.runandload("qgis:simplifygeometries","/qgis_data/ms/mississippi.shp",0.3,"/qgis_data/ms/generalize.shp")
    

How it works...

The simplicity of the simplifygeometries command makes the operation look simple. However, the simplification is itself quite complex. The same settings rarely produce desirable results across multiple datasets.

The shapefile in this recipe starts out quite complex with hundreds of points, as seen in the following visualization:

How it works...

The simplified version has only 10 points, as seen in the following 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