]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - www/maps/maps/controllers/directions.py
Added a distances() method returning the distances between consumers/producers.
[dead/census-tools.git] / www / maps / maps / controllers / directions.py
1 import json
2 import logging
3 import os
4 import site
5 import sys
6
7 from pylons import request, response, session, tmpl_context as c
8 from pylons.controllers.util import abort, redirect_to
9 from maps.lib.base import BaseController, render
10
11 # Ugh.
12 project_root = os.path.dirname(os.path.abspath(__file__)) + '/../../../..'
13 site.addsitedir(project_root + '/src')
14 import KML
15
16
17 log = logging.getLogger(__name__)
18
19 class DirectionsController(BaseController):
20
21 # Google geocodes the start and end coordinates
22 # automatically. There's no other way to pass the "name" of a
23 # route back to the server, so we have to rely on the geocoded
24 # names for identification.
25 def get_route_address(self, route, start=True):
26 location_string = "start"
27
28 if not start:
29 # There are only two named properties, one for
30 # start_geocode and one for end_geocode.
31 location_string = "end"
32
33 # Get the GeocoderResponse object.
34 gr = route[location_string + '_geocode']
35
36 try:
37 return gr['formatted_address']
38 except:
39 return 'Unknown'
40
41
42 def directions_result_to_placemarks(self, result):
43 placemarks = []
44 trip_idx = 0
45
46 for trip in result['trips']:
47 trip_idx += 1
48
49 for route in trip['routes']:
50 p = KML.Placemark()
51 route_start = self.get_route_address(route, True)
52 route_end = self.get_route_address(route, False)
53 route_name = route_start + " to " + route_end
54 route_name += ' (' + str(trip_idx) + ')'
55 name_element = KML.Name(route_name)
56 p.children.append(name_element)
57 ls = KML.LineString()
58
59 coords = KML.Coordinates()
60
61 for step in route['steps']:
62 for coord in step['lat_lngs']:
63 coords.text += str(coord.values()[1])
64 coords.text += ','
65 coords.text += str(coord.values()[0])
66 coords.text += ',0 '
67
68 ls.children.append(coords)
69 p.children.append(ls)
70 placemarks.append(p)
71
72 return placemarks
73
74
75 def json_to_kml(self):
76 directions_array = json.loads(request.POST['data'])
77 placemarks = []
78 for result in directions_array:
79 placemarks += self.directions_result_to_placemarks(result)
80
81 doc = KML.Document()
82 for p in placemarks:
83 doc.children.append(p)
84
85 response.content_type = 'application/vnd.google-earth.kml+xml'
86 response.headers['Content-disposition'] = 'attachment; filename=routes.kml'
87 return doc.to_kml()
88
89
90 def directions_result_to_csv(self, result):
91 row = ''
92
93 for trip in result['trips']:
94
95 for route in trip['routes']:
96 route_start = self.get_route_address(route, True)
97 route_end = self.get_route_address(route, False)
98 row += '"' + route_start + '"'
99 row += ', '
100 row += '"' + route_end + '"'
101 row += ', '
102 row += str(float(route['distance']['value']) / 1000)
103 row += "\n"
104
105 return row
106
107
108 def distances(self):
109 directions_array = json.loads(request.POST['data'])
110 csv = ''
111
112 for result in directions_array:
113 csv += self.directions_result_to_csv(result) + "\n"
114
115 response.content_type = 'text/csv'
116 response.headers['Content-disposition'] = 'attachment; filename=distances.csv'
117 return csv
118