]> gitweb.michael.orlitzky.com - dead/census-tools.git/blobdiff - www/maps/maps/controllers/directions.py
Updated some class names to match a recent Google change.
[dead/census-tools.git] / www / maps / maps / controllers / directions.py
index eabdfd49da36c155787b72a96ba986ee19ee7e1a..f7fa5532307a770efbad4467604cf6856f2f5de0 100644 (file)
@@ -20,9 +20,9 @@ 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
+    # leg back to the server, so we have to rely on the geocoded
     # names for identification.
-    def get_route_address(self, route, start=True):
+    def get_leg_address(self, leg, start=True):
         location_string = "start"
         
         if not start:
@@ -31,7 +31,7 @@ class DirectionsController(BaseController):
             location_string = "end"
 
         # Get the GeocoderResponse object.
-        gr = route[location_string + '_geocode']
+        gr = leg[location_string + '_geocode']
 
         try:
             return gr['formatted_address']
@@ -41,28 +41,28 @@ class DirectionsController(BaseController):
     
     def directions_result_to_placemarks(self, result):
         placemarks = []
-        trip_idx = 0
+        route_idx = 0
 
-        for trip in result['trips']:
-            trip_idx += 1            
+        for route in result['routes']:
+            route_idx += 1
             
-            for route in trip['routes']:
+            for leg in route['legs']:
                 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)
+                leg_start = self.get_leg_address(leg, True)
+                leg_end   = self.get_leg_address(leg, False)
+                leg_name  = leg_start + " to " + leg_end
+                leg_name += ' (' + str(route_idx) + ')'
+                name_element = KML.Name(leg_name)
                 p.children.append(name_element)
                 ls = KML.LineString()
 
                 coords = KML.Coordinates()
                 
-                for step in route['steps']:
+                for step in leg['steps']:
                     for coord in step['lat_lngs']:
-                        coords.text += str(coord.values()[1])
-                        coords.text += ','
                         coords.text += str(coord.values()[0])
+                        coords.text += ','
+                        coords.text += str(coord.values()[1])
                         coords.text += ',0 '
                         
                 ls.children.append(coords)
@@ -73,7 +73,7 @@ class DirectionsController(BaseController):
 
     
     def json_to_kml(self):
-        directions_array = json.loads(request.POST['directions_result'])
+        directions_array = json.loads(request.POST['data'])
         placemarks = []
         for result in directions_array:
             placemarks += self.directions_result_to_placemarks(result)
@@ -86,3 +86,33 @@ class DirectionsController(BaseController):
         response.headers['Content-disposition'] = 'attachment; filename=routes.kml'
         return doc.to_kml()
 
+
+    def directions_result_to_csv(self, result):
+        rows = []
+
+        for route in result['routes']:
+
+            for leg in route['legs']:
+                leg_start = self.get_leg_address(route, True)
+                leg_end   = self.get_leg_address(route, False)
+                row = '"' + leg_start + '"'
+                row += ', '
+                row += '"' + leg_end   + '"'
+                row += ', '
+                row += str(float(leg['distance']['value']) / 1000)
+                rows.append(row)
+                
+        return rows
+
+    
+    def distances(self):
+        directions_array = json.loads(request.POST['data'])
+        csv_rows = []
+        
+        for result in directions_array:
+            csv_rows += self.directions_result_to_csv(result)
+        
+        response.content_type = 'text/csv'
+        response.headers['Content-disposition'] = 'attachment; filename=distances.csv'
+        return "\n".join(csv_rows)
+