pr_extract_spectra: Extract values and statistics from a PRISMA hyperspectral...

Description Usage Arguments Value Examples

View source: R/pr_extract_spectra.R

Description

function to extract values of a PRISMA image converted with prismaread on points/polygons saved on a vector file or on a 'sf' object

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
pr_extract_spectra(
  in_file,
  in_vect,
  id_field = NULL,
  dissolve = FALSE,
  stats = TRUE,
  selstats = c("mean", "stdev"),
  stats_format = "long",
  quantiles = FALSE,
  percs = c(0.05, 0.25, 0.5, 0.75, 0.95),
  allpix = FALSE,
  out_file = NULL
)

Arguments

in_file

input PRISMA file obtained with 'pr_convert'

in_vect

either the full path to a vector file, or a 'sf' object containing the points/polygons on which data should be extracted

id_field

'character' (Optional), name of the column of the vector dataset to be used to "name" the outputs, and also "aggregate" them in case 'dissolve' is TRUE. If NULL, a arbitrary 'ID' field is created, and each point/polygon is considered separately (see Details), Default: NULL

dissolve

'logical' If TRUE and 'id_field' was specified, in case multiple features of the input vector share a common id, they are dissolved before extracting the data, Default: FALSE

stats

'logical' IF TRUE, compute standard statistics (mean, min, max, sd, variation coefficient) on the vector features. Statistics to be computed are set using the 'selstats' argument, Default: TRUE

selstats

'character' containing the statistics to be computed. Possible values are: "mean", "stdev","variance","coeffvar", "min","max", Default: c("mean", "stdev")

stats_format

'character' ["long" | "wide"] defines the format used for statistics output. If "long", the output has one column for the ID of the feature, and one column for each statistic. If "wide", the output has one column for each ID/statistic couple (e.g., mean_id_1, stdev_id_1, mean_id_2, etcetera)

quantiles

'logical', if TRUE, also compute quantiles on the vector features. Computed quantiles are set using the 'percs' argument, Default: FALSE

percs

'(sorted) numeric array [0,1]' defines which quantiles should be computed if 'quantiles' is TRUE, Default: c(0.05,0.25,0.50,0.75,0.95)

allpix

'logical' IF TRUE, also save the values for all pixels of the 'in_vect' features in the 'allpix' slot of the output list, Default: FALSE

out_file

'character' full path of an output file where results should be stored, with extension. Valid extensions are ".csv", ".xls", ".xlsx" and ".RData". If NULL, output is not saved, Default: NULL

Value

format of the output varies based on arguments 'allpix' and 'stats' 1. If stats = TRUE and allpix = FALSE: a 'tibble' containing extracted statistics, for each feature of the input and each wavelength. Format depends on 'stat_sformat'; 2. If stats = FALSE and allpix = TRUE: a 'tibble' containing extracted raster values for each pixel of each feature of the input and each wavelength; 3. If stats = TRUE and allpix = TRUE: a 'list' in which the 'stats' slot contains statistics, and the 'allpix' slot contains pixel values;

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
## Not run: 
if(interactive()){
 in_file <- "D:/prismareaetd/L2D/testL2D_HCO_VNIR.envi"
 in_vect <- "D:/prismaread/test/testpoints_l2d_polys.gpkg"
 # extract base statistics
 test <- pr_extract_spectra(in_file, in_vect,
                            out_file = "D:/Temp/test1.xlsx")
 test
 # plot results using ggplot
 ggplot(test, aes(x = wvl, y = mean)) +
   geom_line(aes(color = ID, group = ID)) +
   facet_wrap(~ID) +
   theme_light()

 # extract base statistics ands save results as excel file, in "wide" format
 test <- pr_extract_spectra(in_file, in_vect,
                            out_file = "D:/Temp/test1.xlsx",
                            stats_format = "wide")
 test

 # extract custom statistics
 test <- pr_extract_spectra(in_file, in_vect,
                            selstats = c("mean", "coeffvar", "stdev",
                                         "min", "max"))
 # plot results using ggplot
 ggplot(test, aes(x = wvl)) +
   geom_line(aes(y = mean, color = ID, group = ID)) +
   geom_line(aes(y = mean + stdev, group = ID), color = "grey75") +
   geom_line(aes(y = mean - stdev, group = ID), color = "grey75") +
   facet_wrap(~ID) +
   theme_light()

 # extract custom statistics and quantiles
 test <- pr_extract_spectra(in_file, in_vect, quantiles = TRUE,
                                selstats = c("mean", "stdev"))
 test

 # extract also all pixels
 test <- pr_extract_spectra(in_file, in_vect, allpix = TRUE,
                            quantiles = TRUE,
                            selstats = c("mean", "stdev"))
 test$allpix

 ggplot(test$allpix, aes(x = wvl)) +
   geom_line(aes(y = value, group = pixel, color = ID), lwd = 0.01)  +
   facet_wrap(~ID) +
   theme_light()

 }

## End(Not run)

lbusett/prismaread documentation built on Feb. 22, 2022, 7:33 p.m.