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.
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
.
Generalizing is native to QGIS, but we will access it in PyQGIS through the Processing Toolbox using the qgis:simplifygeometries
algorithm, as follows:
processing
module:import processing
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")
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:
The simplified version has only 10
points, as seen in the following image: