-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.py
44 lines (37 loc) · 1.01 KB
/
util.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
#!/usr/bin/env python
import math, urllib2, json
earthRadius = 6378137
def toSphMercator(lat, lon):
return (
earthRadius * math.log(math.tan(math.pi/4 + lat*math.pi/360)),
earthRadius * lon * math.pi / 180
)
def toGeographic(y, x):
return (
360/math.pi * math.atan(math.exp(y/earthRadius)) - 90,
180*x/(math.pi*earthRadius)
)
def toTileCoords(lat, lon, zoom):
halfEquator = math.pi * earthRadius
equator = 2 * halfEquator
numTiles = math.pow(2, zoom)
(lat, lon) = toSphMercator(lat, lon)
# moving zero to the top left corner
lat = halfEquator - lat
lon = lon + halfEquator
y = lat * numTiles / equator
x = lon * numTiles / equator
return (int(math.floor(y)), int(math.floor(x)))
def fetchJson(url):
try:
return json.loads(
urllib2.urlopen(url).read()
)
except urllib2.URLError as e:
if hasattr(e, "reason"):
logging.error("Failed to reach the server: %s" % e.reason)
elif hasattr(e, "code"):
logging.error("Server error: %s" % e.code)
sys.exit(-1)
except:
logging.error("JSON error")