]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - maps/controllers/directions.py
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
12 project_root
= os
.path
.dirname(os
.path
.abspath(__file__
)) + '/../../../..'
13 site
.addsitedir(project_root
+ '/src')
17 log
= logging
.getLogger(__name__
)
19 class DirectionsController(BaseController
):
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"
29 # There are only two named properties, one for
30 # start_geocode and one for end_geocode.
31 location_string
= "end"
33 # Get the GeocoderResponse object.
34 gr
= route
[location_string
+ '_geocode']
37 return gr
['formatted_address']
42 def directions_result_to_placemarks(self
, result
):
46 for trip
in result
['trips']:
49 for route
in trip
['routes']:
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
)
59 coords
= KML
.Coordinates()
61 for step
in route
['steps']:
62 for coord
in step
['lat_lngs']:
63 coords
.text
+= str(coord
.values()[1])
65 coords
.text
+= str(coord
.values()[0])
68 ls
.children
.append(coords
)
75 def json_to_kml(self
):
76 directions_array
= json
.loads(request
.POST
['directions_result'])
78 for result
in directions_array
:
79 placemarks
+= self
.directions_result_to_placemarks(result
)
83 doc
.children
.append(p
)
85 response
.content_type
= 'application/vnd.google-earth.kml+xml'
86 response
.headers
['Content-disposition'] = 'attachment; filename=routes.kml'