source: subversion/applications/routing/pyroute_webGUI/server.py @ 8115

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

add outline support for serving routes on web interface

  • Property svn:executable set to *
File size: 3.1 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------------------
3# DEVELOPMENT CODE, DO NOT USE
4#----------------------------------------------------------------------------
5# Copyright 2008, Oliver White
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#---------------------------------------------------------------------------
20from BaseHTTPServer import *
21import re
22import sys
23import math
24sys.path.append("../pyroutelib2")
25import routeAsGpx
26
27class tileServer(BaseHTTPRequestHandler):
28  def __init__(self, request, client_address, server):
29    self.re_tile = re.compile('/(\w+)/(\d+)/(\d+)/(\d+)\.png')
30    self.re_route = re.compile('/route\?start=(.*?)%2C(.*?)&end=(.*?)%2C(.*?)&type=(.*?)')
31    BaseHTTPRequestHandler.__init__(self, request, client_address, server)
32
33  def log_message(self, format, *args):
34    pass  # Kill logging to stderr
35 
36  def return_file(self, filename, contentType='text/HTML'):
37    # Serve a real file from disk (for indexes etc.)
38    f = open(filename, "r")
39    self.send_response(200)
40    self.send_header('Content-type',contentType) # TODO: more headers
41    self.end_headers()
42    self.wfile.write(f.read())
43    f.close()
44
45  def do_GET(self):
46    # Specific files to serve:
47    if self.path=='/':
48      self.return_file('slippy.html')
49      return
50
51    # See if a tile was requested
52    match_tile = self.re_tile.search(self.path)
53    match_route = self.re_route.search(self.path)
54   
55    if(match_tile):
56      (layer,z,x,y) = match_tile.groups()
57      z = int(z)
58      x = int(x)
59      y = int(y)
60     
61      # Render the tile
62      print 'Request for %d,%d at zoom %d, layer %s' % (x,y,z,layer)
63      self.send_response(404)
64      return
65     
66      # Return the tile as a PNG
67      self.send_response(200)
68      self.send_header('Content-type','image/PNG')
69      self.end_headers()
70      self.wfile.write(pngData)
71    elif(match_route):
72      (x1,y1,x2,y2,type) = match_route.groups()
73      (x1,y1,x2,y2) = map(float, (x1,y1,x2,y2))
74      dx = x2 - x1
75      dy = y2 - y1
76      dist = math.sqrt(dx * dx + dy * dy)
77      print "Routing from %f,%f to %f,%f = distance %f" % (x1,y1,x2,y2,dist)
78      gpx = routeAsGpx.routeToGpx(
79        x1,y1,x2,y2,
80        "cycle",
81        "Route",
82        "track")
83      self.send_response(200)
84      self.send_header('Content-type','text/plain')
85      self.end_headers()
86      self.wfile.write(gpx)
87    else:
88      print "404: %s" % self.path
89      self.send_response(404)
90
91try:
92  server = HTTPServer(('',1280), tileServer)
93  server.serve_forever()
94except KeyboardInterrupt:
95  server.socket.close()
96  sys.exit()
97
Note: See TracBrowser for help on using the repository browser.