Population and distance to major cities at 1 km


Simple re-projection of population and distance to major cities to UK grid at 1km resolution using st_warp.

Load packages, read data and source custom scripts

rm(list = ls())
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(purrr)

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_processed <- file.path(path_data, "processed")

uk <- st_read(file.path(path_cleaned, "uk_simple.gpkg"), "union_buffer")
#> Reading layer `union_buffer' from data source `/home/rstudio/Documents/Projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 0 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -5115.571 ymin: 2055.298 xmax: 660644.8 ymax: 1223625
#> projected CRS:  OSGB 1936 / British National Grid
uk_bbox_1km <- read_stars(file.path(path_processed, "uk_bbox_1km.tif"))

files <- c(file.path(path_cleaned, "pop_res_2011.tif"),
           file.path(path_processed, "pop_dist_cities.tif"))
pop_vars <- lapply(files, function(x) read_stars(x, proxy = TRUE))

Project urban variables into UK 1 km grid

pop_dist_1km <- lapply(pop_vars, function(x) st_warp(x, uk_bbox_1km))

varnames <- c("pop", "dist")
files_1km <- file.path(path_processed, paste0("uk_1km_", varnames, ".tif"))
walk2(pop_dist_1km, files_1km, ~ write_stars(.x, .y))

Visualize variables

pop_dist_1km <- lapply(files_1km, function(x) read_stars(x, proxy = TRUE))

for (i in seq_along(pop_dist_1km)) {
    plot(pop_dist_1km[[i]], reset = FALSE, main = varnames[i])
    plot(st_transform(uk, st_crs(pop_dist_1km[[i]])), add = TRUE, border = 2)
}

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#>  17.661   0.550  18.167