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/nearest.py

55 lines
1.6 KiB
Python
Raw Normal View History

2016-06-02 06:39:58 +02:00
#!/usr/bin/python3
import subprocess
import json
from pprint import pprint
2016-06-02 06:56:18 +02:00
from config import database
2016-06-02 06:39:58 +02:00
import MySQLdb
2016-06-02 06:56:18 +02:00
db = MySQLdb.connect(database["host"],
database["user"],
database["passwd"],
database["db"])
2016-06-02 06:39:58 +02:00
cur = db.cursor()
cur.execute("SELECT ref FROM stationen ORDER BY ref DESC")
2016-07-18 12:08:51 +02:00
stationList = cur.fetchall()
2016-06-02 06:39:58 +02:00
geojsonFeatures = []
ways = set()
2016-07-18 12:08:51 +02:00
limit = 5
for station in stationList:
2016-06-02 06:39:58 +02:00
sql = '''
SELECT
2016-10-02 11:50:24 +02:00
id, start, goal, length
2016-10-01 20:41:29 +02:00
FROM connections
2016-06-02 06:39:58 +02:00
LEFT JOIN stationen ON (start = ref OR goal = ref)
2016-10-02 11:50:24 +02:00
WHERE ref = {ref} AND start != goal
2016-06-02 06:39:58 +02:00
ORDER BY length
2016-07-18 12:08:51 +02:00
LIMIT {limit}
'''.format(ref=station[0], limit=limit)
2016-06-02 06:39:58 +02:00
cur.execute(sql)
nearest = cur.fetchall()
for way in nearest:
2016-07-18 12:08:51 +02:00
if not way[0] in ways: # Wenn nicht schon hinzugefügt
2016-06-02 06:39:58 +02:00
ways.add(way[0])
print(str(way[0]))
filename = str(way[0]) + ".gpx"
geojsonString = subprocess.check_output(["togeojson", "file/" + filename]).decode('UTF-8')
2016-07-18 12:08:51 +02:00
geojson = json.loads(geojsonString)
2016-06-02 06:39:58 +02:00
feature = geojson["features"][0]
del feature["properties"]["desc"], feature["properties"]["name"] # Überreste von GPX entfernen
feature["properties"]["nodes"] = [way[1], way[2]]
2016-06-02 15:14:51 +02:00
feature["properties"]["id"] = way[0]
2016-10-02 11:50:24 +02:00
feature["properties"]["wayLength"] = way[3]
2016-06-02 06:39:58 +02:00
geojsonFeatures.append(feature)
else:
print(str(way[0]) + ": Dup")
2016-06-02 15:14:51 +02:00
geojsonComplete = {
"type": "FeatureCollection",
"features": geojsonFeatures
}
2016-06-02 06:39:58 +02:00
2016-07-18 12:08:51 +02:00
with open('www/nearest.json', 'w') as outfile:
json.dump(geojsonComplete, outfile)