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.
3 Answers
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
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
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.
iface.mapCanvas().scale()
.