|
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") |
0 commit comments