-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from raceconditionrunning/build-db
build routes.yml from route-db.csv
- Loading branch information
Showing
8 changed files
with
189 additions
and
138 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import csv | ||
import os | ||
import sys | ||
|
||
FIELDS = ['id', 'name', 'start', 'dist', 'elev', 'end', 'type', 'map'] | ||
TYPES = ['Loop', 'P2P', 'OB'] | ||
LOCS = [ | ||
'Beacon', | ||
'CapHill', | ||
'ColCity', | ||
'CSE', | ||
'GasWorks', | ||
'JaysCafe', | ||
'Locks', | ||
'MtBaker', | ||
'Northgate', | ||
'Roosevelt', | ||
'SODO', | ||
'Westlake' | ||
] | ||
|
||
warnings = False | ||
def warn(msg): | ||
global warnings | ||
warnings = True | ||
print(f"WARNING! {msg}") | ||
|
||
def warn_rc(route, msg): | ||
warn(f"route {route['id']}: {msg}") | ||
|
||
def check_route(route, gpx_dir): | ||
# all fields set | ||
for field in FIELDS: | ||
if field not in route or route[field].strip() == '': | ||
warn_rc(route, f"missing '{field}' field") | ||
|
||
# valid type | ||
if route['type'] not in TYPES: | ||
warn_rc(route, f"invalid type '{route['type']}'") | ||
|
||
# valid start and end locations | ||
if route['start'] not in LOCS: | ||
warn_rc(route, f"invalid start '{route['start']}'") | ||
if route['end'] not in LOCS: | ||
warn_rc(route, f"invalid end '{route['end']}'") | ||
|
||
# valid dist and elev | ||
try: | ||
assert 0 < float(route['dist']) | ||
except: | ||
warn_rc(route, f"invalid dist '{route['dist']}'") | ||
try: | ||
assert 0 < float(route['elev']) | ||
except: | ||
warn_rc(route, f"invalid elev '{route['elev']}'") | ||
|
||
# every route has a gpx | ||
gpx_path = os.path.join(gpx_dir, route['id']) + '.gpx' | ||
if not os.path.isfile(gpx_path): | ||
warn_rc(route, f"no GPX file at '{gpx_path}'") | ||
|
||
def main(): | ||
# get args | ||
if len(sys.argv) != 2: | ||
print(f"Usage: {sys.argv[0]} <data_dir>") | ||
exit(1) | ||
|
||
data_dir = sys.argv[1] | ||
if not os.path.isdir(data_dir): | ||
print(f"Error: no such directory '{data_dir}'") | ||
exit(1) | ||
|
||
csv_path = os.path.join(data_dir, 'route-db.csv') | ||
if not os.path.isfile(csv_path): | ||
print(f"Error: no route-db.csv at '{csv_path}'") | ||
exit(1) | ||
|
||
gpx_dir = os.path.join(data_dir, 'gpx') | ||
if not os.path.isdir(gpx_dir): | ||
print(f"Error: no gpx directory at '{gpx_dir}'") | ||
exit(1) | ||
|
||
yml_path = os.path.join(data_dir, 'route-db.yml') | ||
|
||
# read routes | ||
with open(csv_path, 'r') as f: | ||
reader = csv.DictReader(f, dialect='unix') | ||
routes = list(reader) | ||
|
||
# ensure all route ids unique | ||
ids = set() | ||
for route in routes: | ||
if route['id'] in ids: | ||
warn(f"route {route['id']} is not unique") | ||
ids.add(route['id']) | ||
|
||
# check every route | ||
for route in routes: | ||
check_route(route, gpx_dir) | ||
|
||
# bail if any warnings | ||
if warnings: | ||
print("Exiting due to warnings - please fix and re-run.") | ||
exit(1) | ||
|
||
# sort routes by start and increasing distance | ||
routes.sort(key=lambda x: (x['start'], float(x['dist']), x['end'], x['type'], x['id'])) | ||
|
||
# write sorted routes back | ||
with open(csv_path, 'w') as f: | ||
writer = csv.DictWriter(f, fieldnames=reader.fieldnames, dialect='unix') | ||
writer.writeheader() | ||
for route in routes: | ||
writer.writerow(route) | ||
|
||
# output yaml version as well | ||
with open(yml_path, 'w') as f: | ||
f.write('# AUTOGENERATED - DO NOT EDIT\n\n') | ||
for route in routes: | ||
f.write(f"- id: {route['id']}\n") | ||
f.write(f" name: \"{route['name']}\"\n") | ||
f.write(f" start: \"{route['start']}\"\n") | ||
f.write(f" dist: {route['dist']}\n") | ||
f.write(f" elev: {route['elev']}\n") | ||
f.write(f" end: \"{route['end']}\"\n") | ||
f.write(f" type: \"{route['type']}\"\n") | ||
f.write(f" map: \"{route['map']}\"\n") | ||
f.write('\n') | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
route-db.yaml | ||
route-db.yml | ||
routes.yml |
File renamed without changes.
Oops, something went wrong.