3

I'm trying to build a plugin that firstly captures the map coordinates.

I have tried to adapt the map tool explained in this post: How to programatically check for a mouse click in QGIS

The code I used for the main function is below:

def canvasReleaseEvent(self, event):
 #Get the click
 x = event.pos().x()
 y = event.pos().y()
 canvas = self.canvas
 canvas.mapRenderer().setProjectionsEnabled(True)
 canvas.mapRenderer().setDestinationCrs(QgsCoordinateReferenceSystem(27700))
 espg = canvas.mapRenderer().destinationCrs().authid()
 point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)

Unfortunately the when I print point it always returns as (0,0). I have tried various ways to set the crs of the points but none work. What needs to be done to make it work?

asked Aug 9, 2014 at 10:43
5
  • Does print on x,y return anything? Commented Aug 9, 2014 at 11:11
  • 2
    Also don't do setProjectionsEnabled or setDestinationCrs. That is changing the canvas itself which might not be what you want. Use QgsCoordinateTransform if you need to transform from one to the other, or just toMapCoordinates is enough to go from click to canvas coordinates Commented Aug 9, 2014 at 11:14
  • Thanks @Nathan W - I was just being explicit with the crs to make sure it wasn't that. Commented Aug 9, 2014 at 11:25
  • print x, print y does return values. These aren't recognisable to me though. All are three digit couplets like 356,172? Commented Aug 9, 2014 at 11:31
  • Yeah they are the screen coordinates. Remove the other lines and just have the toMapCoordinates stuff. Commented Aug 9, 2014 at 12:09

1 Answer 1

0

This should do it:

class PointTool(QgsMapToolEmitPoint):
def __init__(self, canvas):
 QgsMapToolEmitPoint.__init__(self, canvas)
def canvasReleaseEvent(self, mouseEvent):
 qgsPoint = self.toMapCoordinates(mouseEvent.pos())
 print('x:', qgsPoint.x(), ', y:', qgsPoint.y())

As your question is possibly a duplicate to this post.

answered Jun 14, 2016 at 7:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.