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

Creating the simplest map renderer

In order to turn a dynamic GIS map into a static map image or document, you must create a renderer to freeze the map view and create a graphic version of it. In this recipe, we'll render a map to a JPEG image and save it.

Getting ready

You will need to download the following zipped shapefile and extract it to your qgis_data directory, to a subdirectory named hancock:

You will also need to open the Python Console under the Plugins menu in QGIS. You can run these lines of code inside the console.

How to do it...

In this recipe, we will load our shapefile, add it to the map, create a blank image, set up the map view, render the map image, and save it. To do this, we need to perform the following steps:

  1. First, we need to import the underlying Qt libraries required for image handling:

    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
  2. Next, we load the layer and add it to the map:

    lyr = QgsVectorLayer("/qgis_data...