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

Adding standard map tools to the canvas


In this recipe, you'll learn how to add standard map navigation tools to a standalone map canvas. Creating the simplest possible interactive application provides a framework to begin building specialized geospatial applications using QGIS as a library.

Getting ready

Download the following zipped shapefile and extract it to your qgis_data directory into a folder named ms from https://geospatialpython.googlecode.com/files/Mississippi.zip.

How to do it...

We will walk through the steps required to create a map canvas, add a layer to it, and then add some tools to zoom and pan the map, as follows:

  1. First, because we are working outside the QGIS Python interpreter, we need to import some QGIS and Qt libraries:

    from qgis.gui import *
    from qgis.core import *
    from PyQt4.QtGui import *
    from PyQt4.QtCore import SIGNAL, Qt
    import sys, os
    
  2. Then, we must set the location of our main QGIS application directory. This setting is platform-dependent:

    # OSX:
    QgsApplication.setPrefixPath...