Calculate statistics for vectors
Usage
lsm_vector_statistic(
input,
output = NULL,
landscape_metric,
landscape_metric_has_null = FALSE,
vector,
type,
column_prefix,
method = "average",
percentile = NULL
)
Arguments
- input
[character=""]
- output
[character=""]
Map name output inside GRASS Data Base.- landscape_metric
[character=""]
- landscape_metric_has_null
[character=""]
- vector
[character=""]
- type
[character=""]
- column_prefix
[character=""]
- method
[character=""]
Univariate statistics: number, null_cells, minimum ,maximum, range, average, stddev, variance, coeff_var, sum, first_quartile ,median, third_quartile, percentile- percentile
[character=""]
Examples
library(lsmetrics)
library(terra)
# read habitat data
r <- lsmetrics::lsm_toy_landscape()
# 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", "quiet"), vname = "r", verbose = FALSE)
# vector
p <- terra::vect(data.frame(x = c(235150, 234450, 235150),
y = c(7525250, 7524850, 7524350)),
geom = c("x", "y"),
crs = "+proj=utm +zone=23 +south +datum=WGS84 +units=m +no_defs +type=crs")
# import raster from r to grass
rgrass::write_VECT(x = p, flags = c("o", "overwrite", "quiet"), vname = "p")
# area
lsmetrics::lsm_fragment_area(input = "r", id = TRUE, ncell = TRUE, area_integer = TRUE)
#> Converting zero as null
#> Identifying the fragmentes
#> Counting the cell number of fragmentes
#> 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%
#> Calculating the area of fragmentes
# vector statistics
lsmetrics::lsm_vector_statistic(input = "r",
landscape_metric = "r_fragment_area_ha",
landscape_metric_has_null = TRUE,
vector = "p",
type = "point",
column_prefix = "area",
method = "average")
#> Processing input data (3 categories)...
#> Adding columns '['area_average DOUBLE PRECISION']'
#> Adding column <area_average> to the table
#> 6% 12% 18% 25% 31% 37% 43% 50% 56% 62% 68% 75% 81% 87% 93% 100%
#> 100%
#> Updating the database ...
#> Statistics calculated from raster map <r_fragment_area_ha_binary> and
#> uploaded to attribute table of vector map <p>.
# files
# rgrass::execGRASS(cmd = "g.list", type = "vector")
# import from grass to r
r_fragment_area <- rgrass::read_RAST("r_fragment_area_ha", flags = "quiet", return_format = "terra")
# import point
v_point <- rgrass::read_VECT("p", flags = "quiet")
# plot
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)
plot(v_point, "area_average", legend = FALSE, cex = 3, add = TRUE)
plot(p, col = "gray30", cex = 3, add = TRUE)
text(v_point, col = "white", labels = "area_average", cex = .7)
# delete grassdb
unlink("grassdb", recursive = TRUE)