Skip to content

Instantly share code, notes, and snippets.

@thomersch
Created February 12, 2014 19:25
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 thomersch/8962703 to your computer and use it in GitHub Desktop.
Save thomersch/8962703 to your computer and use it in GitHub Desktop.
Convert lat/lon to tile number
import math
def lat_lon_to_tile(lat_deg, lon_deg, zoom):
def sec(x):
return 1/math.cos(x)
lat_rad = math.radians(lat_deg)
n = 2**zoom
xtile = n * ((lon_deg + 180) / 360)
ytile = n * (1 - (math.log(math.tan(lat_rad) + sec(lat_rad)) / math.pi)) / 2
return int(xtile), int(ytile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment