Using a map tool to draw points on the canvas

QGIS contains a built-in functionality to zoom and pan the map in custom applications. It also contains the basic hooks to build your own interactive tools. In this recipe, we'll create an interactive point tool that lets you mark locations on the map by clicking on a point.

Getting ready

We will use the application framework from the previous Adding standard map tools to the canvas recipe, so complete that recipe first. We will extend that application with a new tool. The complete version of this application is available in the code samples provided with this book.

How to do it...

We will set up the button, signal trigger, and actions as we do with all map tools. However, because we are building a new tool, we must also define a class to define exactly what the tool does. To do this, we need to perform the following actions:

  1. First, we define our point tool's button in the actions portion of our application. Place this line after the QAction("Pan") method:
    actionPoint = QAction("Point", self)
    
  2. In the next section, we make sure that when we click on the button, it stays selected:
    actionPoint.setCheckable(True)
    
  3. In the section after that, we define the method that is used when the button is triggered:
    self.connect(actionPoint, SIGNAL("triggered()"), self.point)
    
  4. Now, we add the button to the toolbar along with the other buttons:
    self.toolbar.addAction(actionPoint)
    
  5. Then, we link the application to our specialized tool class:
    self.toolPoint = PointMapTool(self.canvas)
    self.toolPoint.setAction(actionPoint)
    
  6. We set the point tool to be selected when the application loads:
    self.point()
    
  7. Now, we define the method in the main application class for our tool:
    def point(self):
    self.canvas.setMapTool(self.toolPoint)
    
  8. Now, we create a class that describes the type of tool we have and the output it provides. The output is a point on the canvas, defined in the canvasPressEvent method, that receives the button-click event. We will inherit from a generic tool called the QgsMapToolEmitPoint in order to create points:
    classPointMapTool(QgsMapToolEmitPoint):
    def __init__(self, canvas):
    self.canvas = canvas
    QgsMapToolEmitPoint.__init__(self, self.canvas)
    self.point = None
    
    defcanvasPressEvent(self, e):
    self.point = self.toMapCoordinates(e.pos())
    printself.point.x(), self.point.y()
    m = QgsVertexMarker(self.canvas)
    m.setCenter(self.point)
    m.setColor(QColor(0,255,0))
    m.setIconSize(5)
    m.setIconType(QgsVertexMarker.ICON_BOX) # or ICON_CROSS, ICON_X
    m.setPenWidth(3)
    

How it works...

For custom tools, PyQGIS provides a set of generic tools for the common functions that you can extend and piece together. In this case, the EmitPoint tool handles the details of the events and map functionality when you click on a location on the map.

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

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