10

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'd like to obtain 4 numbers: xMin, xMax, yMin, yMax

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
Ben Mayo
  • 955
  • 1
  • 11
  • 28

1 Answers1

16

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()
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • Just out of curiosity. Would it be just as simple to get the coordinates of a click? – BritishSteel May 06 '15 at 20:04
  • 1
    @BritishSteel I think you'd need to subclass the QgsMapTool class to be able to listen to canvasPressEvent. See http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/canvas.html#writing-custom-map-tools for an example. – Germán Carrillo May 06 '15 at 20:23