Sunday 20 November 2016

Getting co-ordinates of current map canvas using PyQGIS?


I'm trying get the extents of the current map canvas. CRS is 22700 BNG.


My code:


canvas = iface.mapCanvas()

print canvas

vLayer = iface.activeLayer()
extents = vLayer.extent()

print extents

Which returns



qgis._gui.QgsMapCanvas object at 0x1198c6a68



qgis._gui.QgsMapCanvas object at 0x1198c6a68



I'm want 4 numbers: xMin, xMax, yMin, yMax



Answer



You can get the current map canvas' extent by running this line of code in the QGIS Python Console:


iface.mapCanvas().extent().toString()

In case that you want the extent of the active layer (as your code suggests), run this:


iface.activeLayer().extent().toString()


You could also get the individual numbers like this:


e = iface.mapCanvas().extent()
e.xMaximum()
e.yMaximum()
e.xMinimum()
e.yMinimum()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...