ropensci/stats19

How to export data as a json file?

Robinlovelace opened this issue · 3 comments

Reproducible example below:

# install.packages("remotes")
remotes::install_cran("stats19")
#> Skipping install of 'stats19' from a cran remote, the SHA1 (1.2.0) has not changed since last install.
#>   Use `force = TRUE` to force installation
crashes = stats19::get_stats19(year = 2018, output_format = "sf")
#> Files identified: dftRoadSafetyData_Accidents_2018.csv
#>    http://data.dft.gov.uk.s3.amazonaws.com/road-accidents-safety-data/dftRoadSafetyData_Accidents_2018.csv
#> Data already exists in data_dir, not downloading
#> Data saved at ~/stats19-data/dftRoadSafetyData_Accidents_2018.csv
#> Reading in:
#> ~/stats19-data/dftRoadSafetyData_Accidents_2018.csv
#> date and time columns present, creating formatted datetime column
#> 55 rows removed with no coordinates
sf::write_sf(crashes, "crashes.geojson")
system("head crashes.geojson")
{
"type": "FeatureCollection",
"name": "crashes",
"features": [
{ "type": "Feature", "properties": { "accident_index": "2018010080971", "longitude": -0.139737, "latitude": 51.524587, "police_force": "Metropolitan Police", "accident_severity": "Slight", "number_of_vehicles": 2, "number_of_casualties": 2, "date": "2018\/01\/01", "day_of_week": "Monday", "time": "01:30", "local_authority_district": "Camden", "local_authority_highway": "Camden", "first_road_class": "A", "first_road_number": 501, "road_type": "Dual carriageway", "speed_limit": 30, "junction_detail": "Not at junction or within 20 metres", "junction_control": "Data missing or out of range", "second_road_class": null, "second_road_number": 0, "pedestrian_crossing_human_control": "None within 50 metres", "pedestrian_crossing_physical_facilities": "No physical crossing facilities within 50 metres", "light_conditions": "Darkness - lights lit", "weather_conditions": "Fine no high winds", "road_surface_conditions": "Dry", "special_conditions_at_site": "None", "carriageway_hazards": "None", "urban_or_rural_area": "Urban", "did_police_officer_attend_scene_of_accident": 1, "lsoa_of_accident_location": "E01000854", "datetime": "2018\/01\/01 01:30:00+00" }, "geometry": { "type": "Point", "coordinates": [ 529150.0, 182270.0 ] } },
{ "type": "Feature", "properties": { "accident_index": "2018010080973", "longitude": 0.046471, "latitude": 51.539651, "police_force": "Metropolitan Police", "accident_severity": "Slight", "number_of_vehicles": 1, "number_of_casualties": 1, "date": "2018\/01\/01", "day_of_week": "Monday", "time": "00:50", "local_authority_district": "Newham", "local_authority_highway": "Newham", "first_road_class": "B", "first_road_number": 165, "road_type": "Single carriageway", "speed_limit": 30, "junction_detail": "Mini-roundabout", "junction_control": "Give way or uncontrolled", "second_road_class": "Unclassified", "second_road_number": 0, "pedestrian_crossing_human_control": "None within 50 metres", "pedestrian_crossing_physical_facilities": "No physical crossing facilities within 50 metres", "light_conditions": "Darkness - lights lit", "weather_conditions": "Fine no high winds", "road_surface_conditions": "Dry", "special_conditions_at_site": "None", "carriageway_hazards": "None", "urban_or_rural_area": "Urban", "did_police_officer_attend_scene_of_accident": 1, "lsoa_of_accident_location": "E01003531", "datetime": "2018\/01\/01 00:50:00+00" }, "geometry": { "type": "Point", "coordinates": [ 542020.0, 184290.0 ] } },
{ "type": "Feature", "properties": { "accident_index": "2018010080974", "longitude": -0.102474, "latitude": 51.529746, "police_force": "Metropolitan Police", "accident_severity": "Slight", "number_of_vehicles": 2, "number_of_casualties": 1, "date": "2018\/01\/01", "day_of_week": "Monday", "time": "00:45", "local_authority_district": "Islington", "local_authority_highway": "Islington", "first_road_class": "A", "first_road_number": 1, "road_type": "Single carriageway", "speed_limit": 20, "junction_detail": "Crossroads", "junction_control": "Give way or uncontrolled", "second_road_class": "C", "second_road_number": 0, "pedestrian_crossing_human_control": "None within 50 metres", "pedestrian_crossing_physical_facilities": "Pedestrian phase at traffic signal junction", "light_conditions": "Darkness - lights lit", "weather_conditions": "Fine no high winds", "road_surface_conditions": "Dry", "special_conditions_at_site": "None", "carriageway_hazards": "None", "urban_or_rural_area": "Urban", "did_police_officer_attend_scene_of_accident": 1, "lsoa_of_accident_location": "E01002723", "datetime": "2018\/01\/01 00:45:00+00" }, "geometry": { "type": "Point", "coordinates": [ 531720.0, 182910.0 ] } },
{ "type": "Feature", "properties": { "accident_index": "2018010080981", "longitude": 0.037828, "latitude": 51.530179, "police_force": "Metropolitan Police", "accident_severity": "Serious", "number_of_vehicles": 2, "number_of_casualties": 1, "date": "2018\/01\/01", "day_of_week": "Monday", "time": "03:00", "local_authority_district": "Newham", "local_authority_highway": "Newham", "first_road_class": "B", "first_road_number": 167, "road_type": "Dual carriageway", "speed_limit": 30, "junction_detail": "More than 4 arms (not roundabout)", "junction_control": "Auto traffic signal", "second_road_class": "A", "second_road_number": 124, "pedestrian_crossing_human_control": "None within 50 metres", "pedestrian_crossing_physical_facilities": "Pedestrian phase at traffic signal junction", "light_conditions": "Darkness - lights lit", "weather_conditions": "Raining no high winds", "road_surface_conditions": "Wet or damp", "special_conditions_at_site": "None", "carriageway_hazards": "None", "urban_or_rural_area": "Urban", "did_police_officer_attend_scene_of_accident": 1, "lsoa_of_accident_location": "E01003492", "datetime": "2018\/01\/01 03:00:00+00" }, "geometry": { "type": "Point", "coordinates": [ 541450.0, 183220.0 ] } },
{ "type": "Feature", "properties": { "accident_index": "2018010080982", "longitude": 0.065781, "latitude": 51.469258, "police_force": "Metropolitan Police", "accident_severity": "Serious", "number_of_vehicles": 2, "number_of_casualties": 2, "date": "2018\/01\/01", "day_of_week": "Monday", "time": "02:20", "local_authority_district": "Greenwich", "local_authority_highway": "Greenwich", "first_road_class": "A", "first_road_number": 207, "road_type": "Single carriageway", "speed_limit": 30, "junction_detail": "Not at junction or within 20 metres", "junction_control": "Data missing or out of range", "second_road_class": null, "second_road_number": 0, "pedestrian_crossing_human_control": "None within 50 metres", "pedestrian_crossing_physical_facilities": "No physical crossing facilities within 50 metres", "light_conditions": "Darkness - lights lit", "weather_conditions": "Fine no high winds", "road_surface_conditions": "Wet or damp", "special_conditions_at_site": "None", "carriageway_hazards": "None", "urban_or_rural_area": "Urban", "did_police_officer_attend_scene_of_accident": 1, "lsoa_of_accident_location": "E01001682", "datetime": "2018\/01\/01 02:20:00+00" }, "geometry": { "type": "Point", "coordinates": [ 543580.0, 176500.0 ] } },
{ "type": "Feature", "properties": { "accident_index": "2018010080983", "longitude": -0.17972, "latitude": 51.638879, "police_force": "Metropolitan Police", "accident_severity": "Slight", "number_of_vehicles": 2, "number_of_casualties": 4, "date": "2018\/01\/01", "day_of_week": "Monday", "time": "01:52", "local_authority_district": "Barnet", "local_authority_highway": "Barnet", "first_road_class": "A", "first_road_number": 1000, "road_type": "Single carriageway", "speed_limit": 30, "junction_detail": "Not at junction or within 20 metres", "junction_control": "Data missing or out of range", "second_road_class": null, "second_road_number": 0, "pedestrian_crossing_human_control": "None within 50 metres", "pedestrian_crossing_physical_facilities": "No physical crossing facilities within 50 metres", "light_conditions": "Darkness - lights lit", "weather_conditions": "Raining + high winds", "road_surface_conditions": "Wet or damp", "special_conditions_at_site": "None", "carriageway_hazards": "None", "urban_or_rural_area": "Urban", "did_police_officer_attend_scene_of_accident": 1, "lsoa_of_accident_location": "E01000271", "datetime": "2018\/01\/01 01:52:00+00" }, "geometry": { "type": "Point", "coordinates": [ 526060.0, 194910.0 ] } },

Created on 2020-03-19 by the reprex package (v0.3.0)

See output attached.

crashes.geojson.zip

layik commented

If we get some spec, happy to implement this.

But.. this should be a docs thing. The example is more than enough. One needs to remember to convert to 4326 if they like to use the output in mapping libraries like leaflet etc as default CRS in stats19 is UK grid.

Thanks for fast response @layik. In fact it was a computer scientist (who may reply here if they have a GH account) who prompted this issue). We could provide endpoints but think a good thing about the package is that its scope is only to point at official DfT endpoints to guarantee data provenance for the users.