0

I would like to take control of the mouse pointer in QGIS (for example constraining it to a given extent...) Is there any way to do that using PyQGIS?

  • Not sure I could ever see a good use for that. What are you trying to do? – Nathan W Jul 30 '14 at 8:29
  • i'm not sure to be able to give good example either ... but i see many examples of ways to constrain user inputs when editing table attributes so i would like to be able to do the same on feature geometries. For example when it comes to create a new polygon i would like to be able to constrain this creation to a given area of interest (in the current view extent)... – snaileater Jul 30 '14 at 9:26
  • 1
    there is a plugin called CadInput github.com/olivierdalang/CadInput. It constrains the "pointer" in various ways but it is not the "real" mouse pointer. it's an "artificial" program pointer, similar to what you can see in AutoCAD. You could see the source on the above website – Piotr Sadowski Jul 30 '14 at 9:38
1

This standalone python snippet uses the setPos method of QCursor to move my mouse pointer to the top left corner of my screen:

 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 app = QApplication([])
 cur = QCursor()
 cur.setPos(0,0)

(Note this is standalone python, not QGIS python. But the last two lines should work in a QGIS python console.)

You could then constrain it to an extent by trapping mouse events and resetting. You need a good understanding of Qt (the underlying user interface library of QGIS) to do this well. Search for "qt move mouse pointer" and similar for much enlightenment.

0

Use QgsMapTool This scrip captures the mouse location from a projected map, reproject back to a geographical coordinates system ad display the coordinates.

from qgis import gui from PyQt4 import QtGui, QtCore

class PointTool(gui.QgsMapTool): def init(self, canvas): gui.QgsMapTool.init(self, canvas) self.canvas = canvas self.crsScr = qgis.core.QgsCoordinateReferenceSystem(100006, qgis.core.QgsCoordinateReferenceSystem.InternalCrsId) self.crsDest = qgis.core.QgsCoordinateReferenceSystem(100000, qgis.core.QgsCoordinateReferenceSystem.InternalCrsId) self.xform = qgis.core.QgsCoordinateTransform(self.crsScr, self.crsDest)

def canvasPressEvent(self, event):
    pass

def canvasReleaseEvent(self, event):
    x = event.pos().x()
    y = event.pos().y()

    point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
    mapPointDMS = self.canvas.getCoordinateTransform().toMapPoint(x, y).toDegreesMinutesSeconds(3, True, True)
    #mapPointDecimals = self.xform.transform(point.X())

    print "point (x,y)          = ",point
    print "point DMS            = ", mapPointDMS
    #print "point Decimal Degree = ", mapPointDecimals
    xcord = float(point.toString().split(',')[0])
    ycord = float(point.toString().split(',')[1])
    print xcord, ycord
    mapPointDecimals = self.xform.transform(qgis.core.QgsPoint(xcord, ycord ), qgis.core.QgsCoordinateTransform.ReverseTransform)

    print "point Decimal Degree = ", mapPointDecimals
    print "point DMS = ", mapPointDecimals.toDegreesMinutesSeconds(3, True, True)
    layer = qgis.utils.iface.activeLayer()

    displayLatLong = qgis.gui.QgsMapTip()
    displayLatLong.showMapTip(layer, mapPointDecimals, QtCore.QPoint(x, y), self.canvas)

def canvasMoveEvent(self, event):
    x = event.pos().x()
    y = event.pos().y()

    point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
    mapPointDMS = self.canvas.getCoordinateTransform().toMapPoint(x, y).toDegreesMinutesSeconds(3, True, True)
    xcord = float(point.toString().split(',')[0])
    ycord = float(point.toString().split(',')[1])
    mapPointDecimals = self.xform.transform(qgis.core.QgsPoint(xcord, ycord ), qgis.core.QgsCoordinateTransform.ReverseTransform)
    layer = qgis.utils.iface.activeLayer()

    displayLatLong = qgis.gui.QgsMapTip()
    displayLatLong.showMapTip(layer, mapPointDecimals, QtCore.QPoint(x, y), self.canvas)
    print "point DMS = ", mapPointDecimals.toDegreesMinutesSeconds(3, True, True)

def activate(self):
    pass

def deactivate(self):
    pass

def isZoomTool(self):
    return False

def isTransient(self):
    return False

def isEditTool(self):
    return True

pointTool = PointTool(qgis.utils.iface.mapCanvas()) qgis.utils.iface.mapCanvas().setMapTool(pointTool)

  • That's not my question ... – snaileater Sep 11 '15 at 22:35

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.