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("/Applications/QGIS.app/Contents/MacOS/", True)
    # Windows:
    # app.setPrefixPath("C:/Program Files/QGIS Valmiera/apps/qgis", True)
    
  3. Next, we begin initializing the class:
    class MyWnd(QMainWindow):
      def __init__(self):
    
  4. Now, we can initialize the application and create the map canvas:
    QMainWindow.__init__(self)
    QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/MacOS/", True)
    QgsApplication.initQgis()
    self.canvas = QgsMapCanvas()
    self.canvas.setCanvasColor(Qt.white)
    
  5. Then, we can load the shapefile layer and add it to the canvas:
    self.lyr = QgsVectorLayer("/Users/joellawhead/qgis_data/ms/mississippi.shp", "Mississippi", "ogr")
    QgsMapLayerRegistry.instance().addMapLayer(self.lyr)
    self.canvas.setExtent(self.lyr.extent())
    self.canvas.setLayerSet([QgsMapCanvasLayer(self.lyr)])
    self.setCentralWidget(self.canvas)
    
  6. Next, we define the buttons that will be visible on the toolbar:
    actionZoomIn = QAction("Zoom in", self)
    actionZoomOut = QAction("Zoom out", self)
    actionPan = QAction("Pan", self)
    actionZoomIn.setCheckable(True)
    actionZoomOut.setCheckable(True)
    actionPan.setCheckable(True)
    
  7. Now, we connect the signal created when the buttons are clicked to the Python methods that will provide each tool's functionality:
    actionZoomIn.triggered.connect(self.zoomIn)
    actionZoomOut.triggered.connect(self.zoomOut)
    actionPan.triggered.connect(self.pan)
    
  8. Next, we create our toolbar and add the buttons:
    self.toolbar = self.addToolBar("Canvas actions")
    (actionZoomIn)
    self.toolbar.addAction(actionZoomOut)
    self.toolbar.addAction(actionPan)
    
  9. Then, we connect the buttons to the applications states:
    self.toolPan = QgsMapToolPan(self.canvas)
    self.toolPan.setAction(actionPan)
    self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in
    self.toolZoomIn.setAction(actionZoomIn)
    self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out
    self.toolZoomOut.setAction(actionZoomOut)
    
  10. Then, we define which button will be selected when the application loads:
    self.pan()
    
  11. Now, we define the Python methods that control the application's behavior for each tool:
    defzoomIn(self):
    self.canvas.setMapTool(self.toolZoomIn)
    defzoomOut(self):
    self.canvas.setMapTool(self.toolZoomOut)
    def pan(self):
    self.canvas.setMapTool(self.toolPan)
    
  12. Then, we create a Qt application that uses our application window class:
    class MainApp(QApplication):
    def __init__(self):
    QApplication.__init__(self,[],True)
    wdg = MyWnd()
    wdg.show()
    self.exec_()
    
  13. Finally, we enter the program's main loop:
    if __name__ == "__main__":
    import sys
    app = MainApp()
    

How it works...

An application is a continuously running program loop that ends only when we quit the application. QGIS is based on the Qt windowing library, so our application class inherits from the main window class that provides the canvas and the ability to create toolbars and dialogs.This is a lot of setup, even for an extremely simple application, but once the framework for an application is complete, it becomes much easier to extend it.

..................Content has been hidden....................

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