Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active August 29, 2015 14:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save perrygeo/5f9104f682aa31ae9553 to your computer and use it in GitHub Desktop.
Calculate size of pixel on a mercator xyz tile
import math
def pixel_size(z, lat, circ=40075160):
"""
Returns the size of a pixel (units same as circumference)
Parameters:
z: zoom level
lat: latitude in degrees
circ: circumference (default = 40075160 meters)
Notes:
spheroid calculation may be off slightly from true ellipsoid
"""
return circ * math.cos(math.radians(lat)) / (2 ** (z + 8))
if __name__ == "__main__":
assert round(pixel_size(z=5, lat=0)) == 4892
assert round(pixel_size(z=5, lat=45)) == 3459
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment