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

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

program to download and cache OSM data in tile-sized chunks, and run a
python server capable of rendering those into a slippy-map.

No pre-rendering, external dependancies, or databases should be
required. This should run in a standard python install.

  • Property svn:executable set to *
File size: 2.2 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 do_GET(self):
36    # See if a tile was requested
37    match = self.re.search(self.path)
38    if(match):
39      (layer,z,x,y) = match.groups()
40      z = int(z)
41      x = int(x)
42      y = int(y)
43     
44     
45      # Render the tile
46      print 'Request for %d,%d at zoom %d, layer %s' % (x,y,z,layer)
47      pngData = OsmRender.RenderTile(z,x,y, None)
48     
49      if(pngData == None):
50        print "Not found"
51        self.send_response(404)
52        return
53     
54      # Return the tile as a PNG
55      self.send_response(200)
56      self.send_header('Content-type','image/PNG')
57      self.end_headers()
58      self.wfile.write(pngData)
59    else:
60      self.send_response(404)
61
62try:
63  server = HTTPServer(('',1280), tileServer)
64  server.serve_forever()
65except KeyboardInterrupt:
66  server.socket.close()
67  sys.exit()
68
Note: See TracBrowser for help on using the repository browser.