Skip to content

Commit b21f53a

Browse files
committed
2 parents 7f6bef6 + 6fbad10 commit b21f53a

13 files changed

Lines changed: 389 additions & 390 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ rsconnect/
6161
# pixi environments
6262
.pixi/*
6363
!.pixi/config.toml
64+
/RT

R/filter_save.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#'
1414
#' @param filt `[matrix]` \cr Filter or weight matrix, such as one created by
1515
#' [oneimpact::filter_create()] or [terra::focalMat()].
16-
#' @param radius `[numeric(1)]` \cr Radius of the Zone of Influence (ZoI) of
16+
#' @param radius `[numeric(1)]` \cr Radius of the Zone of Influence (ZOI) of
1717
#' the matrix, in meters.
1818
#' @param type `[character(1)]` Function for the kernel or filter matrix
1919
#' (see `type` parameter for [oneimpact::filter_create()]).

examples/calc_zoi_cumulative_example.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ density_exp <- calc_zoi_cumulative(cabins, type = "exp_decay", radius = 1000,
3939
# compare
4040
# note the difference in the color scales
4141
plot(c(cumzoi_exp[[3]], density_exp),
42-
main = c("Cumulative ZoI 1000m", "Density 1000m"))
42+
main = c("Cumulative ZOI 1000m", "Density 1000m"))
4343

4444
#--------------------
Lines changed: 155 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,155 @@
1-
# Running calc_zoi_cumulative through GRASS GIS
2-
library(rgrass)
3-
library(terra)
4-
5-
# Load raster data
6-
f <- system.file("raster/sample_area_cabins.tif", package = "oneimpact")
7-
cabins <- terra::rast(f)
8-
9-
# connect to grass gis and create grass location
10-
# For linux or within OSGeo4W shell
11-
grassdir <- system("grass --config path", intern = TRUE)
12-
# grassdir <- system("grass78 --config path", intern = TRUE) # for GRASS 7.8
13-
# If you used the standalone installer in Windows
14-
# grassdir <- "C:\\Programs\\GRASS GIS 7.8" # Correct if the path GRASS version or path is different
15-
16-
gisDB <- "." # create location and mapset in the working directory
17-
loc <- "ETRS_33N/" # name of the location
18-
ms <- "PERMANENT" # name of the mapset
19-
rgrass::initGRASS(gisBase = grassdir,
20-
SG = cabins, # use map to define location projection
21-
home = tempdir(),
22-
override = TRUE,
23-
gisDbase = gisDB,
24-
location = loc,
25-
mapset = ms)
26-
27-
# define map name within GRASS GIS
28-
cabins_g <- "cabins_example"
29-
# add file to GRASS GIS mapset
30-
rgrass::write_RAST(cabins, cabins_g, flags = c("o", "overwrite"))
31-
32-
# check
33-
terra::plot(cabins, col = "black",
34-
main = "Map of tourist cabins")
35-
36-
#---
37-
# define region in GRASS GIS
38-
rgrass::execGRASS("g.region", raster = cabins_g,
39-
flags = "p")
40-
41-
#---
42-
# Guarantee input map is binary (zeros as background)
43-
44-
# Input map name within GRASS GIS - binary map
45-
cabins_bin_g <- grass_binarize(cabins_g, breaks = 1, output = "cabins_example_bin",
46-
null = 0, overwrite = TRUE)
47-
48-
# check input
49-
cabins_bin <- rgrass::read_RAST("cabins_example_bin", return_format = "terra", NODATA = 255)
50-
51-
plot(cabins_bin, col = c("lightyellow", "black"),
52-
main = "Binarized map of cabins")
53-
54-
#---
55-
# Using 'r.mfilter' algorithm (default)
56-
57-
# Exponential decay
58-
exp_name <- calc_zoi_cumulative(x = cabins_bin_g,
59-
radius = 1000, zoi_limit = 0.01,
60-
type = "exp_decay",
61-
where = "GRASS",
62-
g_overwrite = TRUE,
63-
verbose = TRUE)
64-
# Bartlett decay
65-
barlett_name <- calc_zoi_cumulative(x = cabins_bin_g,
66-
radius = 1000,
67-
type = "bartlett",
68-
where = "GRASS",
69-
g_overwrite = TRUE,
70-
verbose = TRUE)
71-
# Gaussian decay
72-
gauss_name <- calc_zoi_cumulative(x = cabins_bin_g,
73-
radius = 1000, zoi_limit = 0.01,
74-
type = "Gauss",
75-
where = "GRASS",
76-
g_overwrite = TRUE,
77-
verbose = TRUE)
78-
79-
# Threshold decay (circle, step)
80-
threshold_name <- calc_zoi_cumulative(x = cabins_bin_g,
81-
radius = 1000,
82-
type = "threshold",
83-
where = "GRASS",
84-
g_overwrite = TRUE,
85-
verbose = TRUE)
86-
87-
(all_names <- c(exp_name, barlett_name, gauss_name, threshold_name))
88-
89-
# visualize
90-
cabins_zoi_cumulative <- rgrass::read_RAST(all_names, return_format = "terra")
91-
92-
title_plot <- c("Exponential decay 1000m", "Bartlett decay 1000m",
93-
"Gaussian decay 1000m", "Threshold decay 1000m")
94-
terra::plot(cabins_zoi_cumulative, main = title_plot)
95-
96-
#---
97-
# calculate density vs cumulative ZoI
98-
exp_name_d <- calc_zoi_cumulative(x = cabins_bin_g,
99-
radius = 1000, zoi_limit = 0.01,
100-
type = "exp_decay", output_type = "density",
101-
where = "GRASS",
102-
g_overwrite = TRUE,
103-
verbose = TRUE)
104-
105-
cabins_density <- rgrass::read_RAST(exp_name_d, return_format = "terra")
106-
107-
terra::plot(c(cabins_zoi_cumulative[[1]], cabins_density),
108-
main = c("Cumulative ZoI", "Density"))
109-
110-
#---
111-
# Using 'r.resamp.filter' algorithm
112-
113-
# rectangle
114-
rectangle_resamp_filt <- calc_zoi_cumulative(x = cabins_bin_g,
115-
radius = 1000,
116-
type = "box",
117-
output_type = "density",
118-
where = "GRASS",
119-
g_module = "r.resamp.filter",
120-
g_overwrite = TRUE,
121-
verbose = TRUE)
122-
rgrass::read_RAST(rectangle_resamp_filt, return_format = "terra") |>
123-
plot(main = "Rectangle ZoI 1000m")
124-
125-
# bartlett
126-
bartlett_resamp_filt <- calc_zoi_cumulative(x = cabins_bin_g,
127-
radius = 1000,
128-
type = "bartlett",
129-
output_type = "cumulative_zoi",
130-
where = "GRASS",
131-
g_module = "r.resamp.filter",
132-
g_overwrite = TRUE,
133-
verbose = TRUE)
134-
rgrass::read_RAST(bartlett_resamp_filt, return_format = "terra") |>
135-
plot(main = "Bartlett ZoI 1000m")
136-
137-
# not run
138-
# Gaussian - to be implemented!
139-
\dontrun{
140-
gauss_resamp_filt <- calc_zoi_cumulative(x = cabins_bin_g,
141-
radius = "1000,3000",
142-
type = "gauss,box",
143-
output_type = "cumulative_zoi",
144-
where = "GRASS",
145-
module = "r.resamp.filter",
146-
overwrite = TRUE, quiet = FALSE)
147-
rgrass::read_RAST(bartlett_resamp_filt, return_format = "terra") |>
148-
plot()
149-
}
150-
151-
# remove rasters created
152-
# to_remove_rast <- unique(c(all_names, exp_name_d,
153-
# rectangle_resamp_filt, bartlett_resamp_filt))
154-
# rgrass::execGRASS("g.remove", type = "vect", name = to_remove_vect, flags = "f")
155-
# rgrass::execGRASS("g.remove", type = "rast", name = to_remove_rast, flags = "f")
1+
# Running calc_zoi_cumulative through GRASS GIS
2+
library(rgrass)
3+
library(terra)
4+
5+
# Load raster data
6+
f <- system.file("raster/sample_area_cabins.tif", package = "oneimpact")
7+
cabins <- terra::rast(f)
8+
9+
# connect to grass gis and create grass location
10+
# For linux or within OSGeo4W shell
11+
grassdir <- system("grass --config path", intern = TRUE)
12+
# grassdir <- system("grass78 --config path", intern = TRUE) # for GRASS 7.8
13+
# If you used the standalone installer in Windows
14+
# grassdir <- "C:\\Programs\\GRASS GIS 7.8" # Correct if the path GRASS version or path is different
15+
16+
gisDB <- "." # create location and mapset in the working directory
17+
loc <- "ETRS_33N/" # name of the location
18+
ms <- "PERMANENT" # name of the mapset
19+
rgrass::initGRASS(gisBase = grassdir,
20+
SG = cabins, # use map to define location projection
21+
home = tempdir(),
22+
override = TRUE,
23+
gisDbase = gisDB,
24+
location = loc,
25+
mapset = ms)
26+
27+
# define map name within GRASS GIS
28+
cabins_g <- "cabins_example"
29+
# add file to GRASS GIS mapset
30+
rgrass::write_RAST(cabins, cabins_g, flags = c("o", "overwrite"))
31+
32+
# check
33+
terra::plot(cabins, col = "black",
34+
main = "Map of tourist cabins")
35+
36+
#---
37+
# define region in GRASS GIS
38+
rgrass::execGRASS("g.region", raster = cabins_g,
39+
flags = "p")
40+
41+
#---
42+
# Guarantee input map is binary (zeros as background)
43+
44+
# Input map name within GRASS GIS - binary map
45+
cabins_bin_g <- grass_binarize(cabins_g, breaks = 1, output = "cabins_example_bin",
46+
null = 0, overwrite = TRUE)
47+
48+
# check input
49+
cabins_bin <- rgrass::read_RAST("cabins_example_bin", return_format = "terra", NODATA = 255)
50+
51+
plot(cabins_bin, col = c("lightyellow", "black"),
52+
main = "Binarized map of cabins")
53+
54+
#---
55+
# Using 'r.mfilter' algorithm (default)
56+
57+
# Exponential decay
58+
exp_name <- calc_zoi_cumulative(x = cabins_bin_g,
59+
radius = 1000, zoi_limit = 0.01,
60+
type = "exp_decay",
61+
where = "GRASS",
62+
g_overwrite = TRUE,
63+
verbose = TRUE)
64+
# Bartlett decay
65+
barlett_name <- calc_zoi_cumulative(x = cabins_bin_g,
66+
radius = 1000,
67+
type = "bartlett",
68+
where = "GRASS",
69+
g_overwrite = TRUE,
70+
verbose = TRUE)
71+
# Gaussian decay
72+
gauss_name <- calc_zoi_cumulative(x = cabins_bin_g,
73+
radius = 1000, zoi_limit = 0.01,
74+
type = "Gauss",
75+
where = "GRASS",
76+
g_overwrite = TRUE,
77+
verbose = TRUE)
78+
79+
# Threshold decay (circle, step)
80+
threshold_name <- calc_zoi_cumulative(x = cabins_bin_g,
81+
radius = 1000,
82+
type = "threshold",
83+
where = "GRASS",
84+
g_overwrite = TRUE,
85+
verbose = TRUE)
86+
87+
(all_names <- c(exp_name, barlett_name, gauss_name, threshold_name))
88+
89+
# visualize
90+
cabins_zoi_cumulative <- rgrass::read_RAST(all_names, return_format = "terra")
91+
92+
title_plot <- c("Exponential decay 1000m", "Bartlett decay 1000m",
93+
"Gaussian decay 1000m", "Threshold decay 1000m")
94+
terra::plot(cabins_zoi_cumulative, main = title_plot)
95+
96+
#---
97+
# calculate density vs cumulative ZOI
98+
exp_name_d <- calc_zoi_cumulative(x = cabins_bin_g,
99+
radius = 1000, zoi_limit = 0.01,
100+
type = "exp_decay", output_type = "density",
101+
where = "GRASS",
102+
g_overwrite = TRUE,
103+
verbose = TRUE)
104+
105+
cabins_density <- rgrass::read_RAST(exp_name_d, return_format = "terra")
106+
107+
terra::plot(c(cabins_zoi_cumulative[[1]], cabins_density),
108+
main = c("Cumulative ZOI", "Density"))
109+
110+
#---
111+
# Using 'r.resamp.filter' algorithm
112+
113+
# rectangle
114+
rectangle_resamp_filt <- calc_zoi_cumulative(x = cabins_bin_g,
115+
radius = 1000,
116+
type = "box",
117+
output_type = "density",
118+
where = "GRASS",
119+
g_module = "r.resamp.filter",
120+
g_overwrite = TRUE,
121+
verbose = TRUE)
122+
rgrass::read_RAST(rectangle_resamp_filt, return_format = "terra") |>
123+
plot(main = "Rectangle ZOI 1000m")
124+
125+
# bartlett
126+
bartlett_resamp_filt <- calc_zoi_cumulative(x = cabins_bin_g,
127+
radius = 1000,
128+
type = "bartlett",
129+
output_type = "cumulative_zoi",
130+
where = "GRASS",
131+
g_module = "r.resamp.filter",
132+
g_overwrite = TRUE,
133+
verbose = TRUE)
134+
rgrass::read_RAST(bartlett_resamp_filt, return_format = "terra") |>
135+
plot(main = "Bartlett ZOI 1000m")
136+
137+
# not run
138+
# Gaussian - to be implemented!
139+
\dontrun{
140+
gauss_resamp_filt <- calc_zoi_cumulative(x = cabins_bin_g,
141+
radius = "1000,3000",
142+
type = "gauss,box",
143+
output_type = "cumulative_zoi",
144+
where = "GRASS",
145+
module = "r.resamp.filter",
146+
overwrite = TRUE, quiet = FALSE)
147+
rgrass::read_RAST(bartlett_resamp_filt, return_format = "terra") |>
148+
plot()
149+
}
150+
151+
# remove rasters created
152+
# to_remove_rast <- unique(c(all_names, exp_name_d,
153+
# rectangle_resamp_filt, bartlett_resamp_filt))
154+
# rgrass::execGRASS("g.remove", type = "vect", name = to_remove_vect, flags = "f")
155+
# rgrass::execGRASS("g.remove", type = "rast", name = to_remove_rast, flags = "f")

examples/calc_zoi_example.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cabins <- terra::rast(f)
88
# check background values
99
terra::freq(cabins) ## No zeros, background is NA
1010

11-
# compute both Zoi metrics with Gaussian decay, radius = 1000 m
11+
# compute both ZOI metrics with Gaussian decay, radius = 1000 m
1212
# since the background is NA, we use zeroAsNA = FALSE
1313
zoi_metrics <- calc_zoi(cabins,
1414
radius = 1000,
@@ -28,7 +28,7 @@ cabins_count <- terra::rast(f)
2828
# check background values
2929
terra::freq(cabins_count) ## Places with no infrastructure have value zero
3030

31-
# compute both Zoi metrics with linear decay, varying radius from 1000 m to 3000 m
31+
# compute both ZOI metrics with linear decay, varying radius from 1000 m to 3000 m
3232
# since the background is zero, we use zeroAsNA = TRUE
3333
zoi_metrics2 <- calc_zoi(cabins_count,
3434
radius = c(1000, 2000, 3000),

0 commit comments

Comments
 (0)