Capturing map coordinates in a stand alone app...


For those of you striving to build some stand alone Python apps based on the QGIS Python bindings here is a quick snip for ya:

class MapCoords(object):
  def __init__(self, mainwindow):
    self.mainwindow = mainwindow
    # This one is to capture the mouse move for coordinate display
    QObject.connect(mainwindow.canvas, SIGNAL("xyCoordinates(QgsPoint&)"),
                    self.updateCoordsDisplay)
    self.latlon = QLabel("0.0 , 0.0")
    self.latlon.setFixedWidth(200)
    self.latlon.setAlignment(Qt.AlignHCenter)
    self.latlon.setFrameStyle(QFrame.StyledPanel)
    self.mainwindow.statusbar.addPermanentWidget(self.latlon)

  # Signal handeler for updating coord display
  def updateCoordsDisplay(self, p):
    capture_string = QString(str(p.x()) + " , " +
                             str(p.y()))
    self.latlon.setText(capture_string)

From your main app (QMainWindow object) just create one of these objects:

    # New Map Coords display in status bar
    self.mapcoords = MapCoords(self)

Thats all there is to it... hook up to the main canvas xyCoordinates signal and you just update a simple QLabel on the status bar of the main window.

Go forth and code...

Aaron

EMAIL: aaronr (at) z-pulley.com
WEB : http://www.reprojected.com



API change

Qgis 1.3 (and possibly before) uses a slightly different signal definition:

SIGNAL("xyCoordinates(const QgsPoint &)")

- and if you don't put in the const, the signals just don't happen!