This repository has been archived by the owner on Aug 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileServer.py
350 lines (252 loc) · 9.97 KB
/
TileServer.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#Copyright 2007 Razvan Taranu
#
#This file is part of PyMapper.
#
#PyMapper is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#PyMapper is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with PyMapper. If not, see <http://www.gnu.org/licenses/>.
#
#PyMapper uses code from maemo-mapper
from StringIO import StringIO
import pygame, urllib, threading, random, sqlite3
#PROXIES = {'http': 'http://fastweb.bell.ca:8083/'}
PROXIES = {}
DB_PATH = '/media/mmc1/gmaps.db'
class TileCache:
def __init__(self):
# tile returned when there is no cached data
self.blackTile = pygame.Surface((256, 256))
self.server = TileServer(self)
self.server.setDaemon(True)
self.server.start()
# decoding the png files is expensive, keep a fixed size cache
self.lock = threading.Lock() # synchronizes cache
self.cacheInserts = 0
self.items = {}
self.lru = []
self.maxSize = 20
def shutdown(self):
self.server.shutdown()
self.server.join()
def fetchTile(self, uid):
# make sure the tile is within bounds
# zoom 17 = 1 tile, zoom 16 = 2 tiles, zoom 15 = 4 tiles, etc
x, y, zoom = uid
maxTile = (1 << (17 - zoom)) - 1
if x < 0 or y < 0 or x > maxTile or y > maxTile:
return self.blackTile
# try the cache
self.lock.acquire()
if self.items.has_key(uid):
surface = self.items[uid]
# in case we have a placeholder item, return the blackTile
if surface == None:
surface = self.blackTile
self.lru.remove(uid)
else:
surface = self.blackTile
# request async data from the database
self.server.requestData(uid)
# placeholder data till the asyc request returns
self.items[uid] = None
self.lru.append(uid)
if len(self.lru) > self.maxSize:
old = self.lru.pop(0)
del(self.items[old])
self.lock.release()
return surface
def getTiles(self, uidList):
surfaceList = []
for uid in uidList:
surface = self.fetchTile(uid)
surfaceList.append(surface)
return surfaceList
def putData(self, uid, data):
self.lock.acquire()
# figure out if this data is still fresh; sometimes async requests
# take too long to come back, so the data we insert is 'obsolete'
obsolete = not self.items.has_key(uid)
self.lock.release()
if obsolete: return
if len(data) > 0:
fh = StringIO(data)
surface = pygame.image.load(fh, 'mt.png')
fh.close()
else:
surface = self.blackTile
self.lock.acquire()
# the tile might have become obsolete while we were loading
if self.items.has_key(uid):
self.items[uid] = surface
self.cacheInserts += 1
self.lock.release()
def hasTiles(self):
self.lock.acquire()
hasTiles = self.cacheInserts > 0
self.cacheInserts = 0
self.lock.release()
return hasTiles
class TileServer(threading.Thread):
def __init__(self, parent):
threading.Thread.__init__(self)
self.parent = parent
self.insertCount = 0 # used to know when to commit
self.maxInserts = 40 # after how many inserts do we commit
# define and start the worker threads, these are used to download tiles
self.maxWorkers = 2
self.nextWorker = 0 # worker who gets the next request
self.workers = []
for n in range(self.maxWorkers):
worker = Worker(self)
worker.setDaemon(True)
worker.start()
self.workers.append(worker)
# define the "buckets" used for async requests
self.lock = threading.Condition()
self.requests = [] # incoming from TileServer
self.pending = [] # outgoing to Worker
self.results = {} # incoming from Worker
self.done = False
def __putData(self, uid, data):
x, y, zoom = uid
params = (x, y, zoom, data)
self.db.execute('INSERT INTO tiles VALUES(?, ?, ?, ?)', params)
self.insertCount = (self.insertCount + 1) % self.maxInserts
if self.insertCount == 0:
self.db.commit()
def __getData(self, uid):
cur = self.db.execute(
"""
SELECT data
FROM tiles
WHERE
x = ?
AND y = ?
AND zoom = ?
""", uid)
result = cur.fetchone()
cur.close()
if result:
data = result[0]
else:
data = None
return data
def run(self):
# define the sqlite connection
self.db = sqlite3.connect(DB_PATH)
# create the data tables and indexes if it is not found
self.db.execute(
"""
CREATE TABLE IF NOT EXISTS tiles (
x INTEGER,
y INTEGER,
zoom INTEGER,
data BLOB)
""")
self.db.execute(
"""
CREATE UNIQUE INDEX IF NOT EXISTS tiles_index
ON tiles (x, y, zoom)
""")
self.db.commit()
self.done = False
while not self.done:
self.lock.acquire()
if not self.requests and not self.results:
self.lock.wait()
requests = self.requests
self.requests = []
results = self.results
self.results = {}
self.lock.release()
# store incoming data from Worker threads
for uid, data in results.items():
if data == None:
pass # TODO: retry the download
else:
# if len(data) >= 0
self.__putData(uid, data)
self.parent.putData(uid, data)
self.pending.remove(uid)
# fetch data for requests from TileServer (database or url)
for uid in requests:
# make sure no async url request is pending for this item
if uid not in self.pending:
data = self.__getData(uid)
if data == None:
# start async url request
self.nextWorker = (self.nextWorker + 1) % self.maxWorkers
self.workers[self.nextWorker].requestData(uid)
self.pending.append(uid)
else:
self.parent.putData(uid, data)
self.db.commit()
self.db.close()
def shutdown(self):
self.lock.acquire()
self.done = True
self.lock.notify()
self.lock.release()
def requestData(self, uid):
self.lock.acquire()
if uid not in self.requests:
self.requests.append(uid)
self.lock.notify()
self.lock.release()
def putData(self, uid, data):
"""Saves data into the database (async)
if len(data) > 0
good data
elif len(data) == 0
bad data (404)
elif data == None
error (timeout)
"""
self.lock.acquire()
self.results[uid] = data
self.lock.notify()
self.lock.release()
class Worker(threading.Thread):
def __init__(self, parent):
threading.Thread.__init__(self)
self.parent = parent
self.lock = threading.Condition() # manages self.requests
self.requests = [] # used as a stack
def run(self):
while True:
# wait for new requests to come from the TileServer
self.lock.acquire()
if not self.requests:
self.lock.wait()
uid = self.requests.pop()
self.lock.release()
url = "http://mt.google.com/mt?x=%s&y=%s&zoom=%s" % uid
# fetch data for the request
data = None
try:
urlh = urllib.urlopen(url, proxies=PROXIES)
# google does not store tiles for all the zoom levels
# sometimes they will send a soft 404 text/html page
if urlh.headers.type == 'image/png':
data = buffer(urlh.read())
else:
data = ''
urlh.close()
except:
print 'Could not download %s' % url
# send the data back to the TileServer
self.parent.putData(uid, data)
def requestData(self, uid):
self.lock.acquire()
self.requests.append(uid)
self.lock.notify()
self.lock.release()