Residential population: clean missing values
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
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_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 <- raster(file.path(path_pop, "UK_residential_population_2011_1_km.tif"))
Visualize residential population on UK
uk_aux <- st_transform(uk, st_crs(crs(pop)))
par(mar = c(2, 2, 2, 0))
plot(uk_aux, graticule = TRUE, axes = TRUE, border = 2, col = rgb(1, 0, 0, 0.3),
main = "Residential Population")
plot(pop, col = grey(1:(11 - 1)/11), add = TRUE, maxpixels = 200000)
We can see that there is a good quantity of cells with missing values (red tiles). We
have to replace those missing values with 0
.
Fix missing values
pop[is.na(pop)] <- 0
uk_aux_sp <- as(uk_aux, "Spatial")
pop <- mask(pop, uk_aux_sp)
write_stars(st_set_crs(st_as_stars(pop), 27700), file.path(path_cleaned, "pop_res_2011.tif"))
Visualize cleaned data
pop <- read_stars(file.path(path_cleaned, "pop_res_2011.tif"), proxy = TRUE)
plot(pop, reset = FALSE, main = "Residential Population")
plot(uk, add = TRUE, border = 2)
Time to execute the task
Only useful when executed with Rscript
.
proc.time()
#> user system elapsed
#> 22.233 1.440 23.343