Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active February 17, 2023 08:10
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save perrygeo/4478844 to your computer and use it in GitHub Desktop.
Save perrygeo/4478844 to your computer and use it in GitHub Desktop.
Web Mercator Scale/Resolution Calculations
"""
Web Mercator Scale and Resolution Calculations
Python implementation of the formulas at http://msdn.microsoft.com/en-us/library/bb259689.aspx
"""
import math
def meters_per_pixel(zoom, lat):
"""
ground resolution = cos(latitude * pi/180) * earth circumference / map width
"""
return (math.cos(lat * math.pi/180.0) * 2 * math.pi * 6378137) / (256 * 2**zoom)
def map_scale(zoom, lat, dpi=96.0):
"""
map scale = 1 : ground resolution * screen dpi / 0.0254 meters/inch
"""
res = meters_per_pixel(zoom, lat)
return (res * dpi) / 0.0254
if __name__ == "__main__":
tests = [ (5, 45.0),
(12, 0.0),
(12, 79.0)]
for test in tests:
print "Zoom %d at latitude %f => %f meters per pixel (scale is 1:%d at 96 dpi)" % (test[0],
test[1], meters_per_pixel(*test), map_scale(*test))
@perrygeo
Copy link
Author

perrygeo commented Jan 7, 2013

Outputs:

$ python mercator_scale.py 
Zoom 5 at latitude 45.000000 => 3459.145026 meters per pixel (scale is 1:13073933 at 96 dpi)
Zoom 12 at latitude 0.000000 => 38.218514 meters per pixel (scale is 1:144447 at 96 dpi)
Zoom 12 at latitude 79.000000 => 7.292436 meters per pixel (scale is 1:27561 at 96 dpi)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment