2019-01-20 10:29:54 -05:00
|
|
|
"""Load CSV data to API
|
|
|
|
|
|
|
|
This is effectively an example script using the HTTP API, tailored to particular collected
|
|
|
|
datasets for conservation areas around London.
|
|
|
|
|
|
|
|
Preprocess to filter down to unique buildings:
|
|
|
|
|
|
|
|
head -n 1 file.csv > file.uniq.csv
|
|
|
|
cat file.csv | \
|
|
|
|
cut -d "," -f 1,2 --complement | \
|
|
|
|
sort -n | \
|
|
|
|
uniq >> file.uniq.csv
|
|
|
|
|
|
|
|
Then with this script:
|
|
|
|
- read through CSV file, where entries have been pre-matched to building_id
|
|
|
|
- update building with data
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
import csv
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
|
|
|
|
|
|
|
def main(base_url, api_key, source_file):
|
2023-08-15 09:10:19 -04:00
|
|
|
"""Read from file, update buildings"""
|
|
|
|
with open(source_file, "r") as source_fh:
|
2019-01-20 10:29:54 -05:00
|
|
|
source = csv.DictReader(source_fh)
|
|
|
|
for feature in tqdm(source, total=line_count(source_file)):
|
|
|
|
building_id, data = process_ca(feature)
|
2023-08-15 09:10:19 -04:00
|
|
|
if building_id and building_id != "building_id":
|
2019-01-20 10:29:54 -05:00
|
|
|
save_data(building_id, data, api_key, base_url)
|
|
|
|
|
|
|
|
|
|
|
|
def line_count(fname):
|
2023-08-15 09:10:19 -04:00
|
|
|
"""Count lines - relies on 'wc'"""
|
|
|
|
p = subprocess.run(["wc", "-l", fname], stdout=subprocess.PIPE)
|
2019-01-20 10:29:54 -05:00
|
|
|
if p.returncode != 0:
|
2023-08-15 09:10:19 -04:00
|
|
|
raise IOError(p.returncode)
|
2019-01-20 10:29:54 -05:00
|
|
|
return int(p.stdout.strip().split()[0])
|
|
|
|
|
2023-08-15 09:10:19 -04:00
|
|
|
|
2019-01-20 10:29:54 -05:00
|
|
|
def process_ca(props):
|
2023-08-15 09:10:19 -04:00
|
|
|
building_id = props["building_id"]
|
2019-01-20 10:29:54 -05:00
|
|
|
data = {
|
2023-08-15 09:10:19 -04:00
|
|
|
"planning_in_conservation_area": True,
|
|
|
|
"planning_conservation_area_name": props["conservation_area_name"],
|
2019-01-20 10:29:54 -05:00
|
|
|
}
|
|
|
|
return building_id, data
|
|
|
|
|
|
|
|
|
|
|
|
def save_data(building_id, data, api_key, base_url):
|
2023-08-15 09:10:19 -04:00
|
|
|
"""Save data to a building"""
|
|
|
|
requests.post(
|
2019-08-14 09:05:49 -04:00
|
|
|
"{}/buildings/{}.json?api_key={}".format(base_url, building_id, api_key),
|
2023-08-15 09:10:19 -04:00
|
|
|
json=data,
|
2019-01-20 10:29:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-15 09:10:19 -04:00
|
|
|
if __name__ == "__main__":
|
2019-01-20 10:29:54 -05:00
|
|
|
try:
|
|
|
|
url, api_key, filename = sys.argv[1], sys.argv[2], sys.argv[3]
|
|
|
|
except IndexError:
|
|
|
|
print(
|
|
|
|
"Usage: {} <URL> <api_key> ./path/to/conservation_areas.csv".format(
|
2023-08-15 09:10:19 -04:00
|
|
|
os.path.basename(__file__)
|
|
|
|
)
|
|
|
|
)
|
2019-01-20 10:29:54 -05:00
|
|
|
exit()
|
|
|
|
|
|
|
|
main(url, api_key, filename)
|