Created
August 24, 2022 12:51
-
-
Save leonardehrenfried/f9ba0f585676be35fb9a3fbee5d0241a to your computer and use it in GitHub Desktop.
Script to generate file travelSearch.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import random | |
import haversine as hs | |
file = open("stops.txt", "r") | |
data = list(csv.DictReader(file, delimiter=",")) | |
file.close() | |
f = open('travel.csv', 'w') | |
fieldnames = ["testCaseId", "description", "departure", "fromPlace", "toPlace", "fromLat", "fromLon", "toLat", "toLon", "origin", "destination", "modes", "category"] | |
rows = [] | |
used_names = [] | |
def get_id(stop): | |
if(stop["parent_station"] != ""): | |
return stop["parent_station"] | |
else: | |
return stop["stop_id"] | |
def is_significant(stop): | |
for n in ["Hbf", "Hauptbahnhof"]: | |
if n in stop["stop_name"]: | |
return stop["location_type"] != "2" and len(stop["stop_id"].split(":")) == 3 | |
return False | |
def get_hbf(): | |
stop = None | |
while stop is None: | |
r = random.choice(data) | |
if(is_significant(r)): | |
stop = r | |
print(stop) | |
return stop | |
for i in range(0,100): | |
start = get_hbf() | |
end = get_hbf() | |
loc1=(float(start["stop_lat"]), float(start["stop_lon"])) | |
loc2=(float(end["stop_lat"]), float(end["stop_lon"])) | |
distance = hs.haversine(loc1,loc2) | |
print(distance) | |
rows.append({ | |
"testCaseId": i, | |
"description": f'{start["stop_name"]} to {end["stop_name"]}', | |
"departure": "08:00", | |
"fromPlace": get_id(start), | |
"toPlace": get_id(end), | |
"fromLat": start["stop_lat"], | |
"fromLon": start["stop_lon"], | |
"toLat": end["stop_lat"], | |
"toLon": end["stop_lon"], | |
"origin": start["stop_name"], | |
"destination": end["stop_name"], | |
"modes": "TRANSIT|WALK", | |
"category": "transit" | |
}) | |
with open('travelSearch.csv', 'w', encoding='UTF8', newline='') as f: | |
writer = csv.DictWriter(f, fieldnames=fieldnames) | |
writer.writeheader() | |
writer.writerows(rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment