Sometimes, a raster dataset is the most efficient way to display a complex vector that is merely a backdrop in a map. In these cases, you can rasterize a vector layer to turn it into an image.
We will demonstrate how to rasterize a vector layer using the following contour shapefile, which you can download from https://geospatialpython.googlecode.com/svn/contour.zip.
Extract it to your /qgis_data/rasters
directory.
We will run the gdalogr:rasterize
algorithm to convert this vector data to a raster, as follows:
processing
module:import processing
0
in order to specify pixel dimensions for the output instead of map dimensions, width and height, and finally the output raster name:processing.runalg("gdalogr:rasterize","/qgis_data/rasters/contour.shp","ELEV",0,1000,1000,"/qgis_data/rasters/contour.tif")
If you want to specify the output dimensions in map units, use 1
instead of 0
. Note that the symbology of the layer becomes frozen once you convert it to a raster. The raster is also no longer dynamically scalable.
The following image shows the rasterized output of the elevation contour shapefile: