Skip to contents

Identify the core of landscape elements for each pixel. Calculate area, original area and percentage.

Usage

lsm_core(
  input,
  output = NULL,
  zero_as_null = FALSE,
  edge_depth,
  edge_contraction = "maximum",
  area_round_digits = 0,
  area_unit = "ha",
  map_core_id = FALSE,
  map_core_ncell = FALSE,
  map_core_area = FALSE,
  map_core_number = FALSE,
  map_core_area_index = FALSE,
  map_core_id_original = FALSE,
  map_core_ncell_original = FALSE,
  map_core_area_original = FALSE,
  map_core_percentage = FALSE,
  buffer_radius = NULL,
  buffer_circular = 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=""]
Map name output inside GRASS Data Base.

zero_as_null

[logical(1)=FALSE]
If TRUE, the function treats non-habitat cells as null; if FALSE, the function converts non-habitat zero cells to null cells.

edge_depth

[numeric]
Integer indicating edge distance in meters considered adjacent to form a patch.

edge_contraction

[character=""]

area_round_digits

[logical(1)=FALSE]
If TRUE

area_unit

[logical(1)=FALSE]
If TRUE

map_core_id

[logical(1)=FALSE]

map_core_ncell

[logical(1)=FALSE]

map_core_area

[logical(1)=FALSE]

map_core_area_index

[logical(1)=FALSE]

map_core_id_original

[logical(1)=FALSE]

map_core_ncell_original

[logical(1)=FALSE]

map_core_area_original

[logical(1)=FALSE]

map_core_percentage

[logical(1)=FALSE]

buffer_radius

[numeric]

buffer_circular

[logical(1)=FALSE]

nprocs

[numeric()]

memory

[numeric()]

Examples

library(lsmetrics)
library(terra)

# read habitat data
r <- lsmetrics::lsm_toy_landscape(proj_type = "meters")

# 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["WGS 84 / UTM zone 23S",
#>     BASEGEOGCRS["WGS 84",
#>         ENSEMBLE["World Geodetic System 1984 ensemble",
#>             MEMBER["World Geodetic System 1984 (Transit)"],
#>             MEMBER["World Geodetic System 1984 (G730)"],
#>             MEMBER["World Geodetic System 1984 (G873)"],
#>             MEMBER["World Geodetic System 1984 (G1150)"],
#>             MEMBER["World Geodetic System 1984 (G1674)"],
#>             MEMBER["World Geodetic System 1984 (G1762)"],
#>             MEMBER["World Geodetic System 1984 (G2139)"],
#>             ELLIPSOID["WGS 84",6378137,298.257223563,
#>                 LENGTHUNIT["metre",1]],
#>             ENSEMBLEACCURACY[2.0]],
#>         PRIMEM["Greenwich",0,
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         ID["EPSG",4326]],
#>     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]]],
#>     CS[Cartesian,2],
#>         AXIS["(E)",east,
#>             ORDER[1],
#>             LENGTHUNIT["metre",1]],
#>         AXIS["(N)",north,
#>             ORDER[2],
#>             LENGTHUNIT["metre",1]],
#>     USAGE[
#>         SCOPE["Navigation and medium accuracy spatial referencing."],
#>         AREA["Between 48°W and 42°W, southern hemisphere between 80°S and equator, onshore and offshore. Brazil."],
#>         BBOX[-80,-48,0,-42]],
#>     ID["EPSG",32723]] 

# import raster from r to grass
rgrass::write_RAST(x = r, flags = c("o", "overwrite", "quiet"), vname = "r")
#> SpatRaster read into GRASS using r.in.gdal from memory

# core and edge
lsmetrics::lsm_core_edge(input = "r",
                         edge_depth = 100,
                         core_edge_type = "both",
                         id = TRUE,
                         core_number = TRUE,
                         core_edge_original = TRUE,
                         calculate_area = TRUE,
                         calculate_percentage = TRUE,
                         buffer_radius = 100,
                         buffer_circular = FALSE)
#> Error: 'lsm_core_edge' is not an exported object from 'namespace:lsmetrics'

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

# import from grass to r
r_core100 <- rgrass::read_RAST("r_core100", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_core100 not found in mapsets in search path: PERMANENT
r_core100_id <- rgrass::read_RAST("r_core100_id", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_core100_id not found in mapsets in search path: PERMANENT
r_core100_area_ha <- rgrass::read_RAST("r_core100_area_ha", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_core100_area_ha not found in mapsets in search path: PERMANENT
r_core100_area_ha_original <- rgrass::read_RAST("r_core100_area_ha_original", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_core100_area_ha_original not found in mapsets in search path: PERMANENT
r_core100_core_number <- rgrass::read_RAST("r_core100_core_number_original", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_core100_core_number_original not found in mapsets in search path: PERMANENT
r_core100_pct_buf100 <- rgrass::read_RAST("r_core100_pct_buf100", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_core100_pct_buf100 not found in mapsets in search path: PERMANENT

r_edge100 <- rgrass::read_RAST("r_edge100", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_edge100 not found in mapsets in search path: PERMANENT
r_edge100_id <- rgrass::read_RAST("r_edge100_id", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_edge100_id not found in mapsets in search path: PERMANENT
r_edge100_area_ha <- rgrass::read_RAST("r_edge100_area_ha", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_edge100_area_ha not found in mapsets in search path: PERMANENT
r_edge100_area_ha_original <- rgrass::read_RAST("r_edge100_area_ha_original", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_edge100_area_ha_original not found in mapsets in search path: PERMANENT
r_edge100_pct_buf100 <- rgrass::read_RAST("r_edge100_pct_buf100", flags = "quiet", return_format = "terra")
#> Error in sanitize_layername(name = vname[i], type = "raster", mapsets = msp,     ignore.stderr = ignore.stderr): r_edge100_pct_buf100 not found in mapsets in search path: PERMANENT

# plot
plot(r_core100, legend = FALSE, axes = FALSE, main = "Core")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_core100' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_core100)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_core100' not found

plot(r_core100_id, legend = FALSE, axes = FALSE, main = "Core id")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_core100_id' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_core100_id)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_core100_id' not found

plot(r_core100_area_ha, legend = FALSE, axes = FALSE, main = "Core area (ha)")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_core100_area_ha' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_core100_area_ha)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_core100_area_ha' not found

plot(r_core100_area_ha_original, legend = FALSE, axes = FALSE, main = "Core area original (ha)")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_core100_area_ha_original' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_core100_area_ha_original)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_core100_area_ha_original' not found

plot(r_core100_core_number, legend = FALSE, axes = FALSE, main = "Number of cores")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_core100_core_number' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_core100_core_number)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_core100_core_number' not found

plot(r_core100_pct_buf100, legend = FALSE, axes = FALSE, main = "Core percentage (buffer 100 m)")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_core100_pct_buf100' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_core100_pct_buf100, cex = .75)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_core100_pct_buf100' not found


plot(r_edge100, legend = FALSE, axes = FALSE, main = "Edge")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_edge100' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_edge100)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_edge100' not found

plot(r_edge100_id, legend = FALSE, axes = FALSE, main = "Edge id")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_edge100_id' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_edge100_id)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_edge100_id' not found

plot(r_edge100_area_ha, legend = FALSE, axes = FALSE, main = "Edge area (ha)")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_edge100_area_ha' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_edge100_area_ha)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_edge100_area_ha' not found

plot(r_edge100_area_ha_original, legend = FALSE, axes = FALSE, main = "Edge area original (ha)")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_edge100_area_ha_original' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_edge100_area_ha_original)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_edge100_area_ha_original' not found

plot(r_edge100_pct_buf100, legend = FALSE, axes = FALSE, main = "Edge percentage (buffer 100 m)")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r_edge100_pct_buf100' not found
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)

text(r_edge100_pct_buf100, cex = .75)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'text': object 'r_edge100_pct_buf100' not found

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