In [1]:
import requests
import json
Find your State FIPS Code¶
In [2]:
r = requests.get("https://roadway.report/v1/states")
r.json()[:10]
Out[2]:
[{'id': 1, 'name': 'ALABAMA'},
{'id': 2, 'name': 'ALASKA'},
{'id': 4, 'name': 'ARIZONA'},
{'id': 5, 'name': 'ARKANSAS'},
{'id': 6, 'name': 'CALIFORNIA'},
{'id': 8, 'name': 'COLORADO'},
{'id': 9, 'name': 'CONNECTICUT'},
{'id': 10, 'name': 'DELAWARE'},
{'id': 11, 'name': 'DISTRICT OF COLUMBIA'},
{'id': 12, 'name': 'FLORIDA'}]
Find your County FIPS Code¶
In [3]:
county_id = None
offset=0
STATE_FIPS = 48 # Texas
COUNTY_NAME = "TRAVIS"
while not county_id and offset < 1000:
r = requests.get(f"https://roadway.report/v1/counties?state_id={STATE_FIPS}&offset={offset}")
offset += 100
counties = r.json()['items']
for county in counties:
if county['name'] == COUNTY_NAME:
county_id = county['id']
print(county['id'])
48453
In [4]:
COUNTY_ID=48453
OFFSET = 0
# Set a number larger than the size of the table to start.
count = 10000000
all_json_objects = []
while OFFSET < count:
url = f"https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id={COUNTY_ID}&offset={OFFSET}"
print(f'making request to {url}')
r = requests.get(url)
count = r.json()['count']
OFFSET += 100
all_json_objects += r.json()['items']
geojson = {
"type": "FeatureCollection",
"features": all_json_objects
}
output_file = "texas_crashes.geojson"
print(f"saving file {output_file}")
with open(output_file, "w", encoding="utf-8") as f:
json.dump(geojson, f)
making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=0 making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=100 making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=200 making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=300 making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=400 making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=500 making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=600 making request to https://roadway.report/v1/accidents?datetime__gt=2020-01-01&county_id=48453&offset=700 saving file texas_crashes.geojson
In [5]:
COUNTY_ID=48453
url = f"https://roadway.report/v1/csv?datetime__gt=2020-01-01&county_id={COUNTY_ID}"
print(f'making request to {url}')
r = requests.get(url)
output_file = "travis_county_crashes.csv"
with open(output_file, "wb") as file:
file.write(r.content)
making request to https://roadway.report/v1/csv?datetime__gt=2020-01-01&county_id=48453
Get Bicycle / Micromobility Crashes in King County, WA¶
We want to filter objects from the ACCIDENT table, based on records in the PERSON table, so we use the endpoint /v1/accidents_by_person
In [6]:
import requests
from time import sleep
API_URL = "https://roadway.report/v1/accidents_by_person"
CRASH_DETAILS_URL = "https://roadway.report/v1/accidents/"
List of "Person Types" found here https://roadway.report/v1/person_type_choices
In [7]:
BICYCLE_PERSON_TYPE = "6" #bicycle
PEDALCYCLE_PERSON_TYPE = "7" # other pedalcycle
PERSONAL_CONVEYANCE_PERSON_TYPE = "8" # Other Personal Conveyance
In [8]:
COUNTY_ID = "53033" # king county wa
# injury severity choices https://roadway.report/v1/injury_severity_choices
INJURY_SEVERITY = "4" #fatal
Get lists of accidents containing fatally injured persons in the three person types considered "micromobility"
In [9]:
all_json_objects = []
# Loop through all the Bicycle crashes (Person Type 6)
OFFSET = 0
count = 10000000
while OFFSET < count:
bicycle_fatalities = requests.get(f"{API_URL}?accident__county_id={COUNTY_ID}&person_type={BICYCLE_PERSON_TYPE}&injury_severity={INJURY_SEVERITY}")
count = bicycle_fatalities.json()['count']
OFFSET += 100
all_json_objects += bicycle_fatalities.json()['items']
# Loop through all the Pedalcycle crashes (Person Type 7)
OFFSET = 0
count = 10000000
while OFFSET < count:
pedalcycle_fatalities = requests.get(f"{API_URL}?accident__county_id={COUNTY_ID}&person_type={PEDALCYCLE_PERSON_TYPE}&injury_severity={INJURY_SEVERITY}")
count = pedalcycle_fatalities.json()['count']
OFFSET += 100
all_json_objects += pedalcycle_fatalities.json()['items']
# Loop through all the "Personal Conveyance" crashes (Person Type 8)
OFFSET = 0
count = 10000000
while OFFSET < count:
personal_conveyance_fatalities = requests.get(f"{API_URL}?accident__county_id={COUNTY_ID}&person_type={PERSONAL_CONVEYANCE_PERSON_TYPE}&injury_severity={INJURY_SEVERITY}")
count = personal_conveyance_fatalities.json()['count']
OFFSET += 100
all_json_objects += personal_conveyance_fatalities.json()['items']
geojson = {
"type": "FeatureCollection",
"features": all_json_objects
}
output_file = "king_county_bicycle_crashes.geojson"
print(f"saving file {output_file}")
with open(output_file, "w", encoding="utf-8") as f:
json.dump(geojson, f)
saving file king_county_bicycle_crashes.geojson