Distance to roads or waterways

Code
library(terra)
library(sf)
library(osmdata)
library(ggplot2)

Roads

Distance to roads was calculated in meters using the OpenStreetMap dataset, including all road types such as footways and paths, and generated at a spatial resolution of 3 arc-seconds.

Code
# get bounding box in the right format
area <- list.files("data/umrbpl/", full.names = TRUE, pattern = "shp$") |>
  read_sf() |>
  st_transform(crs = "EPSG:4326")
umrbpl_bb <- area |>
  ext() |>
  matrix(ncol = 2, byrow = TRUE)
rownames(umrbpl_bb) <- c("x", "y")
colnames(umrbpl_bb) <- c("min", "max")

# download road network from OSM
roads_all <- umrbpl_bb |>
  opq() |>
  add_osm_feature(key = "highway") |>
  osmdata_sf()
roads_all$osm_lines <- subset(roads_all$osm_lines, highway != "residential")

template_raster <- rast("data/srtm/topo_variables.tif")[[1]]
dist_to_roads <- distance(template_raster, roads$osm_lines)
if (!dir.exists("data/osm")) dir.create("data/osm")
writeRaster(dist_to_roads, "data/osm/dist_to_road.tif")
Code
"data/osm/dist_to_road.tif" |>
  rast() |>
  plot()

Waterways

Distance to roads was calculated in meters using the OpenStreetMap dataset, including all road types such as footways and paths, and generated at a spatial resolution of 3 arc-seconds.

Code
# get bounding box in the right format
area <- list.files("data/umrbpl/", full.names = TRUE, pattern = "shp$") |>
  read_sf() |>
  st_transform(crs = "EPSG:4326")
umrbpl_bb <- area |>
  ext() |>
  matrix(ncol = 2, byrow = TRUE)
rownames(umrbpl_bb) <- c("x", "y")
colnames(umrbpl_bb) <- c("min", "max")

# download road network from OSM
water_all <- umrbpl_bb |>
  opq() |>
  add_osm_feature(key = "waterway") |>
  osmdata_sf()

template_raster <- rast("data/srtm/topo_variables.tif")[[1]]
dist_to_water <- distance(template_raster, water_all$osm_lines)
if (!dir.exists("data/osm")) dir.create("data/osm")
writeRaster(dist_to_water, "data/osm/dist_to_water.tif", overwrite = TRUE)
Code
"data/osm/dist_to_water.tif" |>
  rast() |>
  plot()