-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from olincollege/SAN-38-vision-zero-accidents
SAN-38 vision zero accidents
- Loading branch information
Showing
6 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
Binary file added
BIN
+300 KB
docs/source/_static/images/Vision_Zero_Network_Community_Map_February_2024.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import geopandas as gpd | ||
import requests | ||
import pandas as pd | ||
|
||
BOSTON_VISION_ZERO_URL = "https://data.boston.gov/api/3/action/datastore_search_sql" | ||
BOSTON_VISION_ZERO_PED_CRASHES = '"e4bfe397-6bfc-49c5-9367-c879fac7401d"' | ||
BOSTON_VISION_ZERO_PED_FATALITIES = '"92f18923-d4ec-4c17-9405-4e0da63e1d6c"' | ||
|
||
|
||
def _boston_vision_zero_ped_accidents(resource_id: str) -> gpd.GeoDataFrame: | ||
params = {"sql": "SELECT * from " + resource_id + " WHERE mode_type = 'ped'"} | ||
response = requests.get(BOSTON_VISION_ZERO_URL, params=params) | ||
response.raise_for_status() | ||
data = response.json()["result"]["records"] | ||
gdf = gpd.GeoDataFrame.from_records(data) | ||
gdf.drop(["_full_text"], axis=1, inplace=True) | ||
gdf.set_geometry( | ||
gpd.points_from_xy(gdf["long"], gdf["lat"]), inplace=True, crs="EPSG:4326" | ||
) | ||
return gdf | ||
|
||
|
||
def boston_vision_zero_ped_accidents(): | ||
gdf_crashes = _boston_vision_zero_ped_accidents(BOSTON_VISION_ZERO_PED_CRASHES) | ||
gdf_fatalities = _boston_vision_zero_ped_accidents( | ||
BOSTON_VISION_ZERO_PED_FATALITIES | ||
) | ||
gdf_crashes["is_fatal"] = False | ||
gdf_crashes.rename(columns={"dispatch_ts": "date_time"}, inplace=True) | ||
gdf_fatalities["is_fatal"] = True | ||
return pd.concat([gdf_crashes, gdf_fatalities], ignore_index=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import os | ||
import time | ||
|
||
import folium | ||
|
||
from night_light.past_accidents.vision_zero import boston_vision_zero_ped_accidents | ||
from night_light.utils import ( | ||
create_folium_map, | ||
LAYER_STYLE_DICT, | ||
LAYER_HIGHLIGHT_STYLE_DICT, | ||
Tooltip, | ||
) | ||
from night_light.utils.mapping import open_html_file | ||
from tests.conftest import BOSTON_CENTER_COORD | ||
|
||
|
||
def test_boston_vision_zero_map(): | ||
"""Test creating a map of the Boston Vision Zero accidents""" | ||
boston_vision_zero_accidents = boston_vision_zero_ped_accidents() | ||
map_filename = "test_boston_vision_zero.html" | ||
accidents_layer = folium.GeoJson( | ||
boston_vision_zero_accidents, | ||
name="Vision Zero Accidents", | ||
style_function=lambda x: LAYER_STYLE_DICT, | ||
highlight_function=lambda x: LAYER_HIGHLIGHT_STYLE_DICT, | ||
smooth_factor=2.0, | ||
tooltip=Tooltip( | ||
fields=["_id", "is_fatal"], | ||
aliases=["Accident ID", "Fatality"], | ||
max_width=800, | ||
), | ||
) | ||
create_folium_map( | ||
layers=[accidents_layer], | ||
zoom_start=12, | ||
center=BOSTON_CENTER_COORD, | ||
map_filename=map_filename, | ||
) | ||
assert os.path.exists(map_filename) | ||
open_html_file(map_filename) | ||
time.sleep(1) | ||
os.remove(map_filename) | ||
assert not os.path.exists(map_filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
|
||
from night_light.past_accidents.vision_zero import boston_vision_zero_ped_accidents | ||
from night_light.utils import query_geojson | ||
|
||
|
||
def test_query_boston_vision_zero_ped_accidents(): | ||
gdf_accidents = boston_vision_zero_ped_accidents() | ||
assert not gdf_accidents.empty | ||
assert "mode_type" in gdf_accidents.columns | ||
assert gdf_accidents.geometry.geom_type.unique() == "Point" | ||
assert gdf_accidents.crs == "EPSG:4326" | ||
assert gdf_accidents["mode_type"].unique() == "ped" | ||
assert not gdf_accidents["lat"].isnull().any() | ||
assert not gdf_accidents["long"].isnull().any() | ||
assert not gdf_accidents["is_fatal"].isnull().any() | ||
|
||
|
||
def test_save_boston_vision_zero_ped_accidents(): | ||
"""Test saving the Boston Vision Zero accidents data to a GeoJSON file""" | ||
gdf_accidents = boston_vision_zero_ped_accidents() | ||
geojson_filename = "test_boston_vision_zero.geojson" | ||
|
||
query_geojson.save_geojson(gdf_accidents, geojson_filename) | ||
saved_gdf = query_geojson.gpd.read_file(geojson_filename) | ||
|
||
assert gdf_accidents.crs == saved_gdf.crs | ||
assert set(gdf_accidents.columns) == set(saved_gdf.columns) | ||
assert gdf_accidents.index.equals(saved_gdf.index) | ||
assert gdf_accidents.shape == saved_gdf.shape | ||
|
||
os.remove(geojson_filename) | ||
assert not os.path.exists(geojson_filename) |