X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=www%2Fmaps%2Fmaps%2Fcontrollers%2Fdirections.py;fp=www%2Fmaps%2Fmaps%2Fcontrollers%2Fdirections.py;h=eabdfd49da36c155787b72a96ba986ee19ee7e1a;hb=5485afbd4da2182072ef9756c65137729bf1eee5;hp=0000000000000000000000000000000000000000;hpb=a5341c314815ed4df97e2b0f94d05322660052cf;p=dead%2Fcensus-tools.git diff --git a/www/maps/maps/controllers/directions.py b/www/maps/maps/controllers/directions.py new file mode 100644 index 0000000..eabdfd4 --- /dev/null +++ b/www/maps/maps/controllers/directions.py @@ -0,0 +1,88 @@ +import json +import logging +import os +import site +import sys + +from pylons import request, response, session, tmpl_context as c +from pylons.controllers.util import abort, redirect_to +from maps.lib.base import BaseController, render + +# Ugh. +project_root = os.path.dirname(os.path.abspath(__file__)) + '/../../../..' +site.addsitedir(project_root + '/src') +import KML + + +log = logging.getLogger(__name__) + +class DirectionsController(BaseController): + + # Google geocodes the start and end coordinates + # automatically. There's no other way to pass the "name" of a + # route back to the server, so we have to rely on the geocoded + # names for identification. + def get_route_address(self, route, start=True): + location_string = "start" + + if not start: + # There are only two named properties, one for + # start_geocode and one for end_geocode. + location_string = "end" + + # Get the GeocoderResponse object. + gr = route[location_string + '_geocode'] + + try: + return gr['formatted_address'] + except: + return 'Unknown' + + + def directions_result_to_placemarks(self, result): + placemarks = [] + trip_idx = 0 + + for trip in result['trips']: + trip_idx += 1 + + for route in trip['routes']: + p = KML.Placemark() + route_start = self.get_route_address(route, True) + route_end = self.get_route_address(route, False) + route_name = route_start + " to " + route_end + route_name += ' (' + str(trip_idx) + ')' + name_element = KML.Name(route_name) + p.children.append(name_element) + ls = KML.LineString() + + coords = KML.Coordinates() + + for step in route['steps']: + for coord in step['lat_lngs']: + coords.text += str(coord.values()[1]) + coords.text += ',' + coords.text += str(coord.values()[0]) + coords.text += ',0 ' + + ls.children.append(coords) + p.children.append(ls) + placemarks.append(p) + + return placemarks + + + def json_to_kml(self): + directions_array = json.loads(request.POST['directions_result']) + placemarks = [] + for result in directions_array: + placemarks += self.directions_result_to_placemarks(result) + + doc = KML.Document() + for p in placemarks: + doc.children.append(p) + + response.content_type = 'application/vnd.google-earth.kml+xml' + response.headers['Content-disposition'] = 'attachment; filename=routes.kml' + return doc.to_kml() +