ub-movements-test/parse_data.py

55 lines
1.3 KiB
Python
Raw Normal View History

import csv
import json
node_names = set()
edges = []
2024-04-12 13:31:55 +00:00
libraries = {}
locations = {}
with open("data/locaties.csv") as fp:
reader = csv.DictReader(fp, delimiter=";")
for item in reader:
locatie = item['Locatie'].split(',')
try:
lat, lon = locatie
except ValueError as e:
lat, lon = None, None
library = {
'name': item['Library Name'],
'code': item['Library Code'],
'adres': item['Adres'],
'lat': lat,
'lon': lon,
}
location = {
'location': item['Location Name'],
'code': item['Location Code'],
'library': library
}
libraries[library['name']] = library
locations[location['code']] = location
with open("data/requests.csv") as fp:
reader = csv.DictReader(fp, delimiter=";")
for item in reader:
node_names.add(item['Owning Library Name'])
node_names.add(item['Pickup Location'])
edges.append(item)
nodes = [{'name': n} for n in node_names]
print(f"{len(nodes)} nodes, {len(edges)} edges")
data = {
2024-04-12 13:31:55 +00:00
'nodes': list(libraries.values()), #nodes,
'edges': edges
}
fn = 'data/parsed_requests.json'
with open(fn, 'w') as fp:
json.dump(data, fp)
print(f"Written to {fn}")