7

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?

9

Try:

canvas.mapRenderer().destinationCrs().authid()

That will return the map canvases' current crs as an epsg code

  • Yes, thanks @ndawson, that did it! The only thing that needs to be change is the comparison in canCRS == wgsCRS..authid() – mic May 20 '14 at 13:39
3

In QGIS3 API changed:

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.