[GIS] How to get the map canvas CRS in EPSG code

pyqgisqgis

This is probably a simple request but I cannot find a way around it:

how can I read from python the map canvas CRS?

I know how to set it up:

canvas = self.iface.mapCanvas()
canvas.mapRenderer().setProjectionsEnabled(True)
canvas.mapRenderer().setDestinationCrs(QgsCoordinateReferenceSystem(EPSG_CODE))

But I want to do that ONLY if I need it:

canvas = self.iface.mapCanvas()
canCRS = (????)
wgsCRS = QgsCoordinateReferenceSystem(4326)
bBox = canvas.extent()

if canCRS == wgsCRS:
  # get get lat long of canvas extent
  lon = bBox.xMinimum()
  lat = bBox.yMinimum()
  lonMax = bBox.xMaximum()
  latMax = bBox.yMaximum()

  return lon, lat, lonMax, latMax

else:
  # set map canvas CRS and get lat long of the extent
  canvas.mapRenderer().setProjectionsEnabled(True)
  canvas.mapRenderer().setDestinationCrs(wgsCRS)
  lon = bBox.xMinimum()
  lat = bBox.yMinimum()
  lonMax = bBox.xMaximum()
  latMax = bBox.yMaximum()

  return lon, lat, lonMax, latMax

How do I get the map canvas CRS in EPSG code so I can compare it with QgsCoordinateReferenceSystem(4326)? Which method should I use?

Best Answer

In QGIS3 API changed:

iface.mapCanvas().mapSettings().destinationCrs().authid()
'EPSG:3045'
Related Question