Archived
1
0
Fork 0
This repository has been archived on 2024-06-28. You can view files and clone it, but cannot push or open issues or pull requests.
citybike/routing.py

58 lines
1.9 KiB
Python
Raw Normal View History

2016-05-31 14:06:15 +02:00
#!/usr/bin/python3
import itertools
import os
2016-07-18 10:39:08 +02:00
import subprocess
2016-05-31 14:50:05 +02:00
import xml.dom.minidom
2016-07-18 09:37:38 +02:00
2016-05-31 14:06:15 +02:00
import MySQLdb
2016-07-18 10:39:08 +02:00
from tqdm import tqdm
2016-05-31 14:06:15 +02:00
2016-06-02 06:56:18 +02:00
from config import database
2016-05-31 14:50:05 +02:00
from gpxlen import getTrackLength
2016-06-02 06:56:18 +02:00
db = MySQLdb.connect(database["host"],
database["user"],
database["passwd"],
database["db"])
2016-05-31 14:06:15 +02:00
2016-06-02 15:14:51 +02:00
cursor = db.cursor()
2016-05-31 14:06:15 +02:00
2016-07-18 10:39:08 +02:00
cursor.execute("SELECT lat, lon,ref FROM stationen ORDER BY ref DESC") # Liste der Stationen
2016-06-02 15:14:51 +02:00
stations = cursor.fetchall()
2016-07-18 10:39:08 +02:00
routeID = 1
2016-07-18 12:08:51 +02:00
totalCombinations = 14641 # 212 über 2
2016-07-18 10:39:08 +02:00
pbar = tqdm(total=totalCombinations)
2016-07-18 12:08:51 +02:00
for way in itertools.product(stations, repeat=2):
2016-07-18 10:39:08 +02:00
command = [
"routino-router",
"--dir=/home/lukas/routino/data",
"--lat1={}".format(way[0][0]),
"--lon1={}".format(way[0][1]),
"--lat2={}".format(way[1][0]),
"--lon2={}".format(way[1][1]),
"--quickest",
"--transport=bicycle",
"--output-gpx-track",
"--quiet"
]
try:
subprocess.check_output(command, stderr=subprocess.STDOUT)
os.rename("quickest-track.gpx", "file/" + str(routeID) + ".gpx") # nach file/2.gpx verschieben
dom = xml.dom.minidom.parse("file/" + str(routeID) + ".gpx")
2016-05-31 14:50:05 +02:00
gpxNode = dom.firstChild
2016-07-18 10:39:08 +02:00
length = round(getTrackLength(gpxNode.getElementsByTagName("trk")[0]), 0) # Länge aus gpx auslesen
2016-10-01 20:41:29 +02:00
cursor.execute("REPLACE INTO connections (id, start, goal, length) VALUES (%s,%s,%s,%s)",
2016-07-18 10:39:08 +02:00
(routeID, way[0][2], way[1][2], length)) # in db eintragen
except subprocess.CalledProcessError as exception:
print() # Neue Zeile
print("Fehler zwischen Station " + str(way[0][2]) + " und " + str(way[1][2]))
print(exception.output.decode())
pbar.update(1)
# stdout.write("\r{:.4f}".format(routeID / totalCombinations * 100)) # Prozentanzeige
routeID += 1
pbar.close()
2016-05-31 14:06:15 +02:00
db.commit()