]> gitweb.michael.orlitzky.com - dead/census-tools.git/blobdiff - www/maps/maps/controllers/directions.py
Switched the order of the lat/long output in the KML routes to match the API.
[dead/census-tools.git] / www / maps / maps / controllers / directions.py
index eabdfd49da36c155787b72a96ba986ee19ee7e1a..d1c0d1567439ce847fcdfa639e1094bb38965dc6 100644 (file)
@@ -60,9 +60,9 @@ class DirectionsController(BaseController):
                 
                 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 += ','
+                        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 trip in result['trips']:
+
+            for route in trip['routes']:
+                route_start = self.get_route_address(route, True)
+                route_end   = self.get_route_address(route, False)
+                row = '"' + route_start + '"'
+                row += ', '
+                row += '"' + route_end   + '"'
+                row += ', '
+                row += str(float(route['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)
+