forked from vvoovv/tile-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing_aerial.py
87 lines (79 loc) · 2.6 KB
/
bing_aerial.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
#!/usr/bin/env python
import sys, string, urllib2, math, logging
import util
from base import Base
digs = string.digits + string.lowercase
def int2base(x, base):
if x < 0: sign = -1
elif x==0: return '0'
else: sign = 1
x *= sign
digits = []
while x:
digits.append(digs[x % base])
x /= base
if sign < 0:
digits.append('-')
digits.reverse()
return ''.join(digits)
class BingAerial(Base):
metadataPath = "http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial"
bingMapsKey = ""
imageUrl = ""
imageUrlSubdomains = None
zoomMax = 21 # max zoom for the whole world
def __init__(self):
# reading bing maps key
self.bingMapsKey = open("bing_maps_key.txt", "r").read()
self.getMetadata()
def getMetadata(self):
response = util.fetchJson("%s?key=%s" % (self.metadataPath, self.bingMapsKey))
if "errorDetails" not in response:
data = response["resourceSets"][0]["resources"][0]
self.tileWidth = data["imageWidth"]
self.tileHeight = data["imageHeight"]
self.imageUrl = data["imageUrl"]
self.imageUrlSubdomains = data["imageUrlSubdomains"]
self.zoomMax = data["zoomMax"]
self.numSubdomains = len(self.imageUrlSubdomains)
else:
logging.error("Unknown response from the server")
sys.exit(-1)
def getTileUrl(self, zoom, x, y, tileCounter):
# calculating quadkey
interleaved = [None]*2*zoom
interleaved[::2] = bin(y)[2:].zfill(zoom)
interleaved[1::2] = bin(x)[2:].zfill(zoom)
interleaved = ''.join(interleaved)
# converting to decimal
interleaved = int(interleaved, 2)
# converting to base 4
quadkey = int2base(interleaved, 4).zfill(zoom)
url = self.imageUrl.replace("{subdomain}", self.imageUrlSubdomains[tileCounter % self.numSubdomains])
url = url.replace("{quadkey}", quadkey)
return url
def getMaxZoom(self, bbox):
(bottom, left) = util.toSphMercator(bbox[1], bbox[0])
(top, right) = util.toSphMercator(bbox[3], bbox[2])
(centerY, centerX) = util.toGeographic((top+bottom)/2, (left+right)/2)
zoom = self.zoomMax
while True:
response = util.fetchJson("%s/%s,%s?zl=%s&key=%s" % (self.metadataPath, centerY, centerX, zoom, self.bingMapsKey))
if "errorDetails" not in response:
data = response["resourceSets"][0]["resources"][0]
if data["vintageEnd"]: break
else:
logging.error("Unknown response from the server")
sys.exit(-1)
zoom = zoom-1
return zoom
def getTileImage(self, zoom, x, y, counter):
url = self.getTileUrl(zoom, x, y, counter)
try:
tile = urllib2.urlopen(url).read()
except Exception, e:
# Something went wrong
logging.error(e)
logging.error("Unable to download image %s" % url)
sys.exit(-1)
return tile