source: subversion/applications/rendering/pyrender/server.py @ 7755

Last change on this file since 7755 was 7755, checked in by ojw, 11 years ago

Serve the slippy-map through the same server, so you can browse the map
at localhost:1280

  • Property svn:executable set to *
File size: 2.6 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------# Map tile server
3#
4# Features:
5#   * Serves slippy-map tile images
6#   * Uses OsmRender module to render them
7#-----------------------------------------------------------------------------# Copyright 2008, Oliver White
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#----------------------------------------------------------------------------
22from BaseHTTPServer import *
23import OsmRender
24import re
25import sys
26
27class tileServer(BaseHTTPRequestHandler):
28  def __init__(self, request, client_address, server):
29    self.re = re.compile('/(\w+)/(\d+)/(\d+)/(\d+)\.png')
30    BaseHTTPRequestHandler.__init__(self, request, client_address, server)
31
32  def log_message(self, format, *args):
33    pass  # Kill logging to stderr
34 
35  def return_file(self, filename, contentType='text/HTML'):
36    # Serve a real file from disk (for indexes etc.)
37    f = open(filename, "r")
38    self.send_response(200)
39    self.send_header('Content-type',contentType) # TODO: more headers
40    self.end_headers()
41    self.wfile.write(f.read())
42    f.close()
43
44  def do_GET(self):
45    # Specific files to serve:
46    if self.path=='/':
47      self.return_file('slippy.html')
48      return
49
50    # See if a tile was requested
51    match = self.re.search(self.path)
52    if(match):
53      (layer,z,x,y) = match.groups()
54      z = int(z)
55      x = int(x)
56      y = int(y)
57     
58     
59      # Render the tile
60      print 'Request for %d,%d at zoom %d, layer %s' % (x,y,z,layer)
61      pngData = OsmRender.RenderTile(z,x,y, None)
62     
63      if(pngData == None):
64        print "Not found"
65        self.send_response(404)
66        return
67     
68      # Return the tile as a PNG
69      self.send_response(200)
70      self.send_header('Content-type','image/PNG')
71      self.end_headers()
72      self.wfile.write(pngData)
73    else:
74      self.send_response(404)
75
76try:
77  server = HTTPServer(('',1280), tileServer)
78  server.serve_forever()
79except KeyboardInterrupt:
80  server.socket.close()
81  sys.exit()
82
Note: See TracBrowser for help on using the repository browser.