Created
November 24, 2021 04:45
-
-
Save dakvid/366a003f772470c2ff2bb1d539554ea5 to your computer and use it in GitHub Desktop.
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
# Create an interactive map of New Zealand's main administrative | |
# boundaries and their overlaps. | |
# For #30DayMapChallenge 2021 - Day 25 - Interactive | |
# -- David Friggens, November 2021 | |
# Setup ------------------------------------------------------------------- | |
library(glue) | |
library(stringr) | |
library(readr) | |
library(dplyr) | |
library(purrr) | |
library(sf) | |
library(rmapshaper) | |
library(leaflet) | |
library(htmltools) | |
library(htmlwidgets) | |
# both data sources from https://datafinder.stats.govt.nz | |
meshblocks <- | |
read_csv("statsnz/geographic-areas-table-2021.csv") %>% | |
select(REGC2021_code, REGC2021_name, | |
TA2021_code, TA2021_name, | |
CB2021_code, CB2021_name, | |
WARD2021_code, WARD2021_name, | |
DHB2015_code, DHB2015_name, | |
GED2020_code, GED2020_name, | |
MED2020_code, MED2020_name, | |
MB2021_code) | |
geo_mesh <- | |
st_read("statsnz/meshblock-2021-clipped-generalised.gpkg") %>% | |
select(MB2021_code = MB2021_V1_00) | |
# Prepare intersections --------------------------------------------------- | |
admin_areas <- | |
geo_mesh %>% | |
inner_join(meshblocks, | |
by = "MB2021_code") %>% | |
group_by(REGC2021_code, REGC2021_name, | |
TA2021_code, TA2021_name, | |
CB2021_code, CB2021_name, | |
WARD2021_code, WARD2021_name, | |
DHB2015_code, DHB2015_name, | |
GED2020_code, GED2020_name, | |
MED2020_code, MED2020_name) %>% | |
count() %>% | |
ungroup() | |
small_admin <- | |
admin_areas %>% | |
ms_simplify(keep = 0.05, keep_shapes = TRUE) | |
small_admin <- | |
small_admin %>% | |
st_transform(crs = "+proj=longlat +datum=WGS84") | |
small_admin <- | |
small_admin %>% | |
filter(TA2021_name != "Chatham Islands Territory") %>% | |
mutate(label_text = glue("<i>{n}</i> meshblocks<br>", | |
"<b>Region:</b> {if_else(REGC2021_code == '99', '<i>none</i>', REGC2021_name)}<br>", | |
"<b>Territorial Authority:</b> {if_else(TA2021_code == '999', '<i>none</i>', TA2021_name)}<br>", | |
"<b>{if_else(CB2021_code %>% str_detect('^076'), 'Local', 'Community')} Board:</b> ", | |
"{if_else(CB2021_code %>% str_detect('99$'), | |
'<i>none</i>', | |
CB2021_name %>% str_replace(' Community| Local Board Area', ''))}<br>", | |
"<b>Ward:</b> {if_else(WARD2021_code %>% str_detect('99$'), '<i>none</i>', WARD2021_name)}<br>", | |
"<b>District Health Board:</b> {DHB2015_name}<br>", | |
"<b>General Electorate:</b> {GED2020_name}<br>", | |
"<b>M\u0101ori Electorate:</b> {MED2020_name}") %>% map(HTML)) | |
# Prepare boundary layers ------------------------------------------------- | |
small_rc <- | |
small_admin %>% | |
filter(REGC2021_code != "99") %>% | |
group_by(REGC2021_code, REGC2021_name) %>% | |
summarise(num_ta = n_distinct(TA2021_code), | |
num_cb = n_distinct(CB2021_code), | |
num_ward = n_distinct(WARD2021_code), | |
num_dhb = n_distinct(DHB2015_code), | |
num_ged = n_distinct(GED2020_code), | |
num_med = n_distinct(MED2020_code)) %>% | |
ungroup() %>% | |
mutate(label_text = glue("<b>{REGC2021_name}</b><br>", | |
"territorial authorities - {num_ta} division{if_else(num_ta == 1, '', 's')}<br>", | |
"{if_else(REGC2021_code == '02', 'local', 'community')} boards - {num_cb} division{if_else(num_cb == 1, '', 's')}<br>", | |
"wards - {num_ward} division{if_else(num_ward == 1, '', 's')}<br>", | |
"district health boards - {num_dhb} division{if_else(num_dhb == 1, '', 's')}<br>", | |
"general electorates - {num_ged} division{if_else(num_ged == 1, '', 's')}<br>", | |
"M\u0101ori electorates - {num_med} division{if_else(num_med == 1, '', 's')}") %>% map(HTML)) | |
small_ta <- | |
small_admin %>% | |
filter(TA2021_code != "999") %>% | |
group_by(TA2021_code, TA2021_name) %>% | |
summarise(num_rc = n_distinct(REGC2021_code), | |
num_cb = n_distinct(CB2021_code), | |
num_ward = n_distinct(WARD2021_code), | |
num_dhb = n_distinct(DHB2015_code), | |
num_ged = n_distinct(GED2020_code), | |
num_med = n_distinct(MED2020_code)) %>% | |
ungroup() %>% | |
mutate(label_text = glue("<b>{TA2021_name}</b><br>", | |
"regions - {num_rc} division{if_else(num_rc == 1, '', 's')}", | |
"{if_else(TA2021_code == '999', 'local', 'community')} boards - {num_cb} division{if_else(num_cb == 1, '', 's')}<br>", | |
"wards - {num_ward} division{if_else(num_ward == 1, '', 's')}<br>", | |
"district health boards - {num_dhb} division{if_else(num_dhb == 1, '', 's')}<br>", | |
"general electorates - {num_ged} division{if_else(num_ged == 1, '', 's')}<br>", | |
"M\u0101ori electorates - {num_med} division{if_else(num_med == 1, '', 's')}") %>% map(HTML)) | |
small_cb <- | |
small_admin %>% | |
filter(CB2021_code %>% str_detect("99$", negate = TRUE)) %>% | |
group_by(CB2021_code, CB2021_name) %>% | |
summarise(num_rc = n_distinct(REGC2021_code), | |
num_ta = n_distinct(TA2021_code), | |
num_ward = n_distinct(WARD2021_code), | |
num_dhb = n_distinct(DHB2015_code), | |
num_ged = n_distinct(GED2020_code), | |
num_med = n_distinct(MED2020_code)) %>% | |
ungroup() %>% | |
mutate(label_text = glue("<b>{CB2021_name}</b><br>", | |
"regions - {num_rc} division{if_else(num_rc == 1, '', 's')}", | |
"territorial authorities - {num_ta} division{if_else(num_ta == 1, '', 's')}<br>", | |
"wards - {num_ward} division{if_else(num_ward == 1, '', 's')}<br>", | |
"district health boards - {num_dhb} division{if_else(num_dhb == 1, '', 's')}<br>", | |
"general electorates - {num_ged} division{if_else(num_ged == 1, '', 's')}<br>", | |
"M\u0101ori electorates - {num_med} division{if_else(num_med == 1, '', 's')}") %>% map(HTML)) | |
small_ward <- | |
small_admin %>% | |
filter(WARD2021_code %>% str_detect("99$", negate = TRUE)) %>% | |
group_by(WARD2021_code, WARD2021_name) %>% | |
summarise(num_rc = n_distinct(REGC2021_code), | |
num_ta = n_distinct(TA2021_code), | |
num_cb = n_distinct(CB2021_code), | |
num_dhb = n_distinct(DHB2015_code), | |
num_ged = n_distinct(GED2020_code), | |
num_med = n_distinct(MED2020_code)) %>% | |
ungroup() %>% | |
mutate(label_text = glue("<b>{WARD2021_name}</b><br>", | |
"regions - {num_rc} division{if_else(num_rc == 1, '', 's')}", | |
"territorial authorities - {num_ta} division{if_else(num_ta == 1, '', 's')}<br>", | |
"{if_else(WARD2021_code %>% str_detect('^076'), 'local', 'community')} boards - {num_cb} division{if_else(num_cb == 1, '', 's')}<br>", | |
"district health boards - {num_dhb} division{if_else(num_dhb == 1, '', 's')}<br>", | |
"general electorates - {num_ged} division{if_else(num_ged == 1, '', 's')}<br>", | |
"M\u0101ori electorates - {num_med} division{if_else(num_med == 1, '', 's')}") %>% map(HTML)) | |
small_dhb <- | |
small_admin %>% | |
group_by(DHB2015_code, DHB2015_name) %>% | |
summarise(num_rc = n_distinct(REGC2021_code), | |
num_ta = n_distinct(TA2021_code), | |
num_cb = n_distinct(CB2021_code), | |
num_ward = n_distinct(WARD2021_code), | |
num_ged = n_distinct(GED2020_code), | |
num_med = n_distinct(MED2020_code)) %>% | |
ungroup() %>% | |
mutate(label_text = glue("<b>{DHB2015_name}</b><br>", | |
"regions - {num_rc} division{if_else(num_rc == 1, '', 's')}", | |
"territorial authorities - {num_ta} division{if_else(num_ta == 1, '', 's')}<br>", | |
"{if_else(DHB2015_code %in% c('02','03','04'), 'local', 'community')} boards - {num_cb} division{if_else(num_cb == 1, '', 's')}<br>", | |
"wards - {num_ward} division{if_else(num_ward == 1, '', 's')}<br>", | |
"general electorates - {num_ged} division{if_else(num_ged == 1, '', 's')}<br>", | |
"M\u0101ori electorates - {num_med} division{if_else(num_med == 1, '', 's')}") %>% map(HTML)) | |
small_ged <- | |
small_admin %>% | |
group_by(GED2020_code, GED2020_name) %>% | |
summarise(num_rc = n_distinct(REGC2021_code), | |
num_ta = n_distinct(TA2021_code), | |
num_cb = n_distinct(CB2021_code), | |
num_ward = n_distinct(WARD2021_code), | |
num_dhb = n_distinct(DHB2015_code), | |
num_med = n_distinct(MED2020_code)) %>% | |
ungroup() %>% | |
mutate(label_text = glue("<b>{GED2020_name}</b><br>", | |
"regions - {num_rc} division{if_else(num_rc == 1, '', 's')}", | |
"territorial authorities - {num_ta} division{if_else(num_ta == 1, '', 's')}<br>", | |
"local/community boards - {num_cb} division{if_else(num_cb == 1, '', 's')}<br>", | |
"wards - {num_ward} division{if_else(num_ward == 1, '', 's')}<br>", | |
"district health boards - {num_dhb} division{if_else(num_dhb == 1, '', 's')}<br>", | |
"M\u0101ori electorates - {num_med} division{if_else(num_med == 1, '', 's')}") %>% map(HTML)) | |
small_med <- | |
small_admin %>% | |
group_by(MED2020_code, MED2020_name) %>% | |
summarise(num_rc = n_distinct(REGC2021_code), | |
num_ta = n_distinct(TA2021_code), | |
num_cb = n_distinct(CB2021_code), | |
num_ward = n_distinct(WARD2021_code), | |
num_dhb = n_distinct(DHB2015_code), | |
num_ged = n_distinct(GED2020_code)) %>% | |
ungroup() %>% | |
mutate(label_text = glue("<b>{MED2020_name}</b><br>", | |
"regions - {num_rc} division{if_else(num_rc == 1, '', 's')}", | |
"territorial authorities - {num_ta} division{if_else(num_ta == 1, '', 's')}<br>", | |
"local/community boards - {num_cb} division{if_else(num_cb == 1, '', 's')}<br>", | |
"wards - {num_ward} division{if_else(num_ward == 1, '', 's')}<br>", | |
"district health boards - {num_dhb} division{if_else(num_dhb == 1, '', 's')}<br>", | |
"general electorates - {num_ged} division{if_else(num_ged == 1, '', 's')}") %>% map(HTML)) | |
# Leaflet ----------------------------------------------------------------- | |
nz_map <- | |
leaflet() %>% | |
addTiles() %>% | |
addMapPane("borders", zIndex = 400) %>% | |
addMapPane("intersections", zIndex = 500) %>% | |
addPolygons(data = small_rc, | |
label = ~label_text, | |
opacity = 1, color = "blue", weight = 4, | |
fillOpacity = 0.1, fillColor = "blue", | |
group = "Regions", options = pathOptions(pane = "borders"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
addPolygons(data = small_ta, | |
label = ~label_text, | |
opacity = 1, color = "blue", weight = 4, | |
fillOpacity = 0.1, fillColor = "blue", | |
group = "Territorial Authorities", options = pathOptions(pane = "borders"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
addPolygons(data = small_cb, | |
label = ~label_text, | |
opacity = 1, color = "blue", weight = 4, | |
fillOpacity = 0.1, fillColor = "blue", | |
group = "Local/Community Boards", options = pathOptions(pane = "borders"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
addPolygons(data = small_ward, | |
label = ~label_text, | |
opacity = 1, color = "blue", weight = 4, | |
fillOpacity = 0.1, fillColor = "blue", | |
group = "Wards", options = pathOptions(pane = "borders"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
addPolygons(data = small_dhb, | |
label = ~label_text, | |
opacity = 1, color = "blue", weight = 4, | |
fillOpacity = 0.1, fillColor = "blue", | |
group = "District Health Boards", options = pathOptions(pane = "borders"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
addPolygons(data = small_ged, | |
label = ~label_text, | |
opacity = 1, color = "blue", weight = 4, | |
fillOpacity = 0.1, fillColor = "blue", | |
group = "General Electorates", options = pathOptions(pane = "borders"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
addPolygons(data = small_med, | |
label = ~label_text, | |
opacity = 1, color = "blue", weight = 4, | |
fillOpacity = 0.1, fillColor = "blue", | |
group = "M\u0101ori Electorates", options = pathOptions(pane = "borders"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
addPolygons(data = small_admin, | |
label = ~label_text, | |
opacity = 0.8, color = "black", weight = 2, | |
fillOpacity = 0.1, fillColor = "white", | |
group = "Intersections", options = pathOptions(pane = "intersections"), | |
highlightOptions = highlightOptions( | |
weight = 3, color = "white", opacity = 1, | |
fillColor = "black", fillOpacity = 0.8 | |
)) %>% | |
hideGroup("Territorial Authorities") %>% | |
hideGroup("Local/Community Boards") %>% | |
hideGroup("Wards") %>% | |
hideGroup("District Health Boards") %>% | |
hideGroup("General Electorates") %>% | |
hideGroup("M\u0101ori Electorates") %>% | |
addLayersControl( | |
baseGroups = c("Regions", | |
"Territorial Authorities", | |
"Local/Community Boards", | |
"Wards", | |
"District Health Boards", | |
"General Electorates", | |
"M\u0101ori Electorates"), | |
overlayGroups = "Intersections", | |
position = "topleft", | |
options = layersControlOptions(collapsed = FALSE) | |
) | |
saveWidget(nz_map, | |
"Day_25/Day_25_nz_boundaries.html", | |
selfcontained = TRUE, | |
title = "#30DayMapChallenge 2021 - Day 25 - Interactive - David Friggens @dakvid") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment