Created
February 20, 2026 21:07
-
-
Save szechno/344eabf7a6f9088eb330f3cbd97ab4c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| library(readr) | |
| library(dplyr) | |
| library(sf) | |
| library(mapgl) | |
| # download data here: https://roadtraffic.dft.gov.uk/downloads | |
| dft <- read_csv("dft_traffic_counts_aadf_by_direction.csv", | |
| col_types = cols( | |
| count_point_id = col_double(), | |
| year = col_double(), | |
| region_id = col_character(), | |
| region_name = col_character(), | |
| region_ons_code = col_character(), | |
| local_authority_id = col_character(), | |
| local_authority_name = col_character(), | |
| local_authority_code = col_character(), | |
| road_name = col_character(), | |
| road_category = col_character(), | |
| road_type = col_character(), | |
| start_junction_road_name = col_character(), | |
| end_junction_road_name = col_character(), | |
| easting = col_double(), | |
| northing = col_double(), | |
| latitude = col_double(), | |
| longitude = col_double(), | |
| link_length_km = col_double(), | |
| link_length_miles = col_double(), | |
| estimation_method = col_character(), | |
| estimation_method_detailed = col_character(), | |
| direction_of_travel = col_character(), | |
| pedal_cycles = col_double(), | |
| two_wheeled_motor_vehicles = col_double(), | |
| cars_and_taxis = col_double(), | |
| buses_and_coaches = col_double(), | |
| LGVs = col_double(), | |
| HGVs_2_rigid_axle = col_double(), | |
| HGVs_3_rigid_axle = col_double(), | |
| HGVs_4_or_more_rigid_axle = col_double(), | |
| HGVs_3_or_4_articulated_axle = col_double(), | |
| HGVs_5_articulated_axle = col_double(), | |
| HGVs_6_articulated_axle = col_double(), | |
| all_HGVs = col_double(), | |
| all_motor_vehicles = col_double() | |
| )) | |
| spec(dft) # provides col() as above which makes it easier to check and correct | |
| dft1 <- dft |> filter(local_authority_name == "West Sussex") | |
| rm(dft) | |
| # peel off latest year for each count site | |
| dft1 <- dft1 |> group_by(count_point_id) |> filter(year == max(year))|> ungroup() | |
| dft1 <- dft1 |> st_as_sf(coords = c("easting", "northing"), crs = 27700) | |
| dft1$direction_of_travel |> unique() | |
| # [1] "E" "W" "N" "S" "C" -- C == combined, ie unknown | |
| dft1 <- dft1 |> mutate(dot = case_when( | |
| direction_of_travel == "N" ~ 0, | |
| direction_of_travel == "E" ~ 90, | |
| direction_of_travel == "S" ~ 180, | |
| direction_of_travel == "W" ~ 270, | |
| .default = 0 | |
| ) | |
| ) | |
| maplibre() |> | |
| fit_bounds(dft1) |> | |
| add_image( | |
| id = "direction_sprite", | |
| url = "https://maprva.org/img/surveillance-direction.png" | |
| ) |> | |
| add_circle_layer( | |
| id = "site", | |
| source = dft1, | |
| circle_color = "red", | |
| # popup = get_column("all_motor_vehicles") # NOTE: TODO combine totals to get actual total | |
| ) |> | |
| add_symbol_layer( | |
| id = "direction", | |
| source = dft1, | |
| icon_image = "direction_sprite", | |
| icon_allow_overlap = TRUE, | |
| icon_rotate = list( | |
| "to-number", | |
| get_column("dot") | |
| ), | |
| text_field = get_column("all_motor_vehicles"), | |
| # text_offset = c(0,1), | |
| text_offset = match_expr( | |
| column = "direction_of_travel", | |
| values = c("N", "E", "S", "W"), | |
| stops = list( | |
| list("literal", c(0, -2)), | |
| list("literal", c(2, 0)), | |
| list("literal", c(0, 2)), | |
| list("literal", c(-2, 0)) | |
| ), | |
| default = list(list("literal", c(0, 0))) | |
| ), | |
| text_allow_overlap = TRUE, | |
| filter = list( | |
| "!=", | |
| get_column("direction_of_travel"), | |
| "C" | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment