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

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

fix missing newlines

  • Property svn:executable set to *
File size: 2.7 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    # Specific files to serve:
50    if self.path=='/':
51      self.return_file('slippy.html')
52      return
53
54    # See if a tile was requested
55    match = self.re.search(self.path)
56    if(match):
57      (layer,z,x,y) = match.groups()
58      z = int(z)
59      x = int(x)
60      y = int(y)
61     
62      # Render the tile
63      print 'Request for %d,%d at zoom %d, layer %s' % (x,y,z,layer)
64      renderer = RenderModule.RenderClass()
65      pngData = renderer.RenderTile(z,x,y, None)
66     
67      if(pngData == None):
68        print "Not found"
69        self.send_response(404)
70        return
71     
72      # Return the tile as a PNG
73      self.send_response(200)
74      self.send_header('Content-type','image/PNG')
75      self.end_headers()
76      self.wfile.write(pngData)
77    else:
78      self.send_response(404)
79
80try:
81  server = HTTPServer(('',1280), tileServer)
82  server.serve_forever()
83except KeyboardInterrupt:
84  server.socket.close()
85  sys.exit()
86
Note: See TracBrowser for help on using the repository browser.