View source: R/calc_zoi_nearest.R
calc_zoi_nearest | R Documentation |
This function takes in a raster with locations or counts of
infrastructure and calculates a raster (or set of rasters, in case there is
more the one value for radius
) representing the zone of influence (ZOI)
from the neareast feature of that type of infrastructure. Zones of influence
are defined by functions that decay with the distance from each
infrastructure and their rate of decay is controlled by the ZOI radius
(radius
), which defines how far the influence of an infrastructure
feature goes. By default, the Gaussian decay ZOI is calculated, but other
decay shapes might be used (see zoi_functions()
for examples).
The function might also return the distance to the nearest feature
or a transformation from it (e.g. log- and sqrt-distance from the nearest
feature).
The procedure might be computed in both R and GRASS GIS. In GRASS, it
requires an active connection between the R session and a GRASS GIS
location and mapset (through the package rgrass), and that the input
maps are already loaded within this GRASS GIS mapset.
If the calculations are done in R, the input is a (set of) raster map(s)
and the function returns another (set of) raster map(s). If the calculations
are done within GRASS GIS, the input is the name of a raster map already
loaded in a GRASS GIS location and mapset, and the function returns
only the name of the output map. This map is stored in the GRASS GIS
location/mapset, and might be retrieved to R through the
rgrass::read_RAST()
function or exported outside GRASS using the
r.out.gdal
module, for instance.
calc_zoi_nearest(
x,
radius = NULL,
type = c("Gauss", "exp_decay", "bartlett", "half_norm", "threshold", "step",
"euclidean", "log", "sqrt")[1],
where = c("R", "GRASS")[1],
intercept = 1,
zoi_limit = 0.05,
lambda = NULL,
log_base = exp(1),
dist_offset = 0,
zeroAsNA = FALSE,
extent_x_cut = NULL,
extent_y_cut = NULL,
g_output_map_name = NULL,
g_dist_metric = c("euclidean", "geodesic", "squared", "maximum", "manhattan")[1],
g_input_as_region = FALSE,
g_remove_intermediate = TRUE,
g_print_expression = FALSE,
g_overwrite = FALSE,
verbose = FALSE,
...
)
x |
The input raster |
radius |
|
type |
See details below. Other options still to be implemented (such as other functions and a generic user-defined ZOI function as input). |
where |
|
intercept |
|
zoi_limit |
|
lambda |
|
log_base |
|
dist_offset |
|
zeroAsNA |
|
extent_x_cut , entent_y_cut |
|
g_output_map_name |
|
g_dist_metric |
|
g_input_as_region |
|
g_remove_intermediate |
|
g_print_expression |
|
g_overwrite |
|
verbose |
|
... |
|
In practice, the function calc_zoi_nearest()
first calculates the distance from each
pixel to the nearest feature and then transforms it according to the ZOI
functions. In R, calc_zoi_nearest()
makes use of the terra::distance()
function and the following procedures are made through raster algebra.
In GRASS, the module
r.grow.distance
is used to calculate the Euclidean distance from
the nearest feature and
r.mapcalc.simple
to transform the distance into the different ZOI of the nearest feature.
The input raster x
should have positive values in the pixels where
infrastructure are located and NA/no-data in all other places.
The input raster is supposed to
represent the location of point, line, or polygon infrastructure
(e.g. houses, roads, mining areas), but any landscape variable whose
representation might be one of those would fit here
(e.g. areas of forest or any other habitat type or land cover).
We recommend that the input raster has a metric projection, so that distances
and zones of influence are based on distance to infrastructure measured in meters.
If the calculations are performed in R (where = "R"
), the function
returns a RasterLayer
(or SpatRaster
, according to the class of the input
object) with the zone of influence of the nearest feature. If multiple values
of radius
are provided, a stack of rasters is returned.
If the calculations are performed in GRASS GIS (where = "GRASS"
),
the maps are kept only within the GRASS GIS location/mapset and the function
returns the name of the calculated maps.
If the computation is done in GRASS GIS, the output is name of
the output raster map(s) within the GRASS GIS location and mapset of the
current session. The user can retrieve these maps to R using
rgrass::read_RAST or export them outside GRASS using the
r.out.gdal
module, for instance.
See zoi_functions()
for some ZOI function shapes.
See also terra::distance()
for details on the calculation of the distance
to the nearest future in R.
See
r.grow.distance,
for details on the calculation of the distance to the nearest future in GRASS.
See calc_zoi_cumulative()
for the computation of the cumulative
zone of influence and density of multiple features.
# Running calc_zoi_nearest through R
library(terra)
library(sf)
# Load raster data
f <- system.file("raster/sample_area_cabins.tif", package = "oneimpact")
cabins <- terra::rast(f)
# Load vector data
f2 <- system.file("vector/sample_area_cabins.gpkg", package = "oneimpact")
cabins_vect <- sf::st_read(f2)
# calculate distance to the nearest feature
d <- calc_zoi_nearest(cabins, type = "euclidean")
plot(d)
# calculate log_dist (the rest is equal)
log_d <- calc_zoi_nearest(cabins, type = "log", log_base = 10)
plot(log_d)
# calculate sqrt_dist
sqrt_d <- calc_zoi_nearest(cabins, type = "sqrt")
plot(sqrt_d)
# calculate exponential decay zone of influence
# using exp_decay_parms parameter
exp_d1 <- calc_zoi_nearest(cabins, type = "exp_decay",
intercept = 1, lambda = 0.001)
plot(exp_d1)
# calculate exponential decay zone of influence using
# radius and zoi_limit (default)
radius2 <- 1000 # zoi = 1000m
zoi_limit2 <- 0.05 # here zoi is the distance where the function reaches 0.05
exp_d2 <- calc_zoi_nearest(cabins, type = "exp_decay", radius = radius2,
zoi_limit = zoi_limit2)
plot(exp_d2)
# buffer
# zoi = 1000m
cabins_vect |>
sf::st_buffer(dist = radius2) |>
sf::st_union() |>
plot(add = T, border = "black")
legend("bottomright", legend = c("ZoI radius"), col = c("black"), lwd = 1.1)
# calculate exponential decay zone of influence using half life parameter
# if half_life = 250 m and zoi_hl_ratio = 4, zoi is 1000 m
half_life3 <- 250 # intensity gets down to 1/16 = 0.06 for 4*half_life=1000m
zoi_hl_ratio3 <- 4 # default
exp_d4 <- calc_zoi_nearest(cabins, type = "exp_decay", half_life = half_life3,
zoi_hl_ratio = zoi_hl_ratio3)
plot(exp_d4)
# buffer
cabins_vect |>
sf::st_buffer(dist = half_life3) |>
sf::st_union() |>
plot(add = T, border = "red")
# zoi = 1000m
cabins_vect |>
sf::st_buffer(dist = half_life3*zoi_hl_ratio3) |>
sf::st_union() |>
plot(add = T, border = "black")
legend("bottomright", legend = c("Exponential half-life", "ZoI radius"),
col = c("red", "black"), lwd = 1.1)
# calculate exponential decay zone of influence using
# radius parameter and zoi_hl_ratio
radius4 <- 4000 # intensity gets down to 1/16 = 0.06 for zoi = 4000m, half_life = 1000m
zoi_hl_ratio4 <- 6 # default
exp_d4 <- calc_zoi_nearest(cabins, type = "exp_decay", radius = radius4,
zoi_hl_ratio = zoi_hl_ratio4)
plot(exp_d4)
# buffer
# half_life = 1000m
cabins_vect |>
sf::st_buffer(dist = radius4/zoi_hl_ratio4) |>
sf::st_union() |>
plot(add = T, border = "red")
# zoi = 4000m
cabins_vect |>
sf::st_buffer(dist = radius4) |>
sf::st_union() |>
plot(add = T, border = "black", )
legend("bottomright", legend = c("Exponential half-life", "ZoI radius"),
col = c("red", "black"), lwd = 1.1)
#---
# bartlett influence, ZOI = 1000m
bart_d <- calc_zoi_nearest(cabins, type = "bartlett", radius = 1000)
plot(bart_d)
# buffer 1000m
cabins_vect |>
sf::st_buffer(dist = 1000) |>
sf::st_union() |>
plot(add = T, border = "black")
legend("bottomright", legend = c("Bartlett ZoI 1000m"),
col = c("black"), lwd = 1.1)
# calculate threshold influence, ZoI = 1000m
d <- calc_zoi_nearest(cabins, type = "threshold", radius = 1000)
plot(d)
# Gaussian decay influence, ZoI = 1000m
g_d <- calc_zoi_nearest(cabins, type = "Gauss", radius = 1000)
plot(g_d)
# buffer 1000m
cabins_vect |>
sf::st_buffer(dist = 1000) |>
sf::st_union() |>
plot(add = T, border = "black")
legend("bottomright", legend = c("Gaussian ZoI 1000m"),
col = c("black"), lwd = 1.1)
#--------------------
# Running calc_zoi_nearest through GRASS GIS
library(rgrass)
library(terra)
# Load raster data
f <- system.file("raster/sample_area_cabins.tif", package = "oneimpact")
cabins <- terra::rast(f)
# connect to grass gis and create grass location
# For linux or within OSGeo4W shell
grassdir <- system("grass --config path", intern = TRUE)
# grassdir <- system("grass78 --config path", intern = TRUE) # for GRASS 7.8
# If you used the standalone installer in Windows
# grassdir <- "C:\Programs\GRASS GIS 7.8" # Correct if the path GRASS version or path is different
gisDB <- "." # create location and mapset in the working directory
loc <- "ETRS_33N/" # name of the location
ms <- "PERMANENT" # name of the mapset
rgrass::initGRASS(gisBase = grassdir,
SG = cabins, # use map to define location projection
home = tempdir(),
override = TRUE,
gisDbase = gisDB,
location = loc,
mapset = ms)
# define map name within GRASS GIS
cabins_g <- "cabins_example"
# add file to GRASS GIS mapset
rgrass::write_RAST(cabins, cabins_g, flags = c("overwrite", "o"))
# check
terra::plot(cabins, col = "black",
main = "Map of tourist cabins")
#---
# define region in GRASS GIS
rgrass::execGRASS("g.region", raster = cabins_g,
flags = "p")
# Input map name within GRASS GIS
cabins_g
# Exponential decay ZoI=1000m
expdecay_name <- calc_zoi_nearest(cabins_g, type = "exp_decay",
radius = 1000,
where = "GRASS",
g_verbose = FALSE, g_overwrite = TRUE)
# Bartlett decay ZoI=1000m
bartlett_name <- calc_zoi_nearest(cabins_g, type = "bartlett",
radius = 1000,
where = "GRASS", g_verbose = FALSE, g_overwrite = TRUE)
# Threshold influence ZoI = 1000m
threshold_name <- calc_zoi_nearest(cabins_g, type = "threshold",
radius = 1000,
where = "GRASS", g_verbose = FALSE, g_overwrite = TRUE)
# Gaussian influence ZoI = 1000m
gaussian_name <- calc_zoi_nearest(cabins_g, type = "Gauss",
radius = 1000,
where = "GRASS", g_verbose = FALSE, g_overwrite = TRUE)
# Log-distance
log_name <- calc_zoi_nearest(cabins_g, type = "log", log_base = 10,
where = "GRASS",
g_verbose = FALSE, g_overwrite = TRUE)
# Euclidean
euclidean_name <- calc_zoi_nearest(cabins_g, type = "euclidean",
where = "GRASS",
g_verbose = FALSE, g_overwrite = TRUE)
(all_names <- c(euclidean_name, log_name, expdecay_name,
bartlett_name, threshold_name, gaussian_name))
# visualize
cabins_zoi_nearest <- rgrass::read_RAST(all_names, return_format = "terra")
title_plot <- c("Euclidean distance", "Log distance (base 10)",
"Exponential ZoI 1000m", "Bartlett ZoI 1000m",
"Threshold ZoI 1000m", "Gaussian ZoI 1000m")
terra::plot(cabins_zoi_nearest, main = title_plot)
# remove rasters created
# to_remove_rast <- c(all_names)
# rgrass::execGRASS("g.remove", type = "rast", name = to_remove_rast, flags = "f")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.