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

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

IDs for routes created

  • Property svn:executable set to *
File size: 4.5 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
24import os
25import pickle
26
27from tile_route import TileRoute
28
29sys.path.append("../pyroutelib2")
30import routeAsCSV
31
32class tileServer(BaseHTTPRequestHandler):
33  def __init__(self, request, client_address, server):
34    self.settings = None
35    self.re_tile = re.compile('/tile/(\w+)/(\d+)/(\d+)/(\d+)\.png')
36    self.re_route = re.compile('/route\?start=(.*?)%2C(.*?)&end=(.*?)%2C(.*?)&type=(.*?)')
37    BaseHTTPRequestHandler.__init__(self, request, client_address, server)
38
39  def getNextRoute(self):
40    for id in range(1,10000):
41      filename = "route/%d.txt" % id
42      if(not os.path.exists(filename)):
43        return(filename,id)
44
45  def settingsFilename(self):
46    return("settings.txt")
47 
48  def getSettings(self):
49    """Load self.settings from disk, or return existing copy, or create blank copy"""
50    if(not self.settings):
51      try:
52        self.settings = {}
53        f = open(self.settingsFilename(), "r")
54        self.settings = pickle.load(f)
55        f.close()
56      except IOError:
57        print "Couldn't load settings from disk"
58       
59       
60  def saveSettings(self):
61    """save self.settings to disk"""
62    f = open(self.settingsFilename(), "w")
63    pickle.dump(self.settings, f)
64    f.close()
65   
66  def log_message(self, format, *args):
67    pass  # Kill logging to stderr
68 
69  def return_file(self, filename, contentType='text/HTML'):
70    # Serve a real file from disk (for indexes etc.)
71    f = open(filename, "r")
72    self.send_response(200)
73    self.send_header('Content-type',contentType) # TODO: more headers
74    self.end_headers()
75    self.wfile.write(f.read())
76    f.close()
77
78  def do_GET(self):
79    # Specific files to serve:
80    if(self.path[0:4]=='/map/' or self.path=='/'):
81      self.return_file('slippy.html')
82      return
83
84    # See if a tile was requested
85    match_tile = self.re_tile.search(self.path)
86    match_route = self.re_route.search(self.path)
87   
88    if self.path=='/counter':
89      self.send_response(200)
90      self.send_header('Content-type','text/plain')
91      self.end_headers()
92     
93      self.getSettings()
94      self.settings['test'] = self.settings.get('test',0) + 1
95      self.wfile.write(self.settings['test'])
96      self.saveSettings()
97   
98    elif(match_tile):
99      (layer,z,x,y) = match_tile.groups()
100      z = int(z)
101      x = int(x)
102      y = int(y)
103     
104      # Render the tile
105      print 'Request for %d,%d at zoom %d, layer %s' % (x,y,z,layer)
106
107
108      a = TileRoute("route/latest.csv")
109      pngData = a.RenderTile(z,x,y,None)
110
111      # Return the tile as a PNG
112      self.send_response(200)
113      self.send_header('Content-type','image/PNG')
114      self.end_headers()
115      self.wfile.write(pngData)
116   
117    elif(match_route):
118      (x1,y1,x2,y2,type) = match_route.groups()
119      (x1,y1,x2,y2) = map(float, (x1,y1,x2,y2))
120      dx = x2 - x1
121      dy = y2 - y1
122      dist = math.sqrt(dx * dx + dy * dy)
123      print "Routing from %f,%f to %f,%f = distance %f" % (x1,y1,x2,y2,dist)
124      (filename,id) = self.getNextRoute()
125      if(filename):
126        routeAsCSV.routeToCSVFile(
127          x1,y1,x2,y2,
128          "cycle",
129          filename)
130        self.send_response(200)
131        self.send_header('Content-type','text/HTML')
132        self.end_headers()
133        self.wfile.write("<p>Your route is available on the <a href='/map/?route=%d'>map</a></p>" % id)
134      else:
135        self.send_response(404)
136    else:
137      print "404: %s" % self.path
138      self.send_response(404)
139
140try:
141  server = HTTPServer(('',1280), tileServer)
142  server.serve_forever()
143except KeyboardInterrupt:
144  server.socket.close()
145  sys.exit()
146
Note: See TracBrowser for help on using the repository browser.