
Calculate percentage using parallel computing
Source:R/lsm_percentage_parallel.R
lsm_percentage_parallel.Rd
Calculate focal ("moving window") values for each cell using the mean from r.neighbors module and multiplies by 100.
Usage
lsm_percentage_parallel(
input,
output = NULL,
zero_as_na = FALSE,
buffer_radius,
buffer_circular = FALSE,
grid_size,
grid_delete = TRUE,
nprocs = 1,
memory = 300
)
Arguments
- input
[character=""]
Habitat map, following a binary classification (e.g. values 1,0 or 1,NA for habitat,non-habitat).- output
[character=""]
Habitat map output name inside GRASS Data Base.- zero_as_na
[logical=""]
- buffer_radius
[numeric()]
Integer indicating window size.- grid_size
[numeric()]
Integer indicating window size.- nprocs
[numeric()]
- memory
[numeric()]
- buffer_cirular
[logical=""]
Examples
library(lsmetrics)
library(terra)
# read habitat data
r <- lsmetrics::lsm_toy_landscape(type = "binary")
# plot
plot(r, legend = FALSE, axes = FALSE, main = "Binary habitat")
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r)
# find grass
path_grass <- system("grass --config path", inter = TRUE) # windows users need to find the grass gis path installation, e.g. "C:/Program Files/GRASS GIS 8.3"
# create grassdb
rgrass::initGRASS(gisBase = path_grass,
SG = r,
gisDbase = "grassdb",
location = "newLocation",
mapset = "PERMANENT",
override = TRUE)
#> gisdbase grassdb
#> location newLocation
#> mapset PERMANENT
#> rows 16
#> columns 16
#> north 7525600
#> south 7524000
#> west 234000
#> east 235600
#> nsres 100
#> ewres 100
#> projection:
#> PROJCRS["unknown",
#> BASEGEOGCRS["unknown",
#> DATUM["World Geodetic System 1984",
#> ELLIPSOID["WGS 84",6378137,298.257223563,
#> LENGTHUNIT["metre",1]],
#> ID["EPSG",6326]],
#> PRIMEM["Greenwich",0,
#> ANGLEUNIT["degree",0.0174532925199433],
#> ID["EPSG",8901]]],
#> CONVERSION["UTM zone 23S",
#> METHOD["Transverse Mercator",
#> ID["EPSG",9807]],
#> PARAMETER["Latitude of natural origin",0,
#> ANGLEUNIT["degree",0.0174532925199433],
#> ID["EPSG",8801]],
#> PARAMETER["Longitude of natural origin",-45,
#> ANGLEUNIT["degree",0.0174532925199433],
#> ID["EPSG",8802]],
#> PARAMETER["Scale factor at natural origin",0.9996,
#> SCALEUNIT["unity",1],
#> ID["EPSG",8805]],
#> PARAMETER["False easting",500000,
#> LENGTHUNIT["metre",1],
#> ID["EPSG",8806]],
#> PARAMETER["False northing",10000000,
#> LENGTHUNIT["metre",1],
#> ID["EPSG",8807]],
#> ID["EPSG",17023]],
#> CS[Cartesian,2],
#> AXIS["(E)",east,
#> ORDER[1],
#> LENGTHUNIT["metre",1,
#> ID["EPSG",9001]]],
#> AXIS["(N)",north,
#> ORDER[2],
#> LENGTHUNIT["metre",1,
#> ID["EPSG",9001]]]]
# import raster from r to grass
rgrass::write_RAST(x = r, flags = c("o", "overwrite"), vname = "r")
#> Over-riding projection check
#> Importing raster map <r>...
#> 0% 6% 12% 18% 25% 31% 37% 43% 50% 56% 62% 68% 75% 81% 87% 93% 100%
#> SpatRaster read into GRASS using r.in.gdal from memory
# percentage
lsmetrics::lsm_percentage_parallel(input = "r", buffer_radius = 100, grid_size = 1000, grid_delete = FALSE, nprocs = 5)
#> Calculating proportion
#> [1] "1 of 4"
#> Calculating proportion
#> Calculating proportion
#> Calculating percentage
#> Changing the raster color
#> Cleaning rasters
#> [1] "2 of 4"
#> Calculating proportion
#> Calculating proportion
#> Calculating percentage
#> Changing the raster color
#> Cleaning rasters
#> [1] "3 of 4"
#> Calculating proportion
#> Calculating proportion
#> Calculating percentage
#> Changing the raster color
#> Cleaning rasters
#> [1] "4 of 4"
#> Calculating proportion
#> Calculating proportion
#> Calculating percentage
#> Changing the raster color
#> Cleaning rasters
#> Changing the raster color
#> Cleaning rasters
#> Warning: The command:
#> g.remove -b -f --quiet type=vector name=r_grid_temp1,r_grid_temp2,r_grid_temp3,r_grid_temp4
#> produced at least one warning during execution:
#> WARNING: No data base element files found
#> WARNING: No data base element files found
#> Warning: The command:
#> g.remove -b -f --quiet type=vector name=r_grid_temp_buf1,r_grid_temp_buf2,r_grid_temp_buf3,r_grid_temp_buf4
#> produced at least one warning during execution:
#> WARNING: No data base element files found
#> WARNING: No data base element files found
# files
# rgrass::execGRASS(cmd = "g.list", type = "raster")
# rgrass::execGRASS(cmd = "g.list", type = "vector")
# import from grass to r
v <- rgrass::read_VECT("grid_sel", flags = "quiet")
r_pct_buf100 <- rgrass::read_RAST("r_pct_buf100", flags = "quiet", return_format = "terra")
# plot
plot(r_pct_buf100, legend = FALSE, axes = FALSE, main = "Habitat percentage (buffer 100 m)")
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
plot(v, lwd = 3, border = "blue", add = TRUE)
text(v, cex = 3, col = "blue")
text(r_pct_buf100, cex = .75)
# delete grassdb
unlink("grassdb", recursive = TRUE)