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

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

add support for openaerialmap (satellite photos) even though they're
weird JPEG thingies that cairo can't read :(

File size: 1.9 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 tileLayerExt(layer):
58  if(layer in ('oam')):
59    return('jpg')
60  return('png')
61
62def tileLayerBase(layer):
63  layers = { \
64    "tah": "http://dev.openstreetmap.org/~ojw/Tiles/tile.php/",
65    "oam": "http://oam1.hypercube.telascience.org/tiles/1.0.0/openaerialmap-900913/",
66    "mapnik": "http://tile.openstreetmap.org/mapnik/"
67    }
68  return(layers[layer])
69 
70def tileURL(x,y,z,layer):
71  return "%s%d/%d/%d.%s" % (tileLayerBase(layer),z,x,y,tileLayerExt(layer))
72
73if __name__ == "__main__":
74  for z in range(0,17):
75    x,y = tileXY(51.50610, -0.119888, z)
76   
77    s,w,n,e = tileEdges(x,y,z)
78    print "%d: %d,%d --> %1.3f :: %1.3f, %1.3f :: %1.3f" % (z,x,y,s,n,w,e)
79    #print "<img src='%s'><br>" % tileURL(x,y,z)
Note: See TracBrowser for help on using the repository browser.