Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

generatePolylineJS2.py - SF Muni Google Map

By: asweigart on Feb 10th, 2014  |  syntax: Python  |  size: 2.24 KB  |  views: 62  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import munipaths # get the path data we previously grabbed
  2. import random
  3.  
  4. COLORS = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800000', '#008000', '#000080', '#808000', '#008080', '#800080']
  5.  
  6. fo = open('polylineJS2.js', 'w')
  7.  
  8. truncatedRouteList = []
  9.  
  10. allRoutesJavaScript = {}
  11. for route in munipaths.munipaths.keys():
  12.     if route in ['1AX', '1BX', '5L', '8AX', '8BX', '9L', '14L', '14X', '28L', '31AX', '31BX', '38L', '38AX', '38BX', '71L', 'K OWL', 'L OWL', 'M OWL', 'N OWL', 'T OWL', '59', '60', '61', '30X', 'NX']:
  13.         continue
  14.     truncatedRouteList.append(route)
  15.     routeJavaScript = []
  16.     for path in munipaths.munipaths[route]:
  17.         latlngJavaScript = []
  18.         for point in path:
  19.             latlngJavaScript.append('%s,%s' % (point['lat'], point['lon']))
  20.         routeJavaScript.append('[' + ','.join(latlngJavaScript) + ']')
  21.     allRoutesJavaScript[route] = '[' + ','.join(routeJavaScript) + ']'
  22.  
  23. completeJS = []
  24. for route in allRoutesJavaScript.keys(): # write up to the last one so we don't have a trailing comma
  25.     completeJS.append('"%s": %s' % (route, allRoutesJavaScript[route]))
  26.  
  27. fo.write('var allData = {%s};\n' % (','.join(completeJS)))
  28.  
  29. # assign each route a random color
  30. colorAssignments = []
  31. for route in truncatedRouteList:
  32.     colorAssignments.append('"%s": "%s"' % (route, random.choice(COLORS)))
  33. fo.write('var routeColors = {%s};\n' % ','.join(colorAssignments))
  34.  
  35. fo.write('var routeNames = ["%s"];\n' % ('","'.join(truncatedRouteList)))
  36.  
  37. fo.write("""
  38. for (var i = 0; i < routeNames.length; i++) { // loop over each route
  39.    var routeName = routeNames[i];
  40.    for (var j = 0; j < allData[routeName].length; j++) { // loop over each path on the route
  41.        var curPath = allData[routeName][j];
  42.        var polylinePoints = [];
  43.        for (var k = 0; k < curPath.length; k += 2) { // loop over each point in the path
  44.            polylinePoints.push(new google.maps.LatLng(curPath[k], curPath[k+1]));
  45.        }
  46.        var routePath = new google.maps.Polyline({
  47.            path: polylinePoints,
  48.            strokeColor: routeColors[routeName],
  49.            strokeOpacity: 1.0,
  50.            strokeWeight: 2
  51.            });
  52.        routePath.setMap(map);
  53.    }
  54. }
  55. """);
  56.  
  57. fo.close()