Skip to content

Instantly share code, notes, and snippets.

@KHwong12
Created December 26, 2023 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KHwong12/4e5d7e628ed8888cabe9cf511af323a7 to your computer and use it in GitHub Desktop.
Save KHwong12/4e5d7e628ed8888cabe9cf511af323a7 to your computer and use it in GitHub Desktop.
library(dplyr)
library(tidyr)
library(lubridate)
library(sf)
library(fst)
hk_collisions = hk_collisions %>%
# Replace "SSPO" district abbr in 2020 and 2021 data to "SSP"
mutate(district = ifelse(district == "SSPO", "SSP", district)) %>%
# Set collisions with unknown easting and northing values (`0` in raw data) to NA
# Also set long and lat to NA
mutate(
easting = ifelse(easting == 8e5, NA, easting),
northing = ifelse(easting == 8e5, NA, northing),
latitude = ifelse(easting == 8e5, NA, latitude),
longitude = ifelse(easting == 8e5, NA, longitude),
)
# Ensure timezone of raw data
lubridate::tz(hk_collisions$date_time) = "Asia/Hong_Kong"
# Get information of all types of vehicles involved in the accidents to show in popup
hk_vehicles_involved <- hk_vehicles %>%
group_by(serial_no) %>%
summarize(vehicle_class_involved = paste(sort(unique(vehicle_class)), collapse = ", "))
# Get casualty role involved in each accident to show in popup
casualty_role_n = hk_casualties %>% count(serial_no, casualty_role)
accidents_cas_type <- casualty_role_n %>%
pivot_wider(
id_cols = serial_no,
names_from = casualty_role,
values_from = n, values_fill = 0
) %>%
rename(cas_ped_n = Pedestrian, cas_pax_n = Passenger, cas_dvr_n = Driver)
# Add date floored to first day of the month for easier month filter handling
collisions_new <- mutate(hk_collisions, year_month = floor_date_to_month(date_time))
hk_collisions_join <- hk_collisions %>%
left_join(accidents_cas_type, by = "serial_no") %>%
left_join(hk_vehicles_involved, by = "serial_no") %>%
# Show full name of district in popup of maps
left_join(data.frame(DC_Abbr = DISTRICT_ABBR, DC_full_name = DISTRICT_FULL_NAME),
by = c("district" = "DC_Abbr"))
## ------- Collision Map data ---------
hk_collisions_valid <- filter(hk_collisions_join, !is.na(latitude) & !is.na(longitude))
# Leaflet default expect WGS84 (crs 4326), need custom CRS for HK1980 Grid (crs 2326)
# https://rstudio.github.io/leaflet/projections.html
hk_collisions_valid_sf <- st_as_sf(x = hk_collisions_valid, coords = c("longitude", "latitude"), crs = 4326, remove = FALSE)
write_fst(hk_collisions, "./inst/app/data/hk_collisions.fst")
st_write(hk_collisions_valid_sf, "./inst/app/data/data-manipulated/hk_collisions_valid_sf.gpkg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment