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:

https://geospatialpython.googlecode.com/svn/hancock.zip

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/hancock/hancock.shp", "Hancock", "ogr")
    reg = QgsMapLayerRegistry.instance()
    reg.addMapLayer(lyr)
    
  3. Now, we create a blank image to accept the map image:
    i = QImage(QSize(600,600), QImage.Format_ARGB32_Premultiplied)
    c = QColor("white")
    i.fill(c.rgb())
    p = QPainter()
    p.begin(i)
    
  4. Then, we access the map renderer:
    r = QgsMapRenderer()
    
  5. Now, we get the IDs of the map layers:
    lyrs = reg.mapLayers().keys()
    
  6. Then, we use the newly initialized renderer layers in the map:
    r.setLayerSet(lyrs)
    
  7. Now, we get the full extent of the map as a rectangle:
    rect = QgsRectangle(r.fullExtent())
    
  8. Then, we set a scale for the renderer. Smaller numbers produce a larger map scale, and larger numbers produce a smaller map scale. We can change the map scale to create a buffer around the map image:
    rect.scale(1.1)
    
  9. Next, we set the extent of the renderer to the rectangle:
    r.setExtent(rect)
    
  10. Now we set the output size and resolution of the image. The resolution is automatically calculated:
    r.setOutputSize(i.size(), i.logicalDpiX())
    
  11. Now, we can render the map and finalize the image:
    r.render(p)
    p.end()
    
  12. Finally, we save the map image:
    i.save("/qgis_data/map.jpg","jpg")
    
  13. Verify that you have a map image in your qgis_data directory, similar to the map displayed in QGIS.

How it works...

QGIS uses the underlying Qt GUI library to create common image types. We haven't used any of the QGIS composer objects to render the image; however, this rendering technique is used to save maps created with the QGIS composer.

There's more...

The QImage object supports other image formats as well. To save a map image to a PNG, replace the last step in the How to do it… section with the following code:

i.save("/qgis_data/map.png","png")
..................Content has been hidden....................

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