Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active February 25, 2025 22:11
Add this script in startup.py to now see zoom for EPSG 3857 when the canevas is using it
from math import cos, log, pi
from qgis.utils import iface
from qgis.PyQt.QtWidgets import QLabel
# Not needed here but could be useful later
# def getScale(zoom_level, screen_dpi=96, latitude=0):
# resolution = (6378137.0 * 2 * pi / 256) * cos(latitude) / (2**zoom_level)
# return (screen_dpi * 1/0.0254 * resolution)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment