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.
- Start QGIS.
- From the Plugins menu, select Python Console.
- Import the
processing
module:import processing
- 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:
The simplified version has only ten points as seen in the following image: