Skip to content

Instantly share code, notes, and snippets.

@anthonynorth
Last active March 21, 2022 22:44
Show Gist options
  • Save anthonynorth/bb018981775758c44aa2101166b26646 to your computer and use it in GitHub Desktop.
Save anthonynorth/bb018981775758c44aa2101166b26646 to your computer and use it in GitHub Desktop.
gb rivers
library(dplyr)
library(sf)
if (!file.exists("rivers_gb.rds")) {
download.file(
"https://beaver-net-app.s3.eu-west-2.amazonaws.com/gb_rivers/rivers_gb.rds",
"rivers_gb.rds"
)
}
# raw data
gb_rivers <- readRDS("rivers_gb.rds") |>
as_tibble() |>
janitor::clean_names()
# remove attributes and reduce precision of drain_area (round to 0.5)
gb_rivers_compact <- gb_rivers |>
select(drain_area, geom) |>
mutate(
# round to nearest 0.5
drain_area = round(drain_area / 0.5) * 0.5
)
# merge adjacent lines with identical features
# less featues = smaller tiles
# note: tippecanoe can also coalesce features (which I have enabled), but this works better
gb_rivers_coords <- st_coordinates(gb_rivers_compact$geom) |>
as_tibble(.name_repair = ~ c("lon", "lat", "id")) |>
inner_join(
transmute(gb_rivers_compact, drain_area, id = row_number()),
by = "id"
)
gb_rivers_mls <- gb_rivers_coords |>
group_by(drain_area) |>
mutate(group_id = cur_group_id()) |>
ungroup() |>
arrange(group_id) |>
sfheaders::sf_multilinestring(
x = "lon",
y = "lat",
linestring_id = "id",
multilinestring_id = "group_id",
keep = TRUE
) |>
st_set_crs(st_crs(gb_rivers$geom)) |>
st_line_merge() |>
select(id = group_id, drain_area, geometry)
# ready for tippecanoe
st_write(
obj = gb_rivers_mls,
driver = "GeoJSON",
dsn = "gb-rivers-compact.geojson",
delete_dsn = TRUE,
layer_options = c("RFC7946=YES", "ID_FIELD=id")
)
library(rdeck)
rdeck(
map_style = mapbox_dark(),
# pre-computed bounds
initial_bounds = sf::st_bbox(
c(xmin = -6.975690, ymin = 49.975074, xmax = 1.739668, ymax = 58.686302),
crs = 4326
),
picking_radius = 2,
layer_selector = TRUE
) |>
add_mvt_layer(
name = "Upstream Catchment Area",
# source data from https://beaver-net-app.s3.eu-west-2.amazonaws.com/gb_rivers/rivers_gb.rds
data = tile_json("anthony-north.49q7uoku"),
get_line_color = scale_color_threshold(
col = drain_area,
palette = viridis::magma,
# pre-computed quantile breaks & limits
breaks = c(0, 1.11, 3.1, 7.98, 31.79, 10378.65),
limits = c(0, 10378.65)
),
get_line_width = width_m,
line_width_min_pixels = 0.75,
pickable = TRUE,
auto_highlight = TRUE,
tooltip = c(drain_area, width_m),
)
library(rdeck)
rdeck(
map_style = mapbox_dark(),
# pre-computed bounds
initial_bounds = sf::st_bbox(
c(xmin = -6.975690, ymin = 49.975074, xmax = 1.739668, ymax = 58.686302),
crs = 4326
),
picking_radius = 2,
layer_selector = TRUE
) |>
add_mvt_layer(
name = "Upstream Catchment Area (compact)",
# source data from https://beaver-net-app.s3.eu-west-2.amazonaws.com/gb_rivers/rivers_gb.rds
data = tile_json("anthony-north.2izrjjdl"),
get_line_color = scale_color_threshold(
col = drain_area,
palette = viridis::magma,
# pre-computed quantile breaks & limits
breaks = c(0, 1.11, 3.1, 7.98, 31.79, 10378.65),
limits = c(0, 10378.65)
),
get_line_width = 15,
line_width_min_pixels = 0.75,
pickable = TRUE,
auto_highlight = TRUE,
tooltip = drain_area,
)
@anthonynorth
Copy link
Author

Tippecanoe command:

tippecanoe gb-rivers.geojson -o gb-rivers.mbtiles --layer=gb-rivers --read-parallel --no-feature-limit --reorder --coalesce --reverse --hilbert --simplification=10 -z12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment