Advertisement
KevinsCommands

Ohio Route Map Python Script

May 20th, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.27 KB | None | 0 0
  1. '''
  2. Read this Reddit post for more information: https://www.reddit.com/r/code/comments/8kvhmg/heres_a_python_script_that_maps_ohios_numbered/
  3.  
  4. Hello there, welcome to my mapping script!
  5. This script takes in text files of the format used by the ODOT in their road inventory located at http://www.dot.state.oh.us/Divisions/Planning/TechServ/TIM/Pages/DESTAPE.aspx
  6. (Make sure to read their readme file to understand the format). The script then outputs a kml with routes traced out and assigned names. I've tried my best to add good comments.
  7. I've also added console messages for most of the steps in the script. Just so you know a station is the name that ODOT gives each entry in it's inventory.
  8. '''
  9.  
  10. #Import operator in order to sort stations further on.
  11. import operator
  12.  
  13. #This is a TXT file which will hold the information for the routes we want to map.
  14. inputFile = open("input.txt", "r")
  15. #This is an empty KML file which will be written to to display each route on a map with a name and a type (State Route, US Route or Interstate).
  16. outputFile = open("output.kml", "w")
  17.  
  18.  
  19. print("---Input and ouput files opened")
  20.  
  21. #This is a list of all the separate lines in the input file.
  22. lines = inputFile.read().split("\n")
  23.  
  24. print("---Input split into separate lines of text")
  25.  
  26. #This is a list that will hold each station of the input file. Stations are just each geographic point in those files.
  27. stations = []
  28. #This is a list that will hold every routes Lat/Long points and the route name separated by type. The three lists are assigned to State Routes, US Routes and Interstates respectively.
  29. routes = [[],[],[]]
  30. routeCoords = [[],[],[]]
  31. legs = [[],[],[]]
  32. legTotalsByType = [-1,-1,-1]
  33. #This is an integer that will indicate what the current route type is as the python script works downwards through the file. 0 is SR, 1 is US and 2 is IR.
  34. currentRouteType = 0
  35.  
  36. print("---Variables set up")
  37.  
  38. #This for loop splits each line into a list. It first makes sure the line isn't an extraneous result by checking if it starts with either SR, US or IR.
  39. for x in range(0,len(lines)):
  40.     if (lines[x][:2] == "SR") | (lines[x][:2] == "US") | (lines[x][:2] == "IR"):
  41.         stations.append(lines[x].split("|"))
  42.  
  43. print("---Lines of text split into individual lists or stations")
  44.  
  45. #This for loop reformats the values of each station
  46. for x in range(0,len(stations)):
  47.     #First index is now a list of route type, name, station number, sequential number for duplicates and heading.
  48.     stations[x][0] = [stations[x][0][:2],stations[x][0][3:8],stations[x][0][10:16],stations[x][0][17],stations[x][0][19:21].strip()]
  49.     #Second index is now a list containing region name and type.
  50.     stations[x][1] = stations[x][1].strip().split("-")
  51.     #Third index is now a string that is just the two letter ref type.
  52.     stations[x][2] = stations[x][2][-3:-1]
  53.     #Fourth index is now a list containing the cross route type, name and logpt.
  54.     stations[x][3] = stations[x][3].strip().split("  ")
  55.     stations[x][3][0] = stations[x][3][0].split(" ")
  56.     length = len(stations[x][3][0])
  57.     for y in range(0,length):
  58.         stations[x][3].insert(y,stations[x][3][y][y])
  59.     stations[x][3].remove(stations[x][3][length])
  60.     #Fifth index is ignored.
  61.     #Sixth index is now a float rather than a string.
  62.     stations[x][5] = float(stations[x][5])
  63.     #Seventh index is now a list containing latitude and longitude separately. They do not need to be numbers as no sorting will be done to them. Only writing to kml document.
  64.     stations[x][6] = stations[x][6].split(" ")
  65.  
  66. print("---Stations reformatted")
  67.  
  68. '''
  69. #These two for loops remove duplicate stations.
  70. for x in range(0,len(stations)):
  71.     for y in range(0,len(stations)):
  72.         if not (x == y):
  73.             if (stations[x][0][0] in stations[y][0]) & (stations[x][0][1] in stations[y][0]) & (stations[x][6][0] in stations[y][6]) & (stations[x][6][1] in stations[y][6]) & (not ((stations[x][2] == "-A"))):
  74.                 stations[x] = [[[],[]],[],"",[],"",0,[[],[]]]
  75.                 break
  76.  
  77. while [[[],[]],[],"",[],"",0,[[],[]]] in stations:
  78.     stations.remove([[[],[]],[],"",[],"",0,[[],[]]])
  79.  
  80. print("---Duplicates removed---")
  81. '''
  82.  
  83. #This for loop assigns each station to a route. The index for a given route in the routes list will be the same as the index for it's coords in the routeCoords list.
  84. for x in range(0,len(stations)):
  85.     if stations[x][0][0] == "SR":
  86.         currentRouteType = 0
  87.     elif stations[x][0][0] == "US":
  88.         currentRouteType = 1
  89.     else:
  90.         currentRouteType = 2
  91.    
  92.     if ((stations[x][2] == "-A")) & (not stations[x][0][1] in routes[currentRouteType]):
  93.         routes[currentRouteType].append(stations[x][0][1])
  94.         routeCoords[currentRouteType].append([])
  95.    
  96.     routeCoords[currentRouteType][routes[currentRouteType].index(stations[x][0][1])].append(stations[x])
  97.  
  98. print("---Stations sorted into routes")
  99.  
  100. #This is currently commented out due to some errors with sorting points and creating weird lines.
  101. '''
  102. #This for loop sorts each routeCoords by state true log milage to ensure correct ordering of points.
  103. for x in range(0,3):
  104.     for y in range(0,len(routeCoords[x])):
  105.         routeCoords[x][y].sort(key=operator.itemgetter(5))
  106.  
  107. print("---Stations sorted by state true log milage")
  108. '''
  109.  
  110. #Never got this to work sadly
  111. '''
  112. #This for loop sorts routes by number and type.
  113. for x in range(0,3):
  114.     routeCoords[x].sort(key=operator.itemgetter())
  115. '''
  116.  
  117. #This for loop breaks the routes into legs so gaps don't create weird cross country lines on our map.
  118. for x in range(0,3):
  119.     for y in range(0,len(routes[x])):
  120.         for z in range(0,len(routeCoords[x][y])):
  121.             if (routeCoords[x][y][z][2] == "-A") | (routeCoords[x][y][z][2] == "-K"):
  122.                 legTotalsByType[x] += 1
  123.                 legs[x].append([routes[x][y]])
  124.            
  125.             legs[x][legTotalsByType[x]].append(routeCoords[x][y][z])
  126.  
  127. print("---Routes split into legs")
  128.  
  129. print(routes)
  130. print(routeCoords)
  131.  
  132. #These following one hundred or so lines write each route to our KML file we created at the start.
  133. outputFile.write("""<?xml version="1.0" encoding="UTF-8"?>
  134. <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
  135. <Document>
  136.     <Style id="SR">
  137.         <LineStyle>
  138.             <width>2</width>
  139.             <color>ff0000ff</color>
  140.         </LineStyle>
  141.         <BalloonStyle>
  142.             <text><![CDATA[<h3>$[name]</h3>]]></text>
  143.         </BalloonStyle>
  144.     </Style>
  145.     <Style id="US">
  146.         <LineStyle>
  147.             <width>4</width>
  148.             <color>ff00ff00</color>
  149.         </LineStyle>
  150.         <BalloonStyle>
  151.             <text><![CDATA[<h3>$[name]</h3>]]></text>
  152.         </BalloonStyle>
  153.     </Style>
  154.     <Style id="IR">
  155.         <LineStyle>
  156.             <width>6</width>
  157.             <color>ffff0000</color>
  158.         </LineStyle>
  159.         <BalloonStyle>
  160.             <text><![CDATA[<h3>$[name]</h3>]]></text>
  161.         </BalloonStyle>
  162.     </Style>
  163.     <Folder>
  164.         <name>State Routes</name>""")
  165.  
  166. for x in range(0,len(legs[0])):
  167.     outputFile.write("""
  168.         <Folder>
  169.             <name>SR """)
  170.    
  171.     outputFile.write("".join(legs[0][x][0]))
  172.    
  173.     outputFile.write("""</name>
  174.             <Placemark>
  175.                 <name>SR """)
  176.    
  177.     outputFile.write("".join(legs[0][x][0]))
  178.    
  179.     outputFile.write("""</name>
  180.                 <LineString>
  181.                     <altitudeMode>clampToGround</altitudeMode>
  182.                     <tesselate>1</tesselate>
  183.                     <gx:drawOrder>3</gx:drawOrder>
  184.                     <coordinates>""")
  185.    
  186.     for y in range(1, len(legs[0][x])):
  187.         outputFile.write("\n                        ")
  188.         outputFile.write("".join(legs[0][x][y][6][1]))
  189.         outputFile.write(",")
  190.         outputFile.write("".join(legs[0][x][y][6][0]))
  191.         outputFile.write(",0")
  192.  
  193.     outputFile.write("""
  194.                     </coordinates>
  195.                 </LineString>
  196.                 <styleUrl>#SR</styleUrl>
  197.             </Placemark>
  198.         </Folder>""")
  199.  
  200. outputFile.write("""
  201.     </Folder>
  202.     <Folder>
  203.         <name>US Routes</name>""")
  204.  
  205. print("---State Routes written to document")
  206.  
  207. for x in range(0,len(legs[1])):
  208.     outputFile.write("""
  209.         <Folder>
  210.             <name>US """)
  211.    
  212.     outputFile.write("".join(legs[1][x][0]))
  213.    
  214.     outputFile.write("""</name>
  215.             <Placemark>
  216.                 <name>SR """)
  217.    
  218.     outputFile.write("".join(legs[1][x][0]))
  219.    
  220.     outputFile.write("""</name>
  221.                 <LineString>
  222.                     <altitudeMode>clampToGround</altitudeMode>
  223.                     <tesselate>1</tesselate>
  224.                     <gx:drawOrder>2</gx:drawOrder>
  225.                     <coordinates>""")
  226.    
  227.     for y in range(1, len(legs[1][x])):
  228.         outputFile.write("\n                        ")
  229.         outputFile.write("".join(legs[1][x][y][6][1]))
  230.         outputFile.write(",")
  231.         outputFile.write("".join(legs[1][x][y][6][0]))
  232.         outputFile.write(",0")
  233.  
  234.     outputFile.write("""
  235.                     </coordinates>
  236.                 </LineString>
  237.                 <styleUrl>#US</styleUrl>
  238.             </Placemark>
  239.         </Folder>""")
  240.  
  241. outputFile.write("""
  242.     </Folder>
  243.     <Folder>
  244.         <name>Interstates</name>""")
  245.  
  246. print("---US Routes written to document")
  247.  
  248. for x in range(0,len(legs[2])):
  249.     outputFile.write("""
  250.         <Folder>
  251.             <name>IR """)
  252.    
  253.     outputFile.write("".join(legs[2][x][0]))
  254.    
  255.     outputFile.write("""</name>
  256.             <Placemark>
  257.                 <name>SR """)
  258.    
  259.     outputFile.write("".join(legs[2][x][0]))
  260.    
  261.     outputFile.write("""</name>
  262.                 <LineString>
  263.                     <altitudeMode>clampToGround</altitudeMode>
  264.                     <tesselate>1</tesselate>
  265.                     <gx:drawOrder>1</gx:drawOrder>
  266.                     <coordinates>""")
  267.    
  268.     for y in range(1, len(legs[2][x])):
  269.         outputFile.write("\n                        ")
  270.         outputFile.write("".join(legs[2][x][y][6][1]))
  271.         outputFile.write(",")
  272.         outputFile.write("".join(legs[2][x][y][6][0]))
  273.         outputFile.write(",0")
  274.    
  275.     outputFile.write("""
  276.                     </coordinates>
  277.                 </LineString>
  278.                 <styleUrl>#IR</styleUrl>
  279.             </Placemark>
  280.         </Folder>""")
  281.  
  282. print("---Interstates written to document")
  283.  
  284. outputFile.write("""
  285.     </Folder>
  286. </Document>
  287. </kml>""")
  288.  
  289. print("---Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement