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

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

Separate rendering functionality from base class, and make it easy for
the server to choose which rendering engine it wants to use

  • Property svn:executable set to *
File size: 2.7 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 re
24import sys
25
26# Choose which rendering engine you want to use here
27import renderer_simple as RenderModule
28
29class tileServer(BaseHTTPRequestHandler):
30  def __init__(self, request, client_address, server):
31    self.re = re.compile('/(\w+)/(\d+)/(\d+)/(\d+)\.png')
32    BaseHTTPRequestHandler.__init__(self, request, client_address, server)
33
34  def log_message(self, format, *args):
35    pass  # Kill logging to stderr
36 
37  def return_file(self, filename, contentType='text/HTML'):
38    # Serve a real file from disk (for indexes etc.)
39    f = open(filename, "r")
40    self.send_response(200)
41    self.send_header('Content-type',contentType) # TODO: more headers
42    self.end_headers()
43    self.wfile.write(f.read())
44    f.close()
45
46  def do_GET(self):
47    # Specific files to serve:
48    if self.path=='/':
49      self.return_file('slippy.html')
50      return
51
52    # See if a tile was requested
53    match = self.re.search(self.path)
54    if(match):
55      (layer,z,x,y) = match.groups()
56      z = int(z)
57      x = int(x)
58      y = int(y)
59     
60      # Render the tile
61      print 'Request for %d,%d at zoom %d, layer %s' % (x,y,z,layer)
62      renderer = RenderModule.RenderClass()
63      pngData = renderer.RenderTile(z,x,y, None)
64     
65      if(pngData == None):
66        print "Not found"
67        self.send_response(404)
68        return
69     
70      # Return the tile as a PNG
71      self.send_response(200)
72      self.send_header('Content-type','image/PNG')
73      self.end_headers()
74      self.wfile.write(pngData)
75    else:
76      self.send_response(404)
77
78try:
79  server = HTTPServer(('',1280), tileServer)
80  server.serve_forever()
81except KeyboardInterrupt:
82  server.socket.close()
83  sys.exit()
84
Note: See TracBrowser for help on using the repository browser.