Identifies fragments and calculates area in a selected unit.
Usage
lsm_area_fragment(
input,
output = NULL,
zero_as_null = FALSE,
region_input = FALSE,
id_direction = 8,
area_round_digit = 0,
area_unit = "ha",
map_fragment_id = FALSE,
map_fragment_ncell = FALSE,
table_fragment_area = FALSE
)
Arguments
- input
[character]
Habitat map (binary classification: e.g., 1/0 or 1/NA) in GRASS.- output
[character]
Output map base name in GRASS.- zero_as_null
[logical]
If TRUE, non-habitat (0) cells are converted to NULL.- region_input
[logical]
- id_direction
[numeric]
Neighborhood for clumping (4 or 8).- area_round_digit
[integer]
Decimal digits for area rounding.- area_unit
[character]
Area unit:"ha"
,"m2"
, or"km2"
.- map_fragment_id
[logical]
Keep fragment ID raster?- map_fragment_ncell
[logical]
Output raster with fragment cell counts?- table_fragment_area
[logical]
Output CSV with fragment area and summary?
Examples
library(lsmetrics)
library(terra)
#> terra 1.8.60
# read habitat data
r <- lsmetrics::lsm_toy_landscape(proj_type = "degrees")
# 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 -22.35558
#> south -22.36995
#> west -47.58312
#> east -47.56875
#> nsres 0.000898125
#> ewres 0.000898125
#> projection:
#> GEOGCRS["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]],
#> CS[ellipsoidal,2],
#> AXIS["geodetic latitude (Lat)",north,
#> ORDER[1],
#> ANGLEUNIT["degree",0.0174532925199433]],
#> AXIS["geodetic longitude (Lon)",east,
#> ORDER[2],
#> ANGLEUNIT["degree",0.0174532925199433]],
#> USAGE[
#> SCOPE["Horizontal component of 3D system."],
#> AREA["World."],
#> BBOX[-90,-180,90,180]],
#> ID["EPSG",4326]]
# import raster from r to grass
rgrass::write_RAST(x = r, flags = c("o", "overwrite", "quiet"), vname = "r", verbose = FALSE)
# area
lsmetrics::lsm_area_fragment(input = "r", area_round_digit = 2, map_fragment_id = TRUE, table_fragment_area = TRUE)
#> Converting zeros to null
#> Identifying fragments
#> Calculation area
#> Mask creating
#> Area calculating
#> First pass
#> 0% 6% 12% 18% 25% 31% 37% 43% 50% 56% 62% 68% 75% 81% 87% 93% 100%
#> Writing output map
#> 0% 6% 12% 18% 25% 31% 37% 43% 50% 56% 62% 68% 75% 81% 87% 93% 100%
#> Color assigning
#> Table exporting
#> Cleaning data
# files
rgrass::execGRASS(cmd = "g.list", type = "raster")
#> r
#> r_fragment_area
#> r_fragment_id
# import from grass to r
r_fragment_id <- rgrass::read_RAST("r_fragment_id", flags = "quiet", return_format = "terra")
# plot
plot(r_fragment_id, legend = FALSE, axes = FALSE, main = "Fragment id")
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_fragment_id)
# import from grass to r
r_fragment_area <- rgrass::read_RAST("r_fragment_area", flags = "quiet", return_format = "terra")
plot(r_fragment_area, legend = FALSE, axes = FALSE, main = "Fragment area (ha)")
plot(as.polygons(r, dissolve = FALSE), lwd = .1, add = TRUE)
plot(as.polygons(r), add = TRUE)
text(r_fragment_area, digits = 2)
# table
da_fragment_area <- vroom::vroom("r_fragment_table_area.csv", show_col_types = FALSE)
da_fragment_area
#> # A tibble: 6 × 3
#> id area ncell
#> <dbl> <dbl> <dbl>
#> 1 1 8.28 9
#> 2 2 23 25
#> 3 3 35.9 39
#> 4 4 1.84 2
#> 5 5 31.3 34
#> 6 6 0.92 1
# delete grassdb
unlink("r_fragment_table_area.csv")
unlink("grassdb", recursive = TRUE)