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

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

Send blank backgrounds directly, rather than getting the renderer to do
them (and downloading data for blank tiles, wtf?)

  • Property svn:executable set to *
File size: 3.3 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
27import os
28
29# Choose which rendering engine you want to use here
30import renderer_default as RenderModule
31
32class tileServer(BaseHTTPRequestHandler):
33  def __init__(self, request, client_address, server):
34    self.re = re.compile('/(\w+)/(\d+)/(\d+)/(\d+)\.png')
35    self.blankre = re.compile('/blank/(\w+)/')
36    BaseHTTPRequestHandler.__init__(self, request, client_address, server)
37
38  def log_message(self, format, *args):
39    pass  # Kill logging to stderr
40 
41  def return_file(self, filename, contentType='text/HTML'):
42    # Serve a real file from disk (for indexes etc.)
43    f = open(filename, "r")
44    if(f):
45      self.send_response(200)
46      self.send_header('Content-type',contentType) # TODO: more headers
47      self.end_headers()
48      self.wfile.write(f.read())
49      f.close()
50    else:
51      self.send_response(404)
52     
53  def do_GET(self):
54    # split query strings off and store separately
55    if self.path.find('?') != -1: 
56      (self.path, self.query_string) = self.path.split('?', 1)
57
58    # Specific files to serve:
59    if self.path=='/':
60      self.return_file('slippy.html')
61      return
62
63    # See if a tile was requested
64    blankmatch = self.blankre.match(self.path)
65   
66    if(blankmatch):
67      (name) = blankmatch.groups()
68      filename = "blank/%s.png" % name
69      if(not os.path.exists(filename)):
70        filename = "blank/white.png"
71      self.return_file(filename, 'image/PNG')
72      return
73   
74    tilematch = self.re.match(self.path)
75    if(tilematch):
76      (layer,z,x,y) = tilematch.groups()
77      z = int(z)
78      x = int(x)
79      y = int(y)
80     
81      # Render the tile
82      print 'z%d: %d,%d - %s' % (z,x,y,layer)
83      renderer = RenderModule.RenderClass()
84      pngData = renderer.RenderTile(z,x,y, layer)
85     
86      if(pngData == None):
87        print "Not found"
88        self.send_response(404)
89        return
90      if(pngData[0] == ':'):
91        self.send_response(200)
92        self.wfile.write(pngData)
93        return
94     
95      # Return the tile as a PNG
96      self.send_response(200)
97      self.send_header('Content-type','image/PNG')
98      self.end_headers()
99      self.wfile.write(pngData)
100    else:
101      self.send_response(404)
102
103try:
104  server = HTTPServer(('',1280), tileServer)
105  server.serve_forever()
106except KeyboardInterrupt:
107  server.socket.close()
108  sys.exit()
109
Note: See TracBrowser for help on using the repository browser.