Skip to content

Instantly share code, notes, and snippets.

@dakvid
Created December 2, 2021 10:13
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 dakvid/78c2dea79b828c45295144c4a883db88 to your computer and use it in GitHub Desktop.
Save dakvid/78c2dea79b828c45295144c4a883db88 to your computer and use it in GitHub Desktop.
#30DayMapChallenge 2021 - Day 05 - OSM
# Create a map of streets in Hamilton, New Zealand
# coloured by the type of street name.
# For #30DayMapChallenge 2021 - Day 05 - OpenStreetMap
# (Whilst I'm not getting the data from OSM, the LINZ
# data is loaded into OSM because of its open licence,
# and I've used OSM data for other maps :-)
# -- David Friggens, 2 December 2021
library(dplyr)
library(stringr)
library(sf)
library(ggplot2)
library(Manu)
library(ggtext)
library(glue)
FONT <- "Rye"
# Read data --------------------------------------------------------------
# From https://data.linz.govt.nz/layer/53382-nz-roads-addressing/
roads <-
read_sf("roads/nz-roads-addressing.gpkg")
roads <-
roads %>%
mutate(
road_name_type =
case_when(
!is.na(road_name_type) ~ road_name_type,
full_road_name %>% str_detect("^State Highway") ~ "Highway",
full_road_name %>% str_detect("^Ara[ -]|^Te Ara ") ~ "Ara",
full_road_name %>% str_detect("^Rue ") ~ "Rue",
full_road_name %>% str_detect("^Access Road|^Main Road") ~ "Road",
TRUE ~ full_road_name %>% str_replace(" East$| West$| North$| South$| [0-9]+$", "") %>% str_extract("[^ ]+$")
)
)
# From https://datafinder.stats.govt.nz
urban_areas <-
read_sf("statsnz/urban-rural-2021-clipped-generalised.gpkg") %>%
select(area_code = UR2021_V1_00,
area_name = UR2021_V1_00_NAME,
urban_type = IUR2021_V1_00_NAME) %>%
filter(urban_type %>% str_detect("rban"))
roads <-
roads %>%
st_transform(crs = st_crs(urban_areas))
# Hamilton ----------------------------------------------------------------
hamilton <-
urban_areas %>%
filter(area_name == "Hamilton")
hamilton_roads <-
hamilton %>%
st_intersection(roads)
hamilton_top_types <-
hamilton_roads %>%
st_drop_geometry() %>%
filter(!is.na(road_name_type)) %>%
count(road_name_type) %>%
arrange(desc(n)) %>%
head(6) %>%
pull(road_name_type)
hamilton_roads <-
hamilton_roads %>%
mutate(road_name_type = if_else(is.na(road_name_type) |
! road_name_type %in% hamilton_top_types,
"Other",
road_name_type))
PALETTE <- get_pal("Pohutukawa")
COL_BG <- "grey80"
hamilton_title <-
tibble(x = 1806465, y = 5825200,
label = "The Streets of Hamilton, NZ")
hamilton_legend <-
tibble(x = 1806465, y = 5824000,
label = glue("Where would you find<br>",
"a <span style='color:{PALETTE[1]};'>{hamilton_top_types[1]}</span>,<br>",
"<span style='color:{PALETTE[2]};'>{hamilton_top_types[2]}</span>,<br>",
"<span style='color:{PALETTE[3]};'>{hamilton_top_types[3]}</span>,<br>",
"<span style='color:{PALETTE[4]};'>{hamilton_top_types[4]}</span>,<br>",
"<span style='color:{PALETTE[5]};'>{hamilton_top_types[5]}</span>,<br>",
"or a <span style='color:{PALETTE[6]};'>{hamilton_top_types[6]}</span>?",
))
hamilton_data_note <-
tibble(x = 1792600, y = 5808800,
label = "#30DayMapChallenge 2021 - Day 05 - OpenStreetMap<br>Data from Land Information NZ<br>(which can also be sourced from OSM)")
hamilton_credit <-
tibble(x = 1806465, y = 5808800,
label = "David Friggens @dakvid")
gg_hamilton <-
ggplot() +
geom_sf(data = hamilton,
fill = "white",
colour = "black", size = 2) +
geom_sf(data = hamilton_roads %>% filter(road_name_type == "Other"),
size = 0.8,
colour = COL_BG) +
geom_sf(data = hamilton_roads %>% filter(road_name_type != "Other") %>% mutate(road_name_type = factor(road_name_type, levels = hamilton_top_types)),
size = 1,
aes(colour = road_name_type)) +
scale_colour_manual(values = PALETTE) +
geom_richtext(data = hamilton_title,
aes(x = x, y = y, label = label),
hjust = 1, vjust = 1, family = FONT, label.colour = NA,
size = 24,
fill = NA, colour = "black") +
geom_richtext(data = hamilton_legend,
aes(x = x, y = y, label = label),
hjust = 1, vjust = 1, family = FONT, label.colour = NA,
size = 14,
fill = NA, colour = "black") +
geom_richtext(data = hamilton_data_note,
aes(x = x, y = y, label = label),
hjust = 0, vjust = 0, family = FONT, label.colour = NA,
size = 8,
fill = NA, colour = "black") +
geom_richtext(data = hamilton_credit,
aes(x = x, y = y, label = label),
hjust = 1, vjust = 0, family = FONT, label.colour = NA,
size = 8,
fill = NA, colour = "black") +
coord_sf(datum = NA) +
theme_void() +
theme(plot.background = element_rect(fill = COL_BG, colour = NA),
panel.background = element_rect(fill = COL_BG, colour = NA),
legend.position = "none")
ggsave(plot = gg_hamilton,
"Day_05/Day_05_hamilton_street_colours.png",
width = 20, height = 23)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment