-
Notifications
You must be signed in to change notification settings - Fork 3
/
tools.py
89 lines (73 loc) · 2.24 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# Translates between lat/long and the slippy-map tile numbering scheme
#
# http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames
# https://svn.openstreetmap.org/applications/routing/pyroute/tilenames.py
#
# Written by Oliver White, 2007
# This file is public-domain
#-------------------------------------------------------
from math import *
def numTiles(z):
return(pow(2,z))
def sec(x):
return(1/cos(x))
def latlon2relativeXY(lat,lon):
x = (lon + 180) / 360
y = (1 - log(tan(radians(lat)) + sec(radians(lat))) / pi) / 2
return(x,y)
def latlon2xy(lat,lon,z):
n = numTiles(z)
x,y = latlon2relativeXY(lat,lon)
return(n*x, n*y)
def tileXY(lat, lon, z):
x,y = latlon2xy(lat,lon,z)
return(int(x),int(y))
def xy2latlon(x,y,z):
n = numTiles(z)
relY = y / n
lat = mercatorToLat(pi * (1 - 2 * relY))
lon = -180.0 + 360.0 * x / n
return(lat,lon)
def latEdges(y,z):
n = numTiles(z)
unit = 1 / n
relY1 = y * unit
relY2 = relY1 + unit
lat1 = mercatorToLat(pi * (1 - 2 * relY1))
lat2 = mercatorToLat(pi * (1 - 2 * relY2))
return(lat1,lat2)
def lonEdges(x,z):
n = numTiles(z)
unit = 360 / n
lon1 = -180 + x * unit
lon2 = lon1 + unit
return(lon1,lon2)
def tileEdges(x,y,z):
lat1,lat2 = latEdges(y,z)
lon1,lon2 = lonEdges(x,z)
return((lat2, lon1, lat1, lon2)) # S,W,N,E
def mercatorToLat(mercatorY):
return(degrees(atan(sinh(mercatorY))))
def tileSizePixels():
return(256)
def tileLayerExt(layer):
if(layer in ('oam')):
return('jpg')
return('png')
def tileLayerBase(layer):
layers = { \
"tah": "http://cassini.toolserver.org:8080/http://a.tile.openstreetmap.org/+http://toolserver.org/~cmarqu/hill/",
#"tah": "http://tah.openstreetmap.org/Tiles/tile/",
"oam": "http://oam1.hypercube.telascience.org/tiles/1.0.0/openaerialmap-900913/",
"mapnik": "http://tile.openstreetmap.org/mapnik/"
}
return(layers[layer])
def tileURL(x,y,z,layer):
return "%s%d/%d/%d.%s" % (tileLayerBase(layer),z,x,y,tileLayerExt(layer))
if __name__ == "__main__":
for z in range(0,17):
x,y = tileXY(37.46476, -122.13786, z)
s,w,n,e = tileEdges(x,y,z)
print("%d: %d,%d --> %1.3f :: %1.3f, %1.3f :: %1.3f" % (z,x,y,s,n,w,e))
#print("<img src='%s'><br>" % tileURL(x,y,z))