Skip to content

Instantly share code, notes, and snippets.

@szechno
Created November 27, 2025 15:39
Show Gist options
  • Select an option

  • Save szechno/90850e775a6305179b91cc8d398aef09 to your computer and use it in GitHub Desktop.

Select an option

Save szechno/90850e775a6305179b91cc8d398aef09 to your computer and use it in GitHub Desktop.
# https://wikishire.co.uk/lookup/
# https://wikishire.co.uk/wiki/Sussex
# https://en.wikipedia.org/wiki/Rape_(county_subdivision)
# https://britishcountyflags.com/2020/09/14/_the-rapes-of-sussex/
library(sf)
library(dplyr)
library(mapgl)
library(units)
wsx <- read_sf("./data/SussexHundredsA/SussexHundredsA.shp")
flag_wsx <- "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a3/FlagOfSussex.PNG/330px-FlagOfSussex.PNG"
flag_chi <- "https://britishcountyflags.com/wp-content/uploads/2016/11/1.png"
flag_arundel <- "https://britishcountyflags.com/wp-content/uploads/2016/11/2.png"
flag_bramber <- "https://britishcountyflags.com/wp-content/uploads/2016/11/21.png"
flag_lewes <- "https://britishcountyflags.com/wp-content/uploads/2016/11/5.png?w=640"
flag_pevensey <- "https://britishcountyflags.com/wp-content/uploads/2016/11/6.png"
flag_hastings <- "https://britishcountyflags.com/wp-content/uploads/2016/11/61.png"
attrib <- paste0("<a href=\"https://wikishire.co.uk/wiki/Sussex\">",
"wikishire.co.uk</a><br />",
"<a href=\"https://britishcountyflags.com/2020/09/14/",
"_the-rapes-of-sussex/\">britishcountyflags.com</a><br />",
"<a href=\"https://en.wikipedia.org/wiki/Rape_",
"(county_subdivision)\">en.wikipedia.org</a><br />")
maplibre_view(wsx)
wsx$Rape |> table()
# Arundel Bramber Chichester Hastings Lewes Pevensey
# 5 12 7 13 11 19
# assign group id rather than row_number() using cur_group_id()
wsx <- wsx |> group_by(Rape) |> mutate(Rape_id = cur_group_id()) |> ungroup()
# fix issue with bramber st_is_valid(bramber)
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
wsx1 <- wsx |> st_make_valid()
# summarise as Rapes rather than Hundreds
wsx_rapes <- wsx1 |> group_by(Rape) |> summarise()
wsx_rapes <- wsx_rapes |> mutate(size = st_area(wsx_rapes) |> set_units("km^2"))
wsx_rapes <- wsx_rapes |> mutate(flag = case_when(
Rape == "Chichester" ~ flag_chi,
Rape == "Arundel" ~ flag_arundel,
Rape == "Bramber" ~ flag_bramber,
Rape == "Lewes" ~ flag_lewes,
Rape == "Pevensey" ~ flag_pevensey,
Rape == "Hastings" ~ flag_hastings
))
wsx_border <- wsx1 |> summarise() |> st_cast("LINESTRING")
wsx_border$name <- "border"
maplibre_view(wsx_border)
wsx_rapes_centers <- wsx_rapes |> st_centroid(of_largest_polygon = TRUE)
wsx_rapes_centers
rape_scale <- interpolate_palette(
data = wsx,
column = "Rape_id",
n = 6,
# palette = viridisLite::mako
colors = c("#0B0405FF", "#382A54FF", "#395D9CFF",
"#3497A9FF", "#60CEACFF", "#DEF5E5FF")
)
# darken Pevensey text
rape_scale1 <- interpolate_palette(
data = wsx,
column = "Rape_id",
n = 6,
# palette = viridisLite::mako + darken https://mdigi.tools/darken-color/
colors = c("#0B0405FF", "#382A54FF", "#1b2c49FF",
"#18474fFF", "#216c55FF", "#298846")
)
maplibre(style = carto_style("positron")) |>
fit_bounds(wsx_rapes_centers) |>
add_fill_layer(id = "Rapes",
source = wsx,
fill_color = rape_scale$expression,
fill_opacity = 0.5,
fill_outline_color = "white",
popup = "popup",
tooltip = "Name",
hover_options = list(
fill_opacity = 1
)) |>
add_symbol_layer(id = "names_hundreds",
source = wsx,
text_field = get_column("Name"),
text_size = 8,
text_color = rape_scale1$expression,
text_halo_color = "white",
text_halo_width = 1) |>
add_line_layer(id = "border",
source = wsx_border) |>
add_image("flag_arundel",
url = "./images/flag_arundel.png") |>
add_symbol_layer(id = "Rape_title",
source = wsx_rapes_centers,
text_field = get_column("Rape"),
text_offset = c(0,2),
icon_image = "flag_arundel",
icon_allow_overlap = TRUE,
icon_size = 0.08,
filter = list("==", "Rape", "Arundel")
) |>
add_image("flag_bramber",
url = "./images/flag_bramber.png") |>
add_symbol_layer(id = "Rape_flag_bramber",
source = wsx_rapes_centers,
text_field = get_column("Rape"),
text_offset = c(0,2),
icon_image = "flag_bramber",
icon_allow_overlap = TRUE,
icon_size = 0.08,
filter = list("==", "Rape", "Bramber")) |>
add_image("flag_chichester",
url = "./images/flag_chichester.png") |>
add_symbol_layer(id = "Rape_flag_chichester",
source = wsx_rapes_centers,
text_field = get_column("Rape"),
text_offset = c(0,2),
icon_image = "flag_chichester",
icon_allow_overlap = TRUE,
icon_size = 0.08,
filter = list("==", "Rape", "Chichester")) |>
add_image("flag_hastings",
url = "./images/flag_hastings.png") |>
add_symbol_layer(id = "Rape_flag_hastings",
source = wsx_rapes_centers,
text_field = get_column("Rape"),
text_offset = c(0,2),
icon_image = "flag_hastings",
icon_allow_overlap = TRUE,
icon_size = 0.08,
filter = list("==", "Rape", "Hastings")) |>
add_image("flag_lewes",
url = "./images/flag_lewes.png") |>
add_symbol_layer(id = "Rape_flag_lewes",
source = wsx_rapes_centers,
filter = list("==", "Rape", "Lewes"),
text_field = get_column("Rape"),
text_offset = c(0,2),
icon_image = "flag_lewes",
icon_allow_overlap = TRUE,
icon_size = 0.08) |>
add_image("flag_pevensey",
url = "./images/flag_pevensey.png") |>
add_symbol_layer(id = "Rape_flag_pevensey",
source = wsx_rapes_centers,
text_field = get_column("Rape"),
text_offset = c(0,2),
icon_image = "flag_pevensey",
icon_allow_overlap = TRUE,
icon_size = 0.08,
filter = list("==", "Rape", "Pevensey")) |>
add_control(
html = paste0(
"<div style='font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif; ",
"background: rgba(255,255,255,0.95); padding: 16px 20px; border-radius: 8px; ",
"box-shadow: 0 2px 12px rgba(0,0,0,0.1); max-width: 430px;'>",
"<div style='font-size: 18px; font-weight: 700; color: #084081; line-height: 1.2;'>",
"The Rapes Of Sussex</div>",
"<div style='font-size: 12px; color: #666; font-weight: 500;'>",
"A rape is a traditional territorial sub-division of the county of Sussex in England, formerly used for various administrative purposes",
"<br />πš–πšŠπšŒπš”πšŠβ€€πšœπš£πšŽπšŒπš‘πš—πš˜<br />27 November 2025",
"</div>"
)
) |>
add_control(html = paste0(
"<div style='font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif; ",
"background: rgba(255,255,255,0.95); padding: 16px 20px; border-radius: 8px; ",
"box-shadow: 0 2px 12px rgba(0,0,0,0.1); max-width: 320px;'>",
"Attribution<hr />",
attrib,
"</div>"),
position = "bottom-left")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment