Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
About the Author
About the Reviewers

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

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: