pyqgis-add-zoom-levels-3857.py
from math import cos, log, pi
from qgis.utils import iface
from qgis.PyQt.QtWidgets import QLabel
def getZoomLevel(scale, screen_dpi=96, latitude=0):
resolution = scale / (screen_dpi * 1/0.0254)
zoom_level = ((6378137.0 * 2 * pi / 256) * cos(latitude)) / resolution
return log(zoom_level)/log(2)
canvas = iface.mapCanvas()
labelZoomNumberEpsg3857 = QLabel()
iface.mainWindow().statusBar().addPermanentWidget(labelZoomNumberEpsg3857)
def changeZoomNumberOsm(scale):
rounded_zoom_level = round(getZoomLevel(scale), 2)
labelZoomNumberEpsg3857.setText("Zoom level : {}".format(str(rounded_zoom_level)))
canvas.scaleChanged.connect(changeZoomNumberOsm)
def showHideZoomLevel():
if canvas.mapSettings().destinationCrs().authid() == "EPSG:3857":
labelZoomNumberEpsg3857.show()
else:
labelZoomNumberEpsg3857.hide()
canvas.destinationCrsChanged.connect(showHideZoomLevel)