Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Read this Reddit post for more information: https://www.reddit.com/r/code/comments/8kvhmg/heres_a_python_script_that_maps_ohios_numbered/
- Hello there, welcome to my mapping script!
- 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
- (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.
- 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.
- '''
- #Import operator in order to sort stations further on.
- import operator
- #This is a TXT file which will hold the information for the routes we want to map.
- inputFile = open("input.txt", "r")
- #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).
- outputFile = open("output.kml", "w")
- print("---Input and ouput files opened")
- #This is a list of all the separate lines in the input file.
- lines = inputFile.read().split("\n")
- print("---Input split into separate lines of text")
- #This is a list that will hold each station of the input file. Stations are just each geographic point in those files.
- stations = []
- #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.
- routes = [[],[],[]]
- routeCoords = [[],[],[]]
- legs = [[],[],[]]
- legTotalsByType = [-1,-1,-1]
- #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.
- currentRouteType = 0
- print("---Variables set up")
- #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.
- for x in range(0,len(lines)):
- if (lines[x][:2] == "SR") | (lines[x][:2] == "US") | (lines[x][:2] == "IR"):
- stations.append(lines[x].split("|"))
- print("---Lines of text split into individual lists or stations")
- #This for loop reformats the values of each station
- for x in range(0,len(stations)):
- #First index is now a list of route type, name, station number, sequential number for duplicates and heading.
- 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()]
- #Second index is now a list containing region name and type.
- stations[x][1] = stations[x][1].strip().split("-")
- #Third index is now a string that is just the two letter ref type.
- stations[x][2] = stations[x][2][-3:-1]
- #Fourth index is now a list containing the cross route type, name and logpt.
- stations[x][3] = stations[x][3].strip().split(" ")
- stations[x][3][0] = stations[x][3][0].split(" ")
- length = len(stations[x][3][0])
- for y in range(0,length):
- stations[x][3].insert(y,stations[x][3][y][y])
- stations[x][3].remove(stations[x][3][length])
- #Fifth index is ignored.
- #Sixth index is now a float rather than a string.
- stations[x][5] = float(stations[x][5])
- #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.
- stations[x][6] = stations[x][6].split(" ")
- print("---Stations reformatted")
- '''
- #These two for loops remove duplicate stations.
- for x in range(0,len(stations)):
- for y in range(0,len(stations)):
- if not (x == y):
- 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"))):
- stations[x] = [[[],[]],[],"",[],"",0,[[],[]]]
- break
- while [[[],[]],[],"",[],"",0,[[],[]]] in stations:
- stations.remove([[[],[]],[],"",[],"",0,[[],[]]])
- print("---Duplicates removed---")
- '''
- #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.
- for x in range(0,len(stations)):
- if stations[x][0][0] == "SR":
- currentRouteType = 0
- elif stations[x][0][0] == "US":
- currentRouteType = 1
- else:
- currentRouteType = 2
- if ((stations[x][2] == "-A")) & (not stations[x][0][1] in routes[currentRouteType]):
- routes[currentRouteType].append(stations[x][0][1])
- routeCoords[currentRouteType].append([])
- routeCoords[currentRouteType][routes[currentRouteType].index(stations[x][0][1])].append(stations[x])
- print("---Stations sorted into routes")
- #This is currently commented out due to some errors with sorting points and creating weird lines.
- '''
- #This for loop sorts each routeCoords by state true log milage to ensure correct ordering of points.
- for x in range(0,3):
- for y in range(0,len(routeCoords[x])):
- routeCoords[x][y].sort(key=operator.itemgetter(5))
- print("---Stations sorted by state true log milage")
- '''
- #Never got this to work sadly
- '''
- #This for loop sorts routes by number and type.
- for x in range(0,3):
- routeCoords[x].sort(key=operator.itemgetter())
- '''
- #This for loop breaks the routes into legs so gaps don't create weird cross country lines on our map.
- for x in range(0,3):
- for y in range(0,len(routes[x])):
- for z in range(0,len(routeCoords[x][y])):
- if (routeCoords[x][y][z][2] == "-A") | (routeCoords[x][y][z][2] == "-K"):
- legTotalsByType[x] += 1
- legs[x].append([routes[x][y]])
- legs[x][legTotalsByType[x]].append(routeCoords[x][y][z])
- print("---Routes split into legs")
- print(routes)
- print(routeCoords)
- #These following one hundred or so lines write each route to our KML file we created at the start.
- outputFile.write("""<?xml version="1.0" encoding="UTF-8"?>
- <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
- <Document>
- <Style id="SR">
- <LineStyle>
- <width>2</width>
- <color>ff0000ff</color>
- </LineStyle>
- <BalloonStyle>
- <text><![CDATA[<h3>$[name]</h3>]]></text>
- </BalloonStyle>
- </Style>
- <Style id="US">
- <LineStyle>
- <width>4</width>
- <color>ff00ff00</color>
- </LineStyle>
- <BalloonStyle>
- <text><![CDATA[<h3>$[name]</h3>]]></text>
- </BalloonStyle>
- </Style>
- <Style id="IR">
- <LineStyle>
- <width>6</width>
- <color>ffff0000</color>
- </LineStyle>
- <BalloonStyle>
- <text><![CDATA[<h3>$[name]</h3>]]></text>
- </BalloonStyle>
- </Style>
- <Folder>
- <name>State Routes</name>""")
- for x in range(0,len(legs[0])):
- outputFile.write("""
- <Folder>
- <name>SR """)
- outputFile.write("".join(legs[0][x][0]))
- outputFile.write("""</name>
- <Placemark>
- <name>SR """)
- outputFile.write("".join(legs[0][x][0]))
- outputFile.write("""</name>
- <LineString>
- <altitudeMode>clampToGround</altitudeMode>
- <tesselate>1</tesselate>
- <gx:drawOrder>3</gx:drawOrder>
- <coordinates>""")
- for y in range(1, len(legs[0][x])):
- outputFile.write("\n ")
- outputFile.write("".join(legs[0][x][y][6][1]))
- outputFile.write(",")
- outputFile.write("".join(legs[0][x][y][6][0]))
- outputFile.write(",0")
- outputFile.write("""
- </coordinates>
- </LineString>
- <styleUrl>#SR</styleUrl>
- </Placemark>
- </Folder>""")
- outputFile.write("""
- </Folder>
- <Folder>
- <name>US Routes</name>""")
- print("---State Routes written to document")
- for x in range(0,len(legs[1])):
- outputFile.write("""
- <Folder>
- <name>US """)
- outputFile.write("".join(legs[1][x][0]))
- outputFile.write("""</name>
- <Placemark>
- <name>SR """)
- outputFile.write("".join(legs[1][x][0]))
- outputFile.write("""</name>
- <LineString>
- <altitudeMode>clampToGround</altitudeMode>
- <tesselate>1</tesselate>
- <gx:drawOrder>2</gx:drawOrder>
- <coordinates>""")
- for y in range(1, len(legs[1][x])):
- outputFile.write("\n ")
- outputFile.write("".join(legs[1][x][y][6][1]))
- outputFile.write(",")
- outputFile.write("".join(legs[1][x][y][6][0]))
- outputFile.write(",0")
- outputFile.write("""
- </coordinates>
- </LineString>
- <styleUrl>#US</styleUrl>
- </Placemark>
- </Folder>""")
- outputFile.write("""
- </Folder>
- <Folder>
- <name>Interstates</name>""")
- print("---US Routes written to document")
- for x in range(0,len(legs[2])):
- outputFile.write("""
- <Folder>
- <name>IR """)
- outputFile.write("".join(legs[2][x][0]))
- outputFile.write("""</name>
- <Placemark>
- <name>SR """)
- outputFile.write("".join(legs[2][x][0]))
- outputFile.write("""</name>
- <LineString>
- <altitudeMode>clampToGround</altitudeMode>
- <tesselate>1</tesselate>
- <gx:drawOrder>1</gx:drawOrder>
- <coordinates>""")
- for y in range(1, len(legs[2][x])):
- outputFile.write("\n ")
- outputFile.write("".join(legs[2][x][y][6][1]))
- outputFile.write(",")
- outputFile.write("".join(legs[2][x][y][6][0]))
- outputFile.write(",0")
- outputFile.write("""
- </coordinates>
- </LineString>
- <styleUrl>#IR</styleUrl>
- </Placemark>
- </Folder>""")
- print("---Interstates written to document")
- outputFile.write("""
- </Folder>
- </Document>
- </kml>""")
- print("---Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement