def __init__(self, cache_path=None, download=True, cache_size=500, service="MicrosoftSat", tile_delay=0.3, debug=False, max_zoom=19): if cache_path is None: try: cache_path = os.path.join(os.environ['HOME'], '.tilecache') except Exception: cache_path = os.path.join(tempfile.gettempdir(), 'MAVtilecache') if not os.path.exists(cache_path): mp_util.mkdir_p(cache_path) self.cache_path = cache_path self.max_zoom = max_zoom self.min_zoom = 1 self.download = download self.cache_size = cache_size self.tile_delay = tile_delay self.service = service self.debug = debug if service not in TILE_SERVICES: raise TileException('unknown tile service %s' % service) # _download_pending is a dictionary of TileInfo objects self._download_pending = {} self._download_thread = None self._loading = os.path.join(os.path.dirname(__file__), 'data', 'loading.jpg') self._unavailable = os.path.join(os.path.dirname(__file__), 'data', 'unavailable.jpg') self._tile_cache = collections.OrderedDict()
def __init__(self, server="dds.cr.usgs.gov", directory="/srtm/version2_1/SRTM3/", cachedir=None, offline=0): if cachedir is None: try: cachedir = os.path.join(os.environ['HOME'], '.tilecache/SRTM') except Exception: cachedir = os.path.join(tempfile.gettempdir(), 'MAVProxySRTM') self.offline = offline self.first_failure = False self.server = server self.directory = directory self.cachedir = cachedir '''print "SRTMDownloader - server= %s, directory=%s." % \ (self.server, self.directory)''' if not os.path.exists(cachedir): mp_util.mkdir_p(cachedir) self.filelist = {} self.filename_regex = re.compile( r"([NS])(\d{2})([EW])(\d{3})\.hgt\.zip") self.filelist_file = os.path.join(self.cachedir, "filelist_python") self.childFileListDownload = None self.childTileDownload = None
def __init__(self, server="firmware.ardupilot.org", directory="/SRTM/", cachedir=None, offline=0, debug=False): if cachedir is None: try: cachedir = os.path.join(os.environ['HOME'], '.tilecache/SRTM') except Exception: if 'LOCALAPPDATA' in os.environ: cachedir = os.path.join(os.environ['LOCALAPPDATA'], '.tilecache/SRTM') else: import tempfile cachedir = os.path.join(tempfile.gettempdir(), 'RTM') self.debug = debug self.offline = offline self.offlinemessageshown = 0 if self.offline == 1 and self.debug: print("Map Module in Offline mode") self.first_failure = False self.server = server self.directory = directory self.cachedir = cachedir if self.debug: print("SRTMDownloader - server=%s, directory=%s." % (self.server, self.directory)) if not os.path.exists(cachedir): mp_util.mkdir_p(cachedir) self.filelist = {} self.filename_regex = re.compile( r"([NS])(\d{2})([EW])(\d{3})\.hgt\.zip") self.filelist_file = os.path.join(self.cachedir, "filelist_python") self.min_filelist_len = 14500
def downloader(self): '''the download thread''' http = httplib2.Http() while self.tiles_pending() > 0: time.sleep(self.tile_delay) keys = self._download_pending.keys()[:] # work out which one to download next, choosing by request_time tile_info = self._download_pending[keys[0]] for key in keys: if self._download_pending[ key].request_time > tile_info.request_time: tile_info = self._download_pending[key] url = tile_info.url(self.service) path = self.tile_to_path(tile_info) key = tile_info.key() try: if self.debug: print("Downloading %s [%u left]" % (url, len(keys))) resp, img = http.request(url) except httplib2.HttpLib2Error as e: #print('Error loading %s' % url) self._tile_cache[key] = self._unavailable self._download_pending.pop(key) if self.debug: print("Failed %s: %s" % (url, str(e))) continue if 'content-type' not in resp or resp['content-type'].find( 'image') == -1: self._tile_cache[key] = self._unavailable self._download_pending.pop(key) if self.debug: print("non-image response %s" % url) continue # see if its a blank/unavailable tile md5 = hashlib.md5(img).hexdigest() if md5 in BLANK_TILES: if self.debug: print("blank tile %s" % url) self._tile_cache[key] = self._unavailable self._download_pending.pop(key) continue mp_util.mkdir_p(os.path.dirname(path)) h = open(path + '.tmp', 'wb') h.write(img) h.close() os.rename(path + '.tmp', path) self._download_pending.pop(key) self._download_thread = None
def downloader(self): '''the download thread''' http = httplib2.Http() while self.tiles_pending() > 0: time.sleep(self.tile_delay) keys = self._download_pending.keys()[:] # work out which one to download next, choosing by request_time tile_info = self._download_pending[keys[0]] for key in keys: if self._download_pending[key].request_time > tile_info.request_time: tile_info = self._download_pending[key] url = tile_info.url(self.service) path = self.tile_to_path(tile_info) key = tile_info.key() try: if self.debug: print("Downloading %s [%u left]" % (url, len(keys))) resp,img = http.request(url) except httplib2.HttpLib2Error as e: #print('Error loading %s' % url) self._tile_cache[key] = self._unavailable self._download_pending.pop(key) if self.debug: print("Failed %s: %s" % (url, str(e))) continue if 'content-type' not in resp or resp['content-type'].find('image') == -1: self._tile_cache[key] = self._unavailable self._download_pending.pop(key) if self.debug: print("non-image response %s" % url) continue # see if its a blank/unavailable tile md5 = hashlib.md5(img).hexdigest() if md5 in BLANK_TILES: if self.debug: print("blank tile %s" % url) self._tile_cache[key] = self._unavailable self._download_pending.pop(key) continue mp_util.mkdir_p(os.path.dirname(path)) h = open(path+'.tmp','wb') h.write(img) h.close() os.rename(path+'.tmp', path) self._download_pending.pop(key) self._download_thread = None
def __init__(self, cache_path=None, download=True, cache_size=500, service="MicrosoftSat", tile_delay=0.3, debug=False, max_zoom=19, refresh_age=30 * 24 * 60 * 60): if cache_path is None: try: cache_path = os.path.join(os.environ['HOME'], '.tilecache') except Exception: if 'LOCALAPPDATA' in os.environ: cache_path = os.path.join(os.environ['LOCALAPPDATA'], '.tilecache') else: import tempfile cache_path = os.path.join(tempfile.gettempdir(), '.tilecache') if not os.path.exists(cache_path): mp_util.mkdir_p(cache_path) self.cache_path = cache_path self.max_zoom = max_zoom self.min_zoom = 1 self.download = download self.cache_size = cache_size self.tile_delay = tile_delay self.service = service self.debug = debug self.refresh_age = refresh_age if service not in TILE_SERVICES: raise TileException('unknown tile service %s' % service) # _download_pending is a dictionary of TileInfo objects self._download_pending = {} self._download_thread = None self._loading = mp_icon('loading.jpg') self._unavailable = mp_icon('unavailable.jpg') try: self._tile_cache = collections.OrderedDict() except AttributeError: # OrderedDicts in python 2.6 come from the ordereddict module # which is a 3rd party package, not in python2.6 distribution import ordereddict self._tile_cache = ordereddict.OrderedDict()
def __init__(self, cache_path=None, download=True, cache_size=500, service="MicrosoftSat", tile_delay=0.3, debug=False, max_zoom=19): if cache_path is None: try: cache_path = os.path.join(os.environ['HOME'], '.tilecache') except Exception: import tempfile cache_path = os.path.join(tempfile.gettempdir(), 'MAVtilecache') if not os.path.exists(cache_path): mp_util.mkdir_p(cache_path) self.cache_path = cache_path self.max_zoom = max_zoom self.min_zoom = 1 self.download = download self.cache_size = cache_size self.tile_delay = tile_delay self.service = service self.debug = debug if service not in TILE_SERVICES: raise TileException('unknown tile service %s' % service) # _download_pending is a dictionary of TileInfo objects self._download_pending = {} self._download_thread = None self._loading = os.path.join(os.path.dirname(__file__), 'data', 'loading.jpg') self._unavailable = os.path.join(os.path.dirname(__file__), 'data', 'unavailable.jpg') self._tile_cache = collections.OrderedDict()
def downloader(self): '''the download thread''' while self.tiles_pending() > 0: time.sleep(self.tile_delay) keys = sorted(self._download_pending.keys()) # work out which one to download next, choosing by request_time tile_info = self._download_pending[keys[0]] for key in keys: if self._download_pending[ key].request_time > tile_info.request_time: tile_info = self._download_pending[key] url = tile_info.url(self.service) path = self.tile_to_path(tile_info) key = tile_info.key() try: if self.debug: print("Downloading %s [%u left]" % (url, len(keys))) req = url_request(url) if url.find('google') != -1: req.add_header('Referer', 'https://maps.google.com/') resp = url_open(req) headers = resp.info() except url_error as e: #print('Error loading %s' % url) if not key in self._tile_cache: self._tile_cache[key] = self._unavailable self._download_pending.pop(key) if self.debug: print("Failed %s: %s" % (url, str(e))) continue if 'content-type' not in headers or headers['content-type'].find( 'image') == -1: if not key in self._tile_cache: self._tile_cache[key] = self._unavailable self._download_pending.pop(key) if self.debug: print("non-image response %s" % url) continue else: img = resp.read() # see if its a blank/unavailable tile md5 = hashlib.md5(img).hexdigest() if md5 in BLANK_TILES: if self.debug: print("blank tile %s" % url) if not key in self._tile_cache: self._tile_cache[key] = self._unavailable self._download_pending.pop(key) continue mp_util.mkdir_p(os.path.dirname(path)) h = open(path + '.tmp', 'wb') h.write(img) h.close() try: os.unlink(path) except Exception: pass os.rename(path + '.tmp', path) self._download_pending.pop(key) self._download_thread = None