QGIS Python Programming Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Generalizing a vector layer

Generalizing geometry, also known as simplifying, removes points from a vector layer to reduce the space required to store the data. Otherwise, simplification may result in small but acceptable changes within a specified tolerance.

Getting ready

For this recipe, we will use a boundary file for the state of Mississippi, which you can download from the following URL:

https://github.com/GeospatialPython/Learn/raw/master/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.

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. Import the processing module:
            import processing 
    
  4. Now, we run the processing algorithm specifying the algorithm name, the input data, a tolerance value spacing between points, and the output dataset name:
            processing.runandload("qgis:simplifygeometries",
                                  "/qgis_data/ms/mississippi.shp",
                                  0.3,"/qgis_data/ms/generalize.shp") 
    

How it works...

The simplicity of this command makes the operation look simple. However, simplification is quite complex. The same settings rarely produce desirable results across multiple datasets. You should note the simplification operation is not topological and can result in gaps, disconnects, or overlaps between neighboring features.

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 ten points as seen in the following image:

How it works...