From 4484a1e4d6c95985c354e356bad10e15a8bddcda Mon Sep 17 00:00:00 2001 From: Jonathan Bloedow Date: Tue, 18 Jun 2024 18:55:08 -0700 Subject: [PATCH] Get cities.csv from website if not present. --- jb/utils/folium_animate_from_sqlite.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/jb/utils/folium_animate_from_sqlite.py b/jb/utils/folium_animate_from_sqlite.py index 59f5ddd..17bc0ef 100644 --- a/jb/utils/folium_animate_from_sqlite.py +++ b/jb/utils/folium_animate_from_sqlite.py @@ -2,7 +2,8 @@ import sqlite3 import random import numpy as np -import sys +import requests +import os import argparse import os @@ -27,12 +28,27 @@ def preproc( sim_report_file ): cursor.executemany(f"INSERT INTO engwal VALUES ({', '.join(['?' for _ in headers])})", reader) # Import cities.csv into cities table - with open('cities.csv', 'r') as file: - reader = csv.reader(file) + if os.path.exists( 'cities.csv' ): + with open('cities.csv', 'r') as file: + reader = csv.reader(file) + headers = next(reader) + cursor.execute(f"CREATE TABLE cities ({', '.join(headers)})") + cursor.executemany(f"INSERT INTO cities VALUES ({', '.join(['?' for _ in headers])})", reader) + else: + url = 'https://packages.idmod.org:443/artifactory/idm-data/laser/cities.csv' + + # Perform an HTTP GET request + response = requests.get(url) + response.raise_for_status() # Check that the request was successful + + # Process the CSV file directly from the response content + content = response.content.decode('utf-8').splitlines() + reader = csv.reader(content) headers = next(reader) cursor.execute(f"CREATE TABLE cities ({', '.join(headers)})") cursor.executemany(f"INSERT INTO cities VALUES ({', '.join(['?' for _ in headers])})", reader) + # Create the view cursor.execute(""" CREATE VIEW cases AS