This function can be used to process a raster in order to
obtain standardised values within polygons of a specified vector file or R spatial object, and/or
replace values of the raster near to the borders of the polygons with local or global averages, and/or
fill holes (NA values) with local or global averages.
Some aliases are set for specific functions (see details).
standardise_rast(in_rast, out_file = NA, in_vect = NULL, by_poly = TRUE, min_area = 0, method = "zscore", fill_method = "focal", fill_na = TRUE, fill_borders = TRUE, buffer = 0, parallel = TRUE, format = NA, dataType = NA, scaleFactor = NA, compress = "DEFLATE", overwrite = FALSE) standardize_rast(in_rast, out_file = NA, in_vect = NULL, by_poly = TRUE, min_area = 0, method = "zscore", fill_method = "focal", fill_na = TRUE, fill_borders = TRUE, buffer = 0, parallel = TRUE, format = NA, dataType = NA, scaleFactor = NA, compress = "DEFLATE", overwrite = FALSE) zscore_rast(in_rast, out_file = NA, in_vect = NULL, by_poly = TRUE, min_area = 0, fill_method = "source", fill_na = TRUE, fill_borders = TRUE, buffer = 0, parallel = TRUE, format = NA, dataType = NA, scaleFactor = NA, compress = "DEFLATE", overwrite = FALSE) rbias_rast(in_rast, out_file = NA, in_vect = NULL, by_poly = TRUE, min_area = 0, fill_method = "source", fill_na = TRUE, fill_borders = TRUE, buffer = 0, parallel = TRUE, format = NA, dataType = NA, scaleFactor = NA, compress = "DEFLATE", overwrite = FALSE) fixborders_rast(in_rast, out_file = NA, in_vect = NULL, min_area = 0, fill_method = "focal", fill_na = TRUE, fill_borders = TRUE, buffer = 0, parallel = TRUE, format = NA, dataType = NA, scaleFactor = 1, compress = "DEFLATE", overwrite = FALSE) fillgaps_rast(in_rast, out_file = NA, fill_method = "focal", format = NA, compress = "DEFLATE", overwrite = FALSE)
in_rast | The input raster or path. |
---|---|
out_file | The path of the output raster; if NULL (default), a RasterLayer is returned by the function). |
in_vect | The vector file or object containing the polygons used as mask and as polygons within which computing the standardised values. |
by_poly | Logical: if TRUE (default), input raster values are standardised basing on the averages and standard deviations computed within each polygon; if FALSE, they are computed on the whole polygons. |
min_area | Numeric: the minimum area (in squared metres) that polygons
must have to be considered and included in the final raster; default is
0 (all non empty polygons are considered). Notice that, if a value of
|
method | Character: the method used to standardise values. Accepted values are:
|
fill_method | Character: the method used to fill buffered areas
(if
|
fill_na | Logical: if TRUE (default), NA values within polygons are
filled using the method selected with argument |
fill_borders | Logical: if TRUE (default), values between the border of
the polygons and the negative buffers around the polygons are filled using
the method set with argument |
buffer | Numeric: the buffer (in metres) applied to polygons usd to
compute average and standard deviation (default is 0).
If |
parallel | Logical or integer: if TRUE, the algorythm used to compute
output values is runned parallelised on single polygons and the number of
cores is automatically determined (otherwise, an integer value can be
provided in order to set this value manually); if FALSE (or 1), it is
runned on a single core. This value is not considered if |
format | (optional) Format of the output file (in a format recognised by GDAL). Default is to maintain each input format. |
dataType | (optional) Numeric datatype of the ouptut rasters.
if "Float32" or "Float64" is chosen, numeric values are not rescaled;
if "Int16" or "UInt16", values are multiplicated by |
scaleFactor | (optional) Scale factor for output values when an integer
datatype is chosen (default values with |
compress | (optional) In the case a GTiff format is present, the compression indicated with this parameter is used. Default is "DEFLATE". |
overwrite | Logical value: should existing output files be overwritten? (default: FALSE) |
NULL if out_file
is not NULL; the output RasterLayer otherwise.
Some aliases are set for specific functions:
zscore_rast()
computes the Z-score on the input raster
(equivalent to standardise_rast(method = "zscore")
);
rbias_rast()
computes the rbias on the input raster
(equivalent to standardise_rast(method = "rbias")
);
fixborders_rast()
can be used to replace border values
(equivalent to standardise_rast(method = "input", by_poly = TRUE, dataType = NA, scaleFactor = 1)
);
fillgaps_rast()
is used to fill gaps (NA values) without using
polygon masks
(equivalent to standardise_rast(by_poly = FALSE, min_area = 0, method = "input", fill_na = TRUE, fill_borders = FALSE, buffer = 0, parallel = FALSE, dataType = NA, scaleFactor = 1)
).
By default, input raster values are standardised basing on the averages
and standard deviations computed within each polygon (it is possible to
specify a buffer value for computing them setting the buffer
argument,
i.e. in order to exclude border values).
Oterwhise, setting by_poly = FALSE
these metrics are computed on the whole
polygons (so raster values are simply linearly combined and masked on the
polygon surface).
# NOT RUN { # These examples cannot be runned globally; # a global example will be done in future. # Define input files and paths projects_dir <- "/mnt/projects" # 10.0.1.230 nrworking_dir <- "/mnt/nr_working" # 10.0.1.230 data_dir <- file.path(nrworking_dir,"luigi/data/s2tsp/180117_test_zscore/MSAVI") raster_ex <- raster(file.path(data_dir,"S2B2A_20180104_022__MSAVI_10.tif")) source(file.path(nrworking_dir,"luigi/code/satfarming/opencon_satfarming.R")) appez <- st_read_db( con_satfarming, query = "SELECT * FROM bf_jolanda.appez WHERE anno = 2017;" ) # Example 1: negative big buffer, focal method, fill NA out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore1.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = -30, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "focal", fill_na = TRUE ) # Example 1.1: negative big buffer, focal method, do not fill NA out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore1_1.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = -30, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "focal", fill_na = FALSE ) # Example 2: negative buffer, average method out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore2.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = -10, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "average" ) # Example 2.1: negative buffer, average method, fill_na=FALSE out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore2_1.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = -10, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "average", fill_na = FALSE ) # Example 2.2: negative buffer, average method, do not fill borders out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore2_2.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = -20, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "average", fill_borders = FALSE ) # Example 2.3: negative buffer, do not fill neither borders nor holes out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore2_3.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = -20, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "indifferent", fill_borders = FALSE, fill_na = FALSE ) # Example 3: no buffer, only fill holes out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore3.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = 0, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "focal" ) # Example 4: do not standardise nor buffer, only fill holes (average) out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore4.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = 0, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "input", fill_method = "average" ) # Example 5: positive (exagerated) buffer out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore5.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=TRUE, buffer = 250, min_area=1E4, parallel=TRUE, overwrite=TRUE, method = "zscore", fill_method = "focal" ) # Example 6: remove borders and holes, globally out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore6.tif")) tmp <- standardise_rast( raster_ex, out_file, in_vect=appez[58:60,], by_poly=FALSE, buffer = -30, min_area=1E4, overwrite=TRUE, method = "zscore", fill_method = "focal" ) # Example 7: fill holes without masking over polygons out_file <- file.path(data_dir,paste0(raster_ex@data@names,"_zscore7.tif")) tmp <- standardise_rast( raster_ex, out_file, buffer = "indifferent", min_area=1E4, overwrite=TRUE, method = "input", fill_method = "focal" ) # }