Skip to contents

Calculate landscape diversity using r.diversity module.

Usage

lsm_diversity_parallel(
  input,
  output = NULL,
  buffer_radius,
  index,
  alpha = NULL,
  grid_size,
  grid_delete = FALSE,
  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.

buffer_radius

[numeric()]
Integer indicating window size.

index

[character=""]
Integer indicating window size.

grid_size

[numeric()]
Integer indicating window size.

grid_delete

[numeric()]
Integer indicating window size.

nprocs

[numeric()]

memory

[numeric()]

Examples

library(lsmetrics)
library(terra)

# read habitat data
r <- lsmetrics::lsm_toy_landscape(type = "class")

# plot
plot(r, legend = FALSE, axes = FALSE, main = "Classes")
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)

text(r)
#> Warning: [readValues] raster has no values
#> Warning: [as.points] raster has no values
#> Error in text.default(xy[, 1], xy[, 2], labels, ...): zero-length 'labels' specified

# 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")
#> Error: [writeRaster] there are no cell values

# diversity
lsmetrics::lsm_diversity_parallel(input = "r", index = "shannon", buffer_radius = 100, grid_size = 1000, grid_delete = FALSE, nprocs = 5, memory = 1e5)
#> Error in rgrass::execGRASS(cmd = "g.region", flags = "a", raster = input,     res = as.character(grid_size)): The command:
#> g.region -a raster=r res=1000
#> produced an error (1) during execution:
#> ERROR: Raster map <r> not found

# files
# rgrass::execGRASS(cmd = "g.list", type = "vector")
# rgrass::execGRASS(cmd = "g.list", type = "raster")

# import from grass to r
v <- rgrass::read_VECT("grid", flags = "quiet")
#> Error in execGRASS("v.info", flags = "t", map = vname, layer = layer,     intern = TRUE, ignore.stderr = ignore.stderr): The command:
#> v.info -t map=grid layer=1
#> produced an error (1) during execution:
#> ERROR: Vector map <grid> not found
r_div_buf100 <- rgrass::read_RAST("r_diversity_shannon_buffer100", flags = "quiet", return_format = "terra")
#> Error in execGRASS("r.info", flags = "r", map = vname[i], intern = TRUE,     ignore.stderr = ignore.stderr): The command:
#> r.info -r map=r_diversity_shannon_buffer100
#> produced an error (1) during execution:
#> ERROR: Raster map <r_diversity_shannon_buffer100> not found

# plot
plot(r_div_buf100, legend = FALSE, axes = FALSE, main = "Landscape diversity (Shannon) (buffer 100 m)")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_div_buf100' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(v, lwd = 3, border = "blue", add = TRUE)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'v' not found
text(v, cex = 3, col = "blue")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'v' not found
text(r_div_buf100, digits = 1, cex = .75)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_div_buf100' not found

# delete grassdb
unlink("grassdb", recursive = TRUE)