source: subversion/applications/routing/pyroute/tilenames.py @ 5361

Last change on this file since 5361 was 5315, checked in by ojw, 12 years ago

Python library for slippy-map tilenames

File size: 1.6 KB
Line 
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#-------------------------------------------------------
11from math import *
12
13def numTiles(z):
14  return(pow(2,z))
15
16def sec(x):
17  return(1/cos(x))
18
19def latlon2relativeXY(lat,lon):
20  x = (lon + 180) / 360
21  y = (1 - log(tan(radians(lat)) + sec(radians(lat))) / pi) / 2
22  return(x,y)
23
24def latlon2xy(lat,lon,z):
25  n = numTiles(z)
26  x,y = latlon2relativeXY(lat,lon)
27  return(n*x, n*y)
28 
29def tileXY(lat, lon, z):
30  x,y = latlon2xy(lat,lon,z)
31  return(int(x),int(y))
32
33def latEdges(y,z):
34  n = numTiles(z)
35  unit = 1 / n
36  relY1 = y * unit
37  relY2 = relY1 + unit
38  lat1 = mercatorToLat(pi * (1 - 2 * relY1))
39  lat2 = mercatorToLat(pi * (1 - 2 * relY2))
40  return(lat1,lat2)
41
42def lonEdges(x,z):
43  n = numTiles(z)
44  unit = 360 / n
45  lon1 = -180 + x * unit
46  lon2 = lon1 + unit
47  return(lon1,lon2)
48 
49def tileEdges(x,y,z):
50  lat1,lat2 = latEdges(y,z)
51  lon1,lon2 = lonEdges(x,z)
52  return((lat2, lon1, lat1, lon2)) # S,W,N,E
53
54def mercatorToLat(mercatorY):
55  return(degrees(atan(sinh(mercatorY))))
56
57def tileURL(x,y,z):
58  query = "%d/%d/%d.png" % (z,x,y)
59  return("http://dev.openstreetmap.org/~ojw/Tiles/tile.php/" + query)
60
61if __name__ == "__main__":
62  for z in range(0,17):
63    x,y = tileXY(51.50610, -0.119888, z)
64   
65    s,w,n,e = tileEdges(x,y,z)
66    print "%d: %d,%d --> %1.3f :: %1.3f, %1.3f :: %1.3f" % (z,x,y,s,n,w,e)
67    #print "<img src='%s'><br>" % tileURL(x,y,z)
Note: See TracBrowser for help on using the repository browser.