2

With a plugin I can click a button and activate a QgsMapTool, defined as:

class PointTool(QgsMapTool):

def __init__(self, canvas):
    QgsMapTool.__init__(self, canvas)
    self.canvas = canvas    

def canvasReleaseEvent(self, event):
    #Get the click
    x = event.pos().x()
    y = event.pos().y()

    point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
    coordinates = str(point).replace('(','').replace(')','')
    coord_x = float(coordinates.split(',')[0])
    coord_y = float(coordinates.split(',')[1])

    QgsMessageLog.logMessage(str('{0:.1f}'.format(coord_x)) + ' ' + str('{0:.1f}'.format(coord_y)),'coords')

It shows me the coordinates in a tab 'coords' of the Log Messages Panel.

The main function I use is within the class class mask_n_functions(QDialog, Ui_InputMask):. And there I call the QgsMapTool as follows:

self.xy.clicked.connect(self.setToolActive)

def setToolActive(self):
    tool = PointTool(iface.mapCanvas())
    iface.mapCanvas().setMapTool(tool)

Now I want to fill two QLineEdit in class mask_n_functions(QDialog, Ui_InputMask): with coord_x and coord_y of class PointTool(QgsMapTool)).

Where do I have to call self.lblx.setText(coord_x)?

Besides, the coordinates from canvas are rounded:

x: 620832.96 -> 620833.0

y: 5713645.90 -> 5713650.0 !!! Why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.