knitr::opts_chunk$set(echo = TRUE)
You have a raster multi-band file containing the values of an index (e.g., EVI) for multiple dates, and a vector file with areas from which you wish to either:
You can use sprawl::extract_rast
to quickly extract both the single-pixel time series, and the
statistics for each polygon and date
First of all, open the input raster containing the time series, and of the input vector file containing the polygons (i.e., the ROIs) for which you want the values.
# Here I'm using test datasets accessed on the filesystem, but on common usage it would as # simple as `in_polys <- read_vect("D:/mypath/myfolder/lc_polys.shp"") library(raster) library(sprawl) in_polys <- read_vect(system.file("extdata","lc_polys.shp", package = "sprawl.data")) in_rast <- stack(system.file("extdata", "sprawl_EVItest.tif", package = "sprawl.data"))
Now, in in_polys
we have a vector with 13 polygons:
in_polys
, and in in_rast
a raster time series with 46 bands:
in_rast
Let's have a look at some bands:
plot_rast(in_rast[[2:7]], limits = c(0,7000), in_poly = in_polys)
To extract the values of pixels included in each polygon, you can then use simply:
extracted_values <- extract_rast(in_rast, in_polys)
extracted_values <- extract_rast(in_rast, in_polys, id_field = "lc_type")
extracted values
is a list containing two components:
extracted_values$alldata
contains the values for each extracted pixel, along with information extracted from the vector file. Additionally, the number of pixels extracted from each polygon is shown in the N_PIX
column, and an identifier to each pixel in the N
column head(extracted_values$alldata)
extracted_values$statdata
contains instead the statistics for each polygon and band. By default, average, standard deviation, minimum, maximum and median are computed (see the help of the function) head(extracted_values$stats)
These results can be easily used for performing further analysis or plotting (coming soon !)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.