Add accidents to DB

This commit is contained in:
Marcel Schwarz 2020-12-19 15:26:02 +01:00
parent 2b205d3927
commit 7704bfcc27

View File

@ -77,6 +77,14 @@ def init_database():
lon REAL, lon REAL,
id_num INTEGER AS (CAST(SUBSTR(id, 12) as INTEGER)) STORED id_num INTEGER AS (CAST(SUBSTR(id, 12) as INTEGER)) STORED
)""") )""")
conn.execute("""CREATE TABLE IF NOT EXISTS accidents(
id INTEGER PRIMARY KEY,
lat REAL,
lon REAL,
location TEXT,
date TEXT,
severity TEXT
)""")
conn.commit() conn.commit()
conn.close() conn.close()
LOG.info("Tables created") LOG.info("Tables created")
@ -97,12 +105,36 @@ def import_bikepoints():
conn = sqlite3.connect("bike-data.db", timeout=300) conn = sqlite3.connect("bike-data.db", timeout=300)
points = json.loads(requests.get("https://api.tfl.gov.uk/BikePoint").text) points = json.loads(requests.get("https://api.tfl.gov.uk/BikePoint").text)
points = list(map(lambda p: (p['id'], p['commonName'], p['lat'], p['lon']), points)) points = list(map(lambda p: (p['id'], p['commonName'], p['lat'], p['lon']), points))
LOG.info(f"Writing {len(points)} bikepoints to DB")
conn.executemany("INSERT OR IGNORE INTO bike_points VALUES (?, ?, ?, ?)", points) conn.executemany("INSERT OR IGNORE INTO bike_points VALUES (?, ?, ?, ?)", points)
conn.commit() conn.commit()
conn.close() conn.close()
LOG.info("Bikepoints imported") LOG.info("Bikepoints imported")
def import_accidents(year):
LOG.info("Importing accidents")
conn = sqlite3.connect("bike-data.db", timeout=300)
def filter_pedal_cycles(accident):
for vehicle in accident['vehicles']:
if vehicle['type'] == "PedalCycle":
return True
return False
accidents = requests.get(f"https://api.tfl.gov.uk/AccidentStats/{year}").text
accidents = json.loads(accidents)
accidents = list(filter(filter_pedal_cycles, accidents))
accidents = list(map(lambda a: (a['id'], a['lat'], a['lon'], a['location'], a['date'], a['severity']), accidents))
LOG.info(f"Writing {len(accidents)} bike accidents to DB")
conn.executemany("INSERT OR IGNORE INTO accidents VALUES (?, ?, ?, ?, ?, ?)", accidents)
conn.commit()
conn.close()
LOG.info("Accidents importet")
def import_usage_stats_file(export_file: ApiExportFile): def import_usage_stats_file(export_file: ApiExportFile):
conn = sqlite3.connect("bike-data.db", timeout=300) conn = sqlite3.connect("bike-data.db", timeout=300)
@ -162,10 +194,12 @@ def main():
for file in all_files: for file in all_files:
import_usage_stats_file(file) import_usage_stats_file(file)
# Import Bikepoints
import_bikepoints()
# Create search-index for faster querying # Create search-index for faster querying
create_indexes() create_indexes()
# Import Bikepoints
import_bikepoints()
# Import bike accidents
import_accidents(2019)
if __name__ == "__main__": if __name__ == "__main__":