| 1 |
#!/usr/bin/env python |
| 2 |
# ------------------------------------------------------- |
| 3 |
# Translates between lat/long and the slippy-map tile |
| 4 |
# numbering scheme |
| 5 |
# |
| 6 |
# http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames |
| 7 |
# |
| 8 |
# Written by Oliver White, 2007 |
| 9 |
# This file is public-domain |
| 10 |
# ------------------------------------------------------- |
| 11 |
from math import atan, degrees, pi |
| 12 |
from math import pow as math_pow |
| 13 |
from math import sinh |
| 14 |
|
| 15 |
|
| 16 |
def num_tiles(z): |
| 17 |
return math_pow(2, z) |
| 18 |
|
| 19 |
|
| 20 |
def lat_edges(y, z): |
| 21 |
n = num_tiles(z) |
| 22 |
unit = 1 / n |
| 23 |
relY1 = y * unit |
| 24 |
relY2 = relY1 + unit |
| 25 |
lat1 = mercator_to_lat(pi * (1 - 2 * relY1)) |
| 26 |
lat2 = mercator_to_lat(pi * (1 - 2 * relY2)) |
| 27 |
return (lat1, lat2) |
| 28 |
|
| 29 |
|
| 30 |
def lon_edges(x, z): |
| 31 |
n = num_tiles(z) |
| 32 |
unit = 360 / n |
| 33 |
lon1 = -180 + x * unit |
| 34 |
lon2 = lon1 + unit |
| 35 |
return (lon1, lon2) |
| 36 |
|
| 37 |
|
| 38 |
def tile_edges(x, y, z): |
| 39 |
lat1, lat2 = lat_edges(y, z) |
| 40 |
lon1, lon2 = lon_edges(x, z) |
| 41 |
return (lon1, lat2, lon2, lat1) # w, s, e, n |
| 42 |
|
| 43 |
|
| 44 |
def mercator_to_lat(mercatorY): |
| 45 |
return degrees(atan(sinh(mercatorY))) |