Are there any procedures when working with QgsMapCanvas (pyqgis, qgis) in order to obtain a pair of coordinates by clicking on the map canvas?
Are there any sample Python codes that demonstrate this?
Are there any procedures when working with QgsMapCanvas (pyqgis, qgis) in order to obtain a pair of coordinates by clicking on the map canvas?
Are there any sample Python codes that demonstrate this?
This example from http://www.qgisworkshop.org shows the components you would need to get what you want. It's for a slightly older version of QGIS but should be a good starting point.http://www.qgisworkshop.org/html/workshop/plugins_tutorial.html#implement-map-canvas-click-action
Github Repo: https://github.com/cugos/qgisworkshop_org
The code is copied from http://www.qgisworkshop.org/html/_static/activate_click_1.py
#This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
#http://creativecommons.org/licenses/by-sa/3.0/
#Feel free to use this material, but we ask that you please retain attribution as per the license
#Copyright (C) 2011 CUGOS
#Copyright (C) 2011 Aaron Racicot
#Copyright (C) 2011 Greg Corradini
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from vector_selectbypointdialog import vector_selectbypointDialog
class vector_selectbypoint:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
# refernce to map canvas
self.canvas = self.iface.mapCanvas()
# out click tool will emit a QgsPoint on every click
self.clickTool = QgsMapToolEmitPoint(self.canvas)
# create our GUI dialog
self.dlg = vector_selectbypointDialog()
def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(QIcon(":/plugins/vector_selectbypoint/icon.png"), \
"some text that appears in the menu", self.iface.mainWindow())
# connect the action to the run method
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu("&some text that appears in the menu", self.action)
# connect our custom function to a clickTool signal that the canvas was clicked
result = QObject.connect(self.clickTool, SIGNAL("canvasClicked(const QgsPoint &, Qt::MouseButton)"), self.handleMouseDown)
#QMessageBox.information( self.iface.mainWindow(),"Info", "connect = %s"%str(result) )
def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu("&some text that appears in the menu",self.action)
self.iface.removeToolBarIcon(self.action)
def handleMouseDown(self, point, button):
self.dlg.clearTextBrowser()
self.dlg.setTextBrowser( str(point.x()) + " , " +str(point.y()) )
#QMessageBox.information( self.iface.mainWindow(),"Info", "X,Y = %s,%s" % (str(point.x()),str(point.y())) )
# run method that performs all the real work
def run(self):
# make our clickTool the tool that we'll use for now
self.canvas.setMapTool(self.clickTool)
# show the dialog
self.dlg.show()
result = self.dlg.exec_()
# See if OK was pressed
if result == 1:
# do something useful (delete the line containing pass and
# substitute with your code
pass
The following code returns a list of XY Coordinates of the region in a map canvas where clicked.
from qgis.gui import QgsMapToolEmitPoint
class PrintClickedPoint(QgsMapToolEmitPoint):
def __init__(self, canvas):
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
def canvasPressEvent( self, e ):
point = self.toMapCoordinates(self.canvas.mouseLastXY())
point = list(point)
print point
canvas_clicked = PrintClickedPoint( iface.mapCanvas() )
iface.mapCanvas().setMapTool( canvas_clicked )