Rasterizing a vector layer

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.

Getting ready

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.

How to do it...

We will run the gdalogr:rasterize algorithm to convert this vector data to a raster, as follows:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. Import the processing module:
    import processing
    
  4. Run the algorithm, specifying the input data, the attribute from which raster values need to be drawn, 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")
    

How it works...

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:

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset