Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created June 13, 2020 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/dde20c29fd5863e4fe84d785698c4d0c to your computer and use it in GitHub Desktop.
Save ThomasG77/dde20c29fd5863e4fe84d785698c4d0c to your computer and use it in GitHub Desktop.
Python functions to convert from/to zoom level to scale (assuming 96 dpi and latitude 0)
from math import cos, log, pi
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment