5

I am working on a Python script which will give me the x,y and depth coordinates in QGIS. I have depth as an attribute of the raster layer. My objective is to get a status bar which shows x,y and the depth coordinate when the mouse is at a certain point of the canvas. The code I have used is:

from qgis.gui import *
from PyQt4.QtGui import QAction, QMainWindow, QApplication
from PyQt4.QtCore import SIGNAL, Qt


class MyWnd(QMainWindow):
  def __init__(self,layer):
    QMainWindow.__init__(self)
    self.statusBar().showMessage(u"x:-,y:-")
    self.canvas = QgsMapCanvas()
    self.canvas.setCanvasColor(Qt.white)

    self.canvas.setExtent(layer.extent())
    self.canvas.setLayerSet([QgsMapCanvasLayer(layer)])

    self.setCentralWidget(self.canvas)

    actionZoomIn = QAction("Zoom in", self)
    actionZoomOut = QAction("Zoom out", self)
    actionPan = QAction("Pan", self)

    actionZoomIn.setCheckable(True)
    actionZoomOut.setCheckable(True)
    actionPan.setCheckable(True)

    self.connect(actionZoomIn, SIGNAL("triggered()"), self.zoomIn)
    self.connect(actionZoomOut, SIGNAL("triggered()"), self.zoomOut)
    self.connect(actionPan, SIGNAL("triggered()"), self.pan)

    self.toolbar = self.addToolBar("Canvas actions")
    self.toolbar.addAction(actionZoomIn)
    self.toolbar.addAction(actionZoomOut)
    self.toolbar.addAction(actionPan)

    # create the map tools
    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)

    self.pan()

  def zoomIn(self):
    self.canvas.setMapTool(self.toolZoomIn)

  def zoomOut(self):
    self.canvas.setMapTool(self.toolZoomOut)

  def pan(self):
    self.canvas.setMapTool(self.toolPan)

  def eventFilter(self,source,event):
    if event.type()==Qt.Event.MouseMove:
      if event.buttons()==Qt.NoButton:
        pos=event.pos()
        x=pos.x()
        y=pos.y()
        p=selfcanvas.getCoordinateTransform().toMapCoordinates(x,y)
        self.statusBar().showMessage(u"x:%s,y:%s" %(p.x(),p.y()))
      else:
        pass
    return QMainWindow.eventFilter(self,source,event)

This file I have saved as mywnd.py in the Python27 folder Then in Python console in QGIS I use:

import mywnd


w=mywnd.MyWnd(qgis.utils.iface.activeLayer())
w.show()

This gives me my active layer but I still can't figure out how to get the values of x and y in the statusbar. I also am not very good at Python.

8
  • Well, no, I am getting the layer which I am working on in a separate window and I need the coordinates. This link would have been useful if I wanted to give a custom message. The thing is I am not able to connect the eventFilter and MyWnd Classes. – Sunny Naik Jun 2 '17 at 8:40
  • Did you try to use self.installEventFilter(self) in __init__? – Matthias Kuhn Jun 2 '17 at 8:46
  • I had not tried that before, it now Gives me an error saying 'Qt' has no attribute 'Event' at the line event.type()==Qt.Event.Mouse.Move: – Sunny Naik Jun 2 '17 at 8:55
  • 1
    Then 1) that's a hint that the event is called (which is good) and 2) that's a hint that the event code has a bug (which is bad). -> replace Qt.Event.Mouse.Move with QEvent.MouseMove ;) – Matthias Kuhn Jun 2 '17 at 9:01
5

Some notes

An event filter...

... needs to be installed or it's never going to be called

def __init__(self):
    ...
    self.installEventFilter(self)

The event types...

... are enumerated in QEvent::Type, so you need to use QEvent.MouseMove.

Mouse tracking ...

... is disabled by default (for performance reason), so you need to explicitly enable it if you want to receive move events it in the event filter even when no button is clicked

self.setMouseTracking(True)

Code style

New style connections are better

Use something like

self.actionZoomIn.triggered.connect(self.zoomIn)

Wildcard imports

... are discouraged.

You are already doing things correctly for from PyQt4.QtGui import xxx. Do the same for the qgis imports. Do not use from qgis.gui import * but instead

from qgis.gui import QgsMapCanvas, QgsMapToolPan, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.