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.

2
  • 1
    If you mean scale, you could try iface.mapCanvas().scale().
    – Joseph
    Commented Jan 23, 2018 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. Commented Jan 23, 2018 at 10:23

3 Answers 3

7

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

0
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

0

You may also need to take account of the "tilePixelRatio". Not all retrieved images will be 256 x 256 px. Some may be 512 x 512 px with tilePixelRatio=2 and will thus have a different zoom value. I'm not sure that QGIS takes this into account anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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