In [13]:
import requests
Find your State FIPS Code¶
In [60]:
r = requests.get("https://roadway.report/v1/states")
r.json()[:10]
Out[60]:
[{'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 [31]:
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 [44]:
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 [49]:
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
Extract FULL JSON Data for a set of crashes¶
The first two endpoints (/v1/accidents, /v1/csv) return a limited set of fields.
You can extract all crash fields as JSON for each crash, but they can only be retrieved one crash at a time.
In this example, we will garner all data available on bicycle / micromobility fatalities in king county
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 [51]:
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 [52]:
BICYCLE_PERSON_TYPE = "6" #bicycle
PEDALCYCLE_PERSON_TYPE = "7" # other pedalcycle
PERSONAL_CONVEYANCE_PERSON_TYPE = "8" # Other Personal Conveyance
In [55]:
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 [56]:
bicycle_fatalities = requests.get(f"{API_URL}?accident__county_id={COUNTY_ID}&person_type={BICYCLE_PERSON_TYPE}&injury_severity={INJURY_SEVERITY}")
pedalcycle_fatalities = requests.get(f"{API_URL}?accident__county_id={COUNTY_ID}&person_type={PEDALCYCLE_PERSON_TYPE}&injury_severity={INJURY_SEVERITY}")
personal_conveyance_fatalities = requests.get(f"{API_URL}?accident__county_id={COUNTY_ID}&person_type={PERSONAL_CONVEYANCE_PERSON_TYPE}&injury_severity={INJURY_SEVERITY}")
In [59]:
with open("king_county_micromobility_fatalities.geojson", "w") as f:
f.write('{"type": "FeatureCollection", "features": [')
for item in bicycle_fatalities.json()['items']:
y = requests.get(f"{CRASH_DETAILS_URL}{item['id']}")
f.write(y.text)
f.write(",")
print(f"bike death: https://roadway.report/v1/accidents/{item['id']}")
for item in pedalcycle_fatalities.json()['items']:
y = requests.get(f"{CRASH_DETAILS_URL}{item['id']}")
f.write(y.text)
f.write(",")
print(f"other pedal death: https://roadway.report/v1/accidents/{item['id']}")
for item in personal_conveyance_fatalities.json()['items']:
y = requests.get(f"{CRASH_DETAILS_URL}{item['id']}")
f.write(y.text)
f.write(",")
print(f"personal conveyance death: https://roadway.report/v1/accidents/{item['id']}")
f.write(']}')
bike death: https://roadway.report/v1/accidents/1975530082 bike death: https://roadway.report/v1/accidents/1975530115 bike death: https://roadway.report/v1/accidents/1976530126 bike death: https://roadway.report/v1/accidents/1976530336 bike death: https://roadway.report/v1/accidents/1976530540 bike death: https://roadway.report/v1/accidents/1976530561 bike death: https://roadway.report/v1/accidents/1977533279 bike death: https://roadway.report/v1/accidents/1978530019 bike death: https://roadway.report/v1/accidents/1979530124 bike death: https://roadway.report/v1/accidents/1979530289 bike death: https://roadway.report/v1/accidents/1979530667 bike death: https://roadway.report/v1/accidents/1980530498 bike death: https://roadway.report/v1/accidents/1981530021 bike death: https://roadway.report/v1/accidents/1981530097 bike death: https://roadway.report/v1/accidents/1981530173 bike death: https://roadway.report/v1/accidents/1981530227 bike death: https://roadway.report/v1/accidents/1981530477 bike death: https://roadway.report/v1/accidents/1982530112 bike death: https://roadway.report/v1/accidents/1982530652 bike death: https://roadway.report/v1/accidents/1982530657 bike death: https://roadway.report/v1/accidents/1983530235 bike death: https://roadway.report/v1/accidents/1983530319 bike death: https://roadway.report/v1/accidents/1983530400 bike death: https://roadway.report/v1/accidents/1983530532 bike death: https://roadway.report/v1/accidents/1984530185 bike death: https://roadway.report/v1/accidents/1984530330 bike death: https://roadway.report/v1/accidents/1984530382 bike death: https://roadway.report/v1/accidents/1985530442 bike death: https://roadway.report/v1/accidents/1986530118 bike death: https://roadway.report/v1/accidents/1987530191 bike death: https://roadway.report/v1/accidents/1988530360 bike death: https://roadway.report/v1/accidents/1989530143 bike death: https://roadway.report/v1/accidents/1989530387 bike death: https://roadway.report/v1/accidents/1989530468 bike death: https://roadway.report/v1/accidents/1990530449 bike death: https://roadway.report/v1/accidents/1990530457 bike death: https://roadway.report/v1/accidents/1990530538 bike death: https://roadway.report/v1/accidents/1990530550 bike death: https://roadway.report/v1/accidents/1992530069 bike death: https://roadway.report/v1/accidents/1992530616 bike death: https://roadway.report/v1/accidents/1993530332 bike death: https://roadway.report/v1/accidents/1993530336 bike death: https://roadway.report/v1/accidents/1993530343 bike death: https://roadway.report/v1/accidents/1994530239 bike death: https://roadway.report/v1/accidents/1994530412 bike death: https://roadway.report/v1/accidents/1994530413 bike death: https://roadway.report/v1/accidents/1995530021 bike death: https://roadway.report/v1/accidents/1995530443 bike death: https://roadway.report/v1/accidents/1995530588 bike death: https://roadway.report/v1/accidents/1997530113 bike death: https://roadway.report/v1/accidents/1997530159 bike death: https://roadway.report/v1/accidents/1997530281 bike death: https://roadway.report/v1/accidents/1997530397 bike death: https://roadway.report/v1/accidents/1997530524 bike death: https://roadway.report/v1/accidents/1997530548 bike death: https://roadway.report/v1/accidents/1997530574 bike death: https://roadway.report/v1/accidents/1998530218 bike death: https://roadway.report/v1/accidents/1998530256 bike death: https://roadway.report/v1/accidents/1998530422 bike death: https://roadway.report/v1/accidents/1999530161 bike death: https://roadway.report/v1/accidents/1999530262 bike death: https://roadway.report/v1/accidents/1999530535 bike death: https://roadway.report/v1/accidents/2000530254 bike death: https://roadway.report/v1/accidents/2000530266 bike death: https://roadway.report/v1/accidents/2000530293 bike death: https://roadway.report/v1/accidents/2000530340 bike death: https://roadway.report/v1/accidents/2000530342 bike death: https://roadway.report/v1/accidents/2000530566 bike death: https://roadway.report/v1/accidents/2001530043 bike death: https://roadway.report/v1/accidents/2001530156 bike death: https://roadway.report/v1/accidents/2001530415 bike death: https://roadway.report/v1/accidents/2002530214 bike death: https://roadway.report/v1/accidents/2002530252 bike death: https://roadway.report/v1/accidents/2002530501 bike death: https://roadway.report/v1/accidents/2004530220 bike death: https://roadway.report/v1/accidents/2005530133 bike death: https://roadway.report/v1/accidents/2005530222 bike death: https://roadway.report/v1/accidents/2005530223 bike death: https://roadway.report/v1/accidents/2005530534 bike death: https://roadway.report/v1/accidents/2006530080 bike death: https://roadway.report/v1/accidents/2006530448 bike death: https://roadway.report/v1/accidents/2007530061 bike death: https://roadway.report/v1/accidents/2007530147 bike death: https://roadway.report/v1/accidents/2007530379 bike death: https://roadway.report/v1/accidents/2008530430 bike death: https://roadway.report/v1/accidents/2008530474 bike death: https://roadway.report/v1/accidents/2009530050 bike death: https://roadway.report/v1/accidents/2009530195 bike death: https://roadway.report/v1/accidents/2009530204 bike death: https://roadway.report/v1/accidents/2009530221 bike death: https://roadway.report/v1/accidents/2010530269 bike death: https://roadway.report/v1/accidents/2010530418 bike death: https://roadway.report/v1/accidents/2011530212 bike death: https://roadway.report/v1/accidents/2011530222 bike death: https://roadway.report/v1/accidents/2011530387 bike death: https://roadway.report/v1/accidents/2011530408 bike death: https://roadway.report/v1/accidents/2011530434 bike death: https://roadway.report/v1/accidents/2012530049 bike death: https://roadway.report/v1/accidents/2012530197 bike death: https://roadway.report/v1/accidents/2012530407 other pedal death: https://roadway.report/v1/accidents/2008530348 other pedal death: https://roadway.report/v1/accidents/2020530530 personal conveyance death: https://roadway.report/v1/accidents/2007530371 personal conveyance death: https://roadway.report/v1/accidents/2008530079 personal conveyance death: https://roadway.report/v1/accidents/2009530455 personal conveyance death: https://roadway.report/v1/accidents/2011530226 personal conveyance death: https://roadway.report/v1/accidents/2011530426 personal conveyance death: https://roadway.report/v1/accidents/2012530294 personal conveyance death: https://roadway.report/v1/accidents/2014530363 personal conveyance death: https://roadway.report/v1/accidents/2016530139 personal conveyance death: https://roadway.report/v1/accidents/2017530282 personal conveyance death: https://roadway.report/v1/accidents/2019530008 personal conveyance death: https://roadway.report/v1/accidents/2020530550 personal conveyance death: https://roadway.report/v1/accidents/2021530167 personal conveyance death: https://roadway.report/v1/accidents/2021530552 personal conveyance death: https://roadway.report/v1/accidents/2022530707 personal conveyance death: https://roadway.report/v1/accidents/2022530708 personal conveyance death: https://roadway.report/v1/accidents/2023530329 personal conveyance death: https://roadway.report/v1/accidents/2023530357 personal conveyance death: https://roadway.report/v1/accidents/2023530390 personal conveyance death: https://roadway.report/v1/accidents/2023530550 personal conveyance death: https://roadway.report/v1/accidents/2023530775
In [ ]: