<filename>scripts/library-isochrones.py
import requests
import csv
import os.path
import json
import time

DATA_SOURCE_LIBS = '../data/branches_geocoded.csv'
OUTPUT_DIR = '../data/isochrones/'
API_KEY = ''
API_URL = 'https://api.openrouteservice.org/v2/isochrones/'


def run():

    libraries = []
    with open(DATA_SOURCE_LIBS, 'r') as file:

        reader = csv.reader(file, delimiter=',', quotechar='"')
        next(reader, None)  # skip the header

        for row in reader:
            library = row[0]
            lng = row[3]
            lat = row[4]
            libraries.append({'library': library, 'lat': lat, 'lng': lng})

    transport = ['driving-car', 'driving-hgv', 'cycling-regular', 'cycling-road',
                 'cycling-safe', 'cycling-mountain', 'cycling-tour', 'foot-walking', 'foot-hiking']

    for library in libraries:
        for tran in transport:
            # Check if we already have a file
            if os.path.isfile(OUTPUT_DIR + library['library'] + '_isochrone_' + tran + '.json') == False:

                body = {
                    'locations': [[float(library['lng']), float(library['lat'])]],
                    'range': [1800],
                    'interval': 300,
                    'attributes': ['area', 'reachfactor', 'total_pop'],
                    'range_type': 'time',
                    'location_type': 'destination',
                    'intersections': False
                }

                url = API_URL + tran
                data = requests.post(url, json=body, headers={
                                     'Authorization': API_KEY})

                print(body)
                print(url)
                data = json.loads(data.text)
                with open(OUTPUT_DIR + '/' + library['library'] + '_isochrone_' + tran + '.json', 'w') as outfile:
                    json.dump(data, outfile)
                time.sleep(10)


run()

<filename>scripts/library-isochrones.py
import requests
import csv
import os.path
import json
import time

DATA_SOURCE_LIBS = '../data/branches_geocoded.csv'
OUTPUT_DIR = '../data/isochrones/'
API_KEY = ''
API_URL = 'https://api.openrouteservice.org/v2/isochrones/'


def run():

    libraries = []
    with open(DATA_SOURCE_LIBS, 'r') as file:

        reader = csv.reader(file, delimiter=',', quotechar='"')
        next(reader, None)  # skip the header

        for row in reader:
            library = row[0]
            lng = row[3]
            lat = row[4]
            libraries.append({'library': library, 'lat': lat, 'lng': lng})

    transport = ['driving-car', 'driving-hgv', 'cycling-regular', 'cycling-road',
                 'cycling-safe', 'cycling-mountain', 'cycling-tour', 'foot-walking', 'foot-hiking']

    for library in libraries:
        for tran in transport:
            # Check if we already have a file
            if os.path.isfile(OUTPUT_DIR + library['library'] + '_isochrone_' + tran + '.json') == False:

                body = {
                    'locations': [[float(library['lng']), float(library['lat'])]],
                    'range': [1800],
                    'interval': 300,
                    'attributes': ['area', 'reachfactor', 'total_pop'],
                    'range_type': 'time',
                    'location_type': 'destination',
                    'intersections': False
                }

                url = API_URL + tran
                data = requests.post(url, json=body, headers={
                                     'Authorization': API_KEY})

                print(body)
                print(url)
                data = json.loads(data.text)
                with open(OUTPUT_DIR + '/' + library['library'] + '_isochrone_' + tran + '.json', 'w') as outfile:
                    json.dump(data, outfile)
                time.sleep(10)


run()

# skip the header # Check if we already have a file