Skip to main content
1 of 2
Devenepali
  • 365
  • 2
  • 10

How to get X,Y coordinate with mouse click on the vertex of line feature with PyQGIS

How to get X,Y coordinate with mouse click with enabling snapping so that only line's vertex point coordinates will be selected.

By using following code i can get latitude longitude of clicked position in map canvas of QGIS but cannot enable the snapping option .

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)
        iface.mapCanvas().snappingUtils().toggleEnabled()
        print(point)

canvas_clicked = PrintClickedPoint( iface.mapCanvas() )
iface.mapCanvas().setMapTool( canvas_clicked )
Devenepali
  • 365
  • 2
  • 10