extract_rast: extract raster values on features of a vector

Description Usage Arguments Value Author(s) Examples

View source: R/extract_rast.R

Description

Function used to extract values of a single- or multi- band raster for pixels corresponding to the features of a vector (Polygon, Point or Line)

Usage

1
2
3
4
5
extract_rast(in_rast, in_vect, selbands = NULL, rastres = NULL,
  id_field = NULL, summ_data = TRUE, full_data = TRUE,
  comp_quant = FALSE, long = FALSE, FUN = NULL, small = TRUE,
  na.rm = TRUE, maxchunk = 5e+07, addfeat = TRUE, addgeom = TRUE,
  keep_null = FALSE, verbose = TRUE, ncores = NULL)

Arguments

in_rast

input raster. Can be wither:

  1. A file name corresponding to a valid single- or multi- band raster (e.g. /my_folder/myfile.tif) All gdal raster formats are supported

  2. An raster, rasterStack or rasterBrick object

in_vect

input vector object containing zones from which data has to be extracted. Can be either:

  1. A file name corresponding to a valid ESRI shapefile (e.g. /my_folder/myshape.shp)

  2. An R +sp or sf object

selbands

2-element numeric array defining starting and ending raster bands to be processed (e.g., c(1, 10). Default: NULL, meaining that all bands will be processed

rastres

numeric, if not null, the input raster is resampled to rastres prior to data extraction using nearest neighbour resampling. This is useful in case the polygons are small with respect to in_rast resolution, Default: NULL (meaning no resampling is done)

id_field

character (optional) field of the vector file to be used to identify the zones from which data is to be extracted, If NULL (or invalid) zonal statistics are extracted on each row of the shapefile, and a new column named id_feat is used on the output to distinguish the zones, Default: NULL

summ_data

logical If TRUE, summary statistics of values belonging to each vector zone are returned in out$stats. Default statistics are average, median, standard deviation and variation coefficient. Quantiles of the distribution can be also returned if comp_quant == TRUE, Default: TRUE

full_data

logical If TRUE, values of all pixels belonging to each vector zone are returned in ‘out$alldata’ (Setting this to FALSE can be a good idea for large datasets....), Default: TRUE

comp_quant

logical if TRUE, also quantiles of the distributions of values are computed for each zone and returned in out$ts_summ, Default: FALSE

long

logical if TRUE, extraction on points provides data in long form

FUN

character or function name, Default: NULL

small

logical if TRUE, values are returned also for small polygons not including any pixel centroids. Values are taken from all cells "touched" by the small polygon, Default: TRUE

na.rm

logical If TRUE, NA values are removed while computing statistics, Default: TRUE

maxchunk

Maximum chunk size (provisional), Default: 5e+06

addfeat

logical If TRUE, columns of the attribute table of the in_vect layer are joined to results of the computation, Default: TRUE

addgeom

logical, If TRUE, the output sent out as an sf object, preserving the geometry of in_vect. Note that this leads to duplication of geometries, and may be very slow for large datasets. Default: FALSE

keep_null

logical If TRUE, the output preserves features of in_vect falling outside the extent of in_rast. Values for these features are set to NA, Default: FALSE

verbose

logical If TRUE, messages concerning the processing status are shown in the console, Default: TRUE

ncores

numeric maximum number of cores to be used in the processin. If NULL, defaults to available cores - 2, but up to a maximum of 8. If user-provided is greater than available cores - 2 or greater than 8, ncores is re-set to the minimum between those two.

Value

out_list list containing two tibbles: out_list$summ_data contains summary statitstics, while out_list$alldata contains the data of all pixels extracted (see examples).

Author(s)

Lorenzo Busetto, phD (2017) lbusett@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## Not run: 
library(sprawl)
library(sprawl.data)
library(raster)
library(tibble)
in_polys <- read_vect(system.file("extdata","lc_polys.shp", package = "sprawl.data"),
                       stringsAsFactors = T)
in_rast  <- raster::stack(system.file("extdata", "sprawl_EVItest.tif", package = "sprawl.data"))
in_rast  <- raster::setZ(in_rast, doytodate(seq(1,366, by = 8), year = 2013))
out      <- extract_rast(in_rast, in_polys, verbose = FALSE)
as_tibble(out$stats)
as_tibble(out$alldata)

## End(Not run)

IREA-CNR-MI/sprawl documentation built on May 27, 2019, 1:12 p.m.