2

Is there any particular way of getting the current zoom level from the map canvas through python code in QGIS environment? I want to convert coordinates of the cursor and the current zoom level into Google Map Tiles coordinates.

  • 1
    If you mean scale, you could try iface.mapCanvas().scale(). – Joseph Jan 23 '18 at 10:15
  • Thanks for your comment but is there any way I can convert scale to zoom levels? I need to put zoom levels in my URL to request tiles of the specific regions at specific zoom levels. – raosaeedali Jan 23 '18 at 10:23
5

Try using this:

import math
scale=iface.mapCanvas().scale()
dpi=iface.mainWindow().physicalDpiX()
maxScalePerPixel = 156543.04
inchesPerMeter = 39.37
zoomlevel = int(round(math.log( ((dpi* inchesPerMeter * maxScalePerPixel) / scale), 2 ), 0))
print zoomlevel

Original code : https://github.com/datalyze-solutions/TileMapScaleLevels/blob/master/tilemapscalelevels.py

1

Just install tile layer plugin then add an XYZ or TMS reference to the map with the plugin you will get zoom column row information https://github.com/minorua/TileLayerPlugin/blob/master/README.md

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.