UK Countries: create simplified boundaries with buffers
This script creates useful boundaries for filtering UK-associate data such as:
- simplified (buffered) UK boundaries per country
- simplified (buffered) UK boundaries
- a (buffered) UK bounding box.
All the files are saved with crs = 27700
.
Load packages, read data, and source custom scripts
rm(list = ls())
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.4.0, PROJ 5.2.0
library(ggplot2)
library(units)
#> udunits system database from /usr/share/xml/udunits
path_proj <- day2day::git_path()
path_data <- file.path(path_proj, "data")
path_raw <- file.path(path_data, "raw")
path_cleaned <- file.path(path_data, "cleaned")
path_src <- file.path(path_proj, "src")
path_sf <- file.path(path_src, "sfaux")
devtools::load_all(path_sf)
#> Loading sfaux
path_uk <- file.path(path_raw, "boundaries",
"Countries_December_2017_Full_Clipped_Boundaries_UK_WGS84.shp")
uk <- st_read(path_uk)
#> Reading layer `Countries_December_2017_Full_Clipped_Boundaries_UK_WGS84' from data source `/home/rstudio/Documents/Projects/land-suitability/data/raw/boundaries/Countries_December_2017_Full_Clipped_Boundaries_UK_WGS84.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 4 features and 10 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -8.650007 ymin: 49.86468 xmax: 1.763546 ymax: 60.86077
#> CRS: 4326
uk_27700 <- st_transform(uk, crs = 27700)
Simplified (buffered) UK boundaries per country
buffer_dist <- set_units(5, "km")
uk_simple <- st_simplify(uk_27700, dTolerance = 500)
uk_buffer <- st_buffer(uk_simple, buffer_dist)
lwd <- 0.3
ggplot(uk_buffer) +
geom_sf(aes(col = "buffered"), fill = NA, size = lwd) +
geom_sf(aes(col = "simplified"), data = uk_simple, fill = NA, size = lwd) +
coord_sf(datum = 27700) +
labs(colour = "UK") +
theme_void()
Simplified and unified (buffered) UK boundaries
uk_union <- st_union(st_geometry(uk_simple))
uk_union <- st_union_unique(uk_union)
uk_union_buffer <- st_buffer(uk_union, set_units(5, "km"))
ggplot(uk_union_buffer) +
geom_sf(aes(col = "buffered"), fill = NA, size = lwd) +
geom_sf(aes(col = "simplified"), uk_union, fill = NA, size = lwd) +
coord_sf(datum = 27700) +
labs(colour = "UK union") +
theme_void()
A (buffered) UK bounding box
bbox <- st_as_sfc(st_bbox(uk_27700))
bbox_buffer <- st_as_sfc(st_bbox(uk_buffer))
ggplot(bbox_buffer) +
geom_sf(aes(col = "buffered"), fill = NA, size = lwd) +
geom_sf(aes(col = "original"), bbox, fill = NA, size = lwd) +
geom_sf(data = uk_union_buffer, col = "gray", size = lwd) +
coord_sf(datum = 27700) +
labs(colour = "Bounding box") +
theme_void()
Save new UK files
uk_simple_file <- file.path(path_cleaned, "uk_simple.gpkg")
st_write(uk_simple, uk_simple_file, delete_dsn = TRUE, layer = "simple")
#> Deleting source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Writing layer `simple' to data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Writing 4 features with 10 fields and geometry type Multi Polygon.
st_write(uk_buffer, uk_simple_file, delete_layer = TRUE, layer = "simple_buffer")
#> Deleting layer `simple_buffer' failed
#> Writing layer `simple_buffer' to data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Writing 4 features with 10 fields and geometry type Unknown (any).
st_write(uk_union, uk_simple_file, delete_layer = TRUE, layer = "union")
#> Deleting layer `union' failed
#> Writing layer `union' to data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Writing 1 features with 0 fields and geometry type Multi Polygon.
st_write(uk_union_buffer, uk_simple_file, delete_layer = TRUE, layer = "union_buffer")
#> Deleting layer `union_buffer' failed
#> Writing layer `union_buffer' to data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Writing 1 features with 0 fields and geometry type Multi Polygon.
st_write(bbox, uk_simple_file, delete_layer = TRUE, layer = "bbox")
#> Deleting layer `bbox' failed
#> Writing layer `bbox' to data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Writing 1 features with 0 fields and geometry type Polygon.
st_write(bbox_buffer, uk_simple_file, delete_layer = TRUE, layer = "bbox_buffer")
#> Deleting layer `bbox_buffer' failed
#> Writing layer `bbox_buffer' to data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Writing 1 features with 0 fields and geometry type Polygon.
Time to execute the task
Only useful when executed with Rscript
.
proc.time()
#> user system elapsed
#> 36.565 1.483 38.191