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

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

Start to generalise rendering engine

  • 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 *
23from OsmRender import OsmRenderBase as Renderer
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      renderer = Renderer()
62      pngData = renderer.RenderTile(z,x,y, None)
63     
64      if(pngData == None):
65        print "Not found"
66        self.send_response(404)
67        return
68     
69      # Return the tile as a PNG
70      self.send_response(200)
71      self.send_header('Content-type','image/PNG')
72      self.end_headers()
73      self.wfile.write(pngData)
74    else:
75      self.send_response(404)
76
77try:
78  server = HTTPServer(('',1280), tileServer)
79  server.serve_forever()
80except KeyboardInterrupt:
81  server.socket.close()
82  sys.exit()
83
Note: See TracBrowser for help on using the repository browser.