Reprojecting a vector layer
Reprojecting a vector layer is inherent to QGIS; however, to access it from Python, we must use the Processing Toolbox.
Getting ready
For this recipe, we'll need the Mississippi cities shapefile in the Mississippi Transverse Mercator Projection, which can be downloaded as a ZIP file here:
https://github.com/GeospatialPython/Learn/raw/master/MSCities_MSTM.zip
Extract the zipped shapefile to a directory named /qgis_data/ms
.
How to do it...
To reproject the layer, we'll simply call the qgis:reprojectlayer
processing algorithm specifying the input shapefile, the new projection, and the output file name.
Start QGIS.
From the Plugins menu, select Python Console.
First, we need to import the processing module:
import processing
Next, we run the reprojection algorithm:
processing.runalg("qgis:reprojectlayer", "/qgis_data/ms/MSCities_MSTM.shp","epsg:4326", "/qgis_data/ms/MSCities_MSTM_4326.shp"...