Distance to major cities


Computing minimum distance to cells with at least one thousand residential population.

Load packages, read data and source custom scripts

rm(list = ls())
library(raster)
#> Loading required package: sp
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.7.1, GDAL 2.4.0, PROJ 5.2.0
library(pdist)

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")
path_src <- file.path(path_proj, "src")

source(file.path(path_src, "stars-chunk-dist.R"))

path_pop <- file.path(path_raw, "population")

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
#> CRS:            27700
pop <- read_stars(file.path(path_cleaned, "pop_res_2011.tif"))

Compute distance to major cities

A cell with more than 1000 people is considered part of a major city.

majorcities <- pop
majorcities[majorcities < 1000] <- NA

dist_cities <- st_distance_stars(pop, majorcities, chunk_size = c(100, 100),
                                 buffer_size = c(110, 110))

write_stars(dist_cities, file.path(path_processed, "pop_dist_cities.tif"))

Visualize distance to major cities

dist_cities <- read_stars(file.path(path_processed, "pop_dist_cities.tif"), proxy = TRUE)

uk_aux <- st_transform(uk, st_crs(dist_cities))
plot(dist_cities, reset = FALSE, main = "Distance to Major Cities")
plot(uk_aux, add = TRUE, border = 2)

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#> 217.626  48.578 266.390