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

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

example placename rendering

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