/usr/share/pyshared/advancedcaching/openstreetmap.py is in agtl 0.8.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2010 Daniel Fett
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Daniel Fett agtl@danielfett.de
# Jabber: fett.daniel@jaber.ccc.de
# Bugtracker and GIT Repository: http://github.com/webhamster/advancedcaching
#
from __future__ import with_statement
import logging
logger = logging.getLogger('openstreetmap')
from os import path, mkdir, extsep, remove
from threading import Semaphore
from urllib import urlretrieve
from socket import setdefaulttimeout
setdefaulttimeout(30)
CONCURRENT_THREADS = 10
def get_tile_loader(prefix, remote_url, max_zoom = 18, reverse_zoom = False, file_type = 'png', size = 256):
class TileLoader():
downloading = {}
semaphore = Semaphore(CONCURRENT_THREADS)
noimage_cantload = None
noimage_loading = None
base_dir = ''
PREFIX = prefix
MAX_ZOOM = max_zoom
FILE_TYPE = file_type
REMOTE_URL = remote_url
TILE_SIZE = size
TPL_LOCAL_PATH = path.join("%s", PREFIX, "%d", "%d")
TPL_LOCAL_FILENAME = path.join("%s", "%%d%s%s" % (extsep, FILE_TYPE))
def __init__(self, id_string, tile, zoom, undersample = False, x = 0, y = 0, callback_draw = None, callback_load = None):
self.id_string = id_string
self.undersample = undersample
self.tile = tile
#self.download_tile = self.gui.ts.check_bounds(*tile)
self.download_tile = tile
if not undersample:
self.download_zoom = zoom
self.display_zoom = zoom
else:
self.download_zoom = zoom - 1
self.display_zoom = zoom
self.download_tile = (int(self.download_tile[0]/2), int(self.download_tile[1]/2))
self.pbuf = None
self.callback_draw = callback_draw
self.callback_load = callback_load
self.my_noimage = None
self.stop = False
self.x = x
self.y = y
# setup paths
self.local_path = self.TPL_LOCAL_PATH % (self.base_dir, self.download_zoom, self.download_tile[0])
self.local_filename = self.TPL_LOCAL_FILENAME % (self.local_path, self.download_tile[1])
self.remote_filename = self.REMOTE_URL % {'zoom': self.download_zoom, 'x' : self.download_tile[0], 'y' : self.download_tile[1]}
def halt(self):
self.stop = True
@staticmethod
def create_recursive(dpath):
if dpath != '/':
if not path.exists(dpath):
head, tail = path.split(dpath)
TileLoader.create_recursive(head)
try:
mkdir(dpath)
except Exception:
# let others fail here.
pass
def run(self):
answer = True
if not path.isfile(self.local_filename):
self.create_recursive(self.local_path)
self.draw(self.get_no_image(self.noimage_loading))
answer = self.__download(self.remote_filename, self.local_filename)
# now the file hopefully exists
if answer == True:
self.load()
self.draw(self.pbuf)
#gobject.idle_add(lambda: self.draw(self.pbuf))
elif answer == False:
#gobject.idle_add(lambda: self.draw(self.get_no_image(self.noimage_cantload)))
self.draw(self.get_no_image(self.noimage_cantload))
else:
# Do nothing here, as the thread was told to stop
pass
def run_again(self):
self.load()
#gobject.idle_add(lambda: self.draw(self.pbuf))
self.draw(self.pbuf)
return False
def get_no_image(self, default):
return (default, None)
'''
if self.my_noimage != None:
return self.my_noimage
size, tile = self.TILE_SIZE, self.tile
# we have no image available. so what do now?
# first, check if we've the "supertile" available (zoomed out)
supertile_zoom = self.download_zoom - 1
supertile_x = int(tile[0]/2)
supertile_y = int(tile[1]/2)
supertile_path = self.TPL_LOCAL_PATH % (self.base_dir, supertile_zoom, supertile_x)
supertile_name = self.TPL_LOCAL_FILENAME % (supertile_path, supertile_y)
#supertile_name = path.join(TileLoader.base_dir, self.PREFIX, str(supertile_zoom), str(supertile_x), "%d%s%s" % (supertile_y, extsep, self.FILE_TYPE))
if not self.undersample and path.exists(supertile_name):
off_x = (tile[0]/2.0 - supertile_x) * size
off_y = (tile[1]/2.0 - supertile_y) * size
#pbuf = gtk.gdk.pixbuf_new_from_file(supertile_name)
#dest = gtk.gdk.Pixbuf(pbuf.get_colorspace(), pbuf.get_has_alpha(), pbuf.get_bits_per_sample(), size, size)
#pbuf.scale(dest, 0, 0, 256, 256, -off_x*2, -off_y*2, 2, 2, gtk.gdk.INTERP_BILINEAR)
self.pbuf = (surface, (off_x, off_y))
self.my_noimage = surface
return dest
else:
self.my_noimage = default
return default
'''
def load(self, tryno=0):
# load the pixbuf to memory
if self.stop:
return True
try:
size, tile = self.TILE_SIZE, self.tile
if self.undersample:
# don't load the tile directly, but load the supertile instead
supertile_x = int(tile[0]/2)
supertile_y = int(tile[1]/2)
off_x = (tile[0]/2.0 - supertile_x) * size
off_y = (tile[1]/2.0 - supertile_y) * size
surface = self.callback_load(self.local_filename)
self.pbuf = (surface, (off_x, off_y))
else:
surface = self.callback_load(self.local_filename)
self.pbuf = (surface, None)
return True
except Exception, e:
if tryno == 0:
return self.recover()
else:
logger.exception("Exception while loading map tile: %s" % e)
self.pbuf = (self.noimage_cantload, None)
return True
def recover(self):
try:
remove(self.local_filename)
except:
pass
self.__download(self.remote_filename, self.local_filename)
return self.load(1)
def draw(self, pbuf):
if not self.stop:
return self.callback_draw(self.id_string, pbuf[0], self.x, self.y, pbuf[1])
return False
def __download(self, remote, local):
if path.exists(local):
return True
#import time
#time.sleep(10)
#return False
with TileLoader.semaphore:
try:
if self.stop:
return None
info = urlretrieve(remote, local)
if "text/html" in info[1]['Content-Type']:
return False
return True
except Exception, e:
print "Download Error", e
return False
def download_tile_only(self):
if not path.isfile(self.local_filename):
self.create_recursive(self.local_path)
return self.__download(self.remote_filename, self.local_filename)
return TileLoader
|