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

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

try rendering the london underground (don't use this version if you want the street map!)

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