source: subversion/applications/routing/pyroute-dev/tilenames.py @ 18454

Last change on this file since 18454 was 18454, checked in by buerste, 10 years ago

-further updates of spaces to tabs

  • Property svn:keywords set to Rev
File size: 2.6 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""Translates between lat/long and the slippy-map tile
5numbering scheme
6
7http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames
8"""
9
10__version__ = "$Rev: 18454 $"
11__license__ = """This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program. If not, see <http://www.gnu.org/licenses/>."""
23_debug = 0
24
25
26from math import *
27
28def numTiles(z):
29        return(pow(2,z))
30
31def sec(x):
32        return(1/cos(x))
33
34def latlon2relativeXY(lat,lon):
35        x = (lon + 180) / 360
36        y = (1 - log(tan(radians(lat)) + sec(radians(lat))) / pi) / 2
37        return(x,y)
38
39def latlon2xy(lat,lon,z):
40        n = numTiles(z)
41        x,y = latlon2relativeXY(lat,lon)
42        return(n*x, n*y)
43
44def tileXY(lat, lon, z):
45        x,y = latlon2xy(lat,lon,z)
46        return(int(x),int(y))
47
48def xy2latlon(x,y,z):
49        n = numTiles(z)
50        relY = y / n
51        lat = mercatorToLat(pi * (1 - 2 * relY))
52        lon = -180.0 + 360.0 * x / n
53        return(lat,lon)
54
55def latEdges(y,z):
56        n = numTiles(z)
57        unit = 1 / n
58        relY1 = y * unit
59        relY2 = relY1 + unit
60        lat1 = mercatorToLat(pi * (1 - 2 * relY1))
61        lat2 = mercatorToLat(pi * (1 - 2 * relY2))
62        return(lat1,lat2)
63
64def lonEdges(x,z):
65        n = numTiles(z)
66        unit = 360 / n
67        lon1 = -180 + x * unit
68        lon2 = lon1 + unit
69        return(lon1,lon2)
70
71def tileEdges(x,y,z):
72        lat1,lat2 = latEdges(y,z)
73        lon1,lon2 = lonEdges(x,z)
74        return((lat2, lon1, lat1, lon2)) # S,W,N,E
75
76def mercatorToLat(mercatorY):
77        return(degrees(atan(sinh(mercatorY))))
78
79def tileSizePixels():
80        return(256)
81
82def tileLayerExt(layer):
83        if(layer in ('oam')):
84                return('jpg')
85        return('png')
86
87def tileLayerBase(layer):
88        layers = { \
89                "tah": "http://tah.openstreetmap.org/Tiles/tile/",
90                "oam": "http://oam1.hypercube.telascience.org/tiles/1.0.0/openaerialmap-900913/",
91                "mapnik": "http://tile.openstreetmap.org/mapnik/"
92                }
93        return(layers[layer])
94
95def tileURL(x,y,z,layer):
96        return "%s%d/%d/%d.%s" % (tileLayerBase(layer),z,x,y,tileLayerExt(layer))
97
98if __name__ == "__main__":
99        for z in range(0,17):
100                x,y = tileXY(51.50610, -0.119888, z)
101
102                s,w,n,e = tileEdges(x,y,z)
103                print "%d: %d,%d --> %1.3f :: %1.3f, %1.3f :: %1.3f" % (z,x,y,s,n,w,e)
104                #print "<img src='%s'><br>" % tileURL(x,y,z)
Note: See TracBrowser for help on using the repository browser.