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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...