6

I am working on a small QGIS Python plugin and want to get the X/Y coordinates from a user click on the canvas. I have a small UI called from button in a toolbar. After filling the UI with information I would like to get the XY coordinates. I am using QGIS 3 with Python 3 and QT5.

I tried to use the steps from How to get co ordinates of points on mouse click in PyQGIS but when running it jumps right to the end without calling the display_point function and there is no reaction to mouse clicks. Are there any changes to the current QGIS version?

My code, which is inside the plugin class and called by a button from the toolbar without UI.

def run2(self):
    # a reference to our map canvas 
    canvas = iface.mapCanvas() 

    # this QGIS tool emits as QgsPoint after each click on the map canvas
    pointTool = QgsMapToolEmitPoint(canvas)
    # Checkpoint
    print("S 1")
    pointTool.canvasClicked.connect( self.display_point )
    canvas.setMapTool( pointTool )
    # Checkpoint
    print("S 3")

def display_point( pointTool ): 

    print('({:.4f}, {:.4f})'.format(pointTool[0], pointTool[1]))
    # Checkpoint
    print("S 2")
  • I'll try the class suggestion from : How to get co ordinates of points on mouse click in PyQGIS above in a short plug in to see how it works. – Cary H Dec 17 '18 at 21:39
3

In a plugin (displayed "Always On Top"), with PyQGIS 3, to avoid it jumps right to the end without calling the display_point function you need following changes in your code:

.
.
.
from PyQt5.QtCore import Qt

from qgis.gui import QgsMapToolEmitPoint
.
.
.
    def __init__(self, iface):
        """Constructor.
.
.
.
        # Create the dialog (after translation) and keep reference
        self.dlg = WhereAmIDialog()
        self.dlg.setWindowFlags(Qt.WindowStaysOnTopHint)
.
.
.
    def initGui(self):
        """Create the menu entries and toolbar icons inside the QGIS GUI."""
.
.
.       
        result = self.pointTool.canvasClicked.connect(self.display_point)
.
.
.
    def display_point(self, point, button):  
        # report map coordinates from a canvas click
        coords = "{}, {}".format(point.x(), point.y())
        self.dlg.lineEdit.setText(str(coords)) 
        self.dlg.show()
.
.
.
    def run(self):
        """Run method that performs all the real work"""
        # make our clickTool the tool that we'll use for now    
        self.canvas.setMapTool(self.pointTool)
.
.
.

It works; as it can be observed at following image:

enter image description here

  • Thanks for your reply. The "point.x()" is the important change as "pointTool[0]" results in an index error. – Firefly Dec 18 '18 at 18:24
0

I think you are close: :)

I did copy and paste the code from the link to xunilk's answer. It worked fine in the Plugins/python console.

If you are using the plugin you may have to include 'self' in the method definition. Also, the pointTool.canvasClicked.connect( self.display_point ) has to be somewhere that is "ran" by the main method. Does "S 1" ever get printed?

from qgis.gui import QgsMapToolEmitPoint

def display_point( pointTool ): 

    print ('{:.4f}, {:.4f})'.format(pointTool[0], pointTool[1]))

# a reference to our map canvas 
canvas = iface.mapCanvas() 

# this QGIS tool emits as QgsPoint after each click on the map canvas
pointTool = QgsMapToolEmitPoint(canvas)

pointTool.canvasClicked.connect( display_point )
  • Thanks for your reply. I will try your solution. "S 1" and "S 3" are shown, "S 3" not. – Firefly Dec 18 '18 at 8:08

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.