source: subversion/applications/viewer/pymap/pymap @ 28620

Last change on this file since 28620 was 14528, checked in by deelkar, 11 years ago

add openstreetbrowser (untested)

  • Property svn:executable set to *
File size: 2.7 KB
Line 
1#!/usr/bin/python
2# -*- coding: iso-8859-1 -*-
3# licensed under a BSD license of your choice
4import urllib,re,os,sys,stat,errno,socket,time,socket
5from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
6
7class MyHttpHandler(BaseHTTPRequestHandler):
8  query_string=''
9
10  def fetch_tile(self):
11    """Retrieve a tile from a remote server and store it locally"""
12    m= re.compile('/tiles/(\w+)/(\d+)/(\d+)/(\d+).png').match(self.path)
13    if not m: return None
14    (layer,z,x,y)=(m.group(1),m.group(2),m.group(3),m.group(4))
15    localpath = installpath + '/tiles/' + layer + '/' + z + '/' + x + '/' + y + '.png'
16
17    # decide the URL to download from
18    if layer == 'mapnik':
19      tileurl = 'http://tile.openstreetmap.org/'+z+'/'+x+'/'+y+'.png'
20    elif layer == 'osma':
21      tileurl = 'http://tah.openstreetmap.org/Tiles/tile/'+z+'/'+x+'/'+y+'.png'
22    elif layer == 'cycle':
23      tileurl = 'http://www.thunderflames.org/tiles/cycle/'+z+'/'+x+'/'+y+'.png'
24    elif layer == 'PSV':
25      tileurl = 'http://xn--pnvkarte-m4a.de/tiles/'+z+'/'+x+'/'+y+'.png'
26    elif layer == 'OSMB':
27      tileurl = 'http://www.openstreetbrowser.org/tiles/'+z+'/'+x+'/'+y+'.png'
28    else: return None
29
30    try: os.makedirs(os.path.dirname(localpath))
31    except os.error, e:
32      if e.errno != errno.EEXIST:
33        raise
34
35    urllib.urlretrieve(tileurl, localpath)
36    return True
37
38  def return_file(self,path):
39      if path.endswith(".png"):contenttype='image/png'
40      else: contenttype='text/html'
41      try:
42        f = open(installpath + path, 'rb')
43        self.send_response(200)
44        self.send_header('Content-type',contenttype)
45        self.end_headers()
46        self.wfile.write(f.read())
47        f.close()
48        return
49      except IOError:
50        self.send_error(404,'File Not Found: %s' % self.path)
51      except socket.error:
52        #client most likely aborted connection (zooming etc)
53        return None
54
55  def do_GET(self):
56        if self.path.find('?') != -1: 
57          (self.path, self.query_string) = self.path.split('?', 1)
58
59        if self.path=='/': self.return_file('/index.html')
60        elif self.path.startswith('/static'):self.return_file(self.path)
61        elif self.path.startswith('/tiles'):
62          try:
63            #if mtime older than 30 days then refetch
64            mtime = os.stat(installpath+self.path).st_mtime
65            if time.time()-2592000 > mtime: self.fetch_tile()
66          except os.error, e:
67            if e.errno == errno.ENOENT:
68              self.fetch_tile()
69            else: raise           
70          self.return_file(self.path)
71        else: self.send_error(404,'File Not Found: %s' % self.path)
72        return
73#main():
74installpath=sys.path[0]
75server = HTTPServer(('127.0.0.1', 8008), MyHttpHandler)
76server.serve_forever()
Note: See TracBrowser for help on using the repository browser.