Skip to content

Commit

Permalink
Documentation changes
Browse files Browse the repository at this point in the history
Removal of sp and raster
  • Loading branch information
r-a-dobson committed Jun 10, 2023
1 parent a162072 commit ddecca5
Show file tree
Hide file tree
Showing 38 changed files with 269 additions and 353 deletions.
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ Suggests:
knitr,
magick,
matrixStats,
raster,
rmarkdown,
sp,
spThin,
stars,
testthat (>= 3.0.0),
Expand Down
47 changes: 0 additions & 47 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,6 @@
"sample_filt_data"


#' @name sample_biome_data
#' @title Biome classification raster for southern Africa.
#' @description
#' Categorical RasterLayer cropped to southern Africa at 0.08 degree resolution from OpenLandMap
#' Potential Distribution of Biomes (Hengl et al., 2018).
#' @format A RasterLayer
#' \describe{
#' \item{class}{RasterLayer}
#' \item{dimensions}{4839, 3243, 15692877 (nrow, ncol, ncell)}
#' \item{resolution}{0.008983153, 0.008983153 (x, y)}
#' \item{extent}{11.71845, 40.85081, -47.89832, -4.428839 (xmin, xmax, ymin, ymax)}
#' \item{crs}{"+proj=longlat +datum=WGS84 +no_defs"}
#' \item{source}{biome_type_2001-01-01.tif }
#' \item{names}{biome_type_2001-01-01.tif }
#' \item{values}{Numerical categorical value ranging from 0, 255 (min, max).}
#' }
#' @docType data
#' @references
#' Hengl T, Walsh MG, Sanderman J, Wheeler I, Harrison SP, Prentice IC. (2018) Global Mapping of
#' Potential Natural Vegetation: An Assessment of Machine Learning Algorithms for Estimating Land
#' Potential. PeerJ Preprints.
"sample_biome_data"



#' @name sample_events_data
#' @title Sample e-Bird sampling event records
Expand All @@ -176,7 +152,6 @@
#' \doi{10.2173/ebirdst.2020}
#'
#' GBIF.org (12 July 2021) GBIF Occurrence Download \doi{10.15468/dl.ppcu6q}

"sample_events_data"


Expand Down Expand Up @@ -212,25 +187,3 @@
#' hazards infrared precipitation with stations-a new environmental record for monitoring extremes".
#' Scientific Data 2, 150066.
"sample_cov_data"

#' @name sample_proj_rast
#' @title Distribution suitability projections for red-billed quelea in southern Africa
#' @description
#' RasterBrick of distribution suitability projections for red-billed quelea across southern at one
#' degree resolution for the dates: "2018-01-01", "2018-04-01", "2018-07-01" and "2018-10-01". A
#' value of 1 represents high suitability and a value of 0 low suitability.
#' @format A RasterBrick
#' \describe{
#' \item{class}{RasterBrick}
#' \item{dimensions}{31, 30, 930, 4 (nrow, ncol, ncell, nlayers)}
#' \item{resolution}{1, 1 (x, y)}
#' \item{extent}{11.6901, 41.6901, -35.43802, -4.438023 (xmin, xmax, ymin, ymax)}
#' \item{crs}{"+proj=longlat +datum=WGS84 +no_defs"}
#' \item{source}{memory} \item{names}{ X2018.01.01_proportional, X2018.04.01_proportional,
#' X2018.07.01_proportional, X2018.10.01_proportional }
#' \item{min values}{Numerical minimum distribution suitability projected in each layer.}
#' \item{max values}{Numerical maximum distribution suitability projected in each layer.}
#' }
#' @docType data
"sample_proj_rast"

21 changes: 13 additions & 8 deletions R/dynamic_proj.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#' Default is "+proj=longlat +datum=WGS84".
#'@param proj.prj a character string, the coordinate reference system desired for output projection
#' rasters. Default is assumed to be the same as prj.
#'@param spatial.mask an object of class `Raster`, `sf` or `Spatial`, representing a mask in which
#'@param spatial.mask an object of class `SpatRaster` or`sf` polygon, representing a mask in which
#' NA cells in the mask layer are removed from the projection covariates.
#'@details Function projects a model object or list of model objects onto projection covariate data
#' frames for each projection date given.
Expand Down Expand Up @@ -190,8 +190,13 @@ dynamic_proj <- function(dates,
# Convert mask to SPDF for use with raster::mask

if (!missing(spatial.mask)) {
spatial.mask<-convert_to_sf(spatial.mask,prj)
# spatial.mask<-sf::as_Spatial(spatial.mask)

spatial.mask <- convert_to_sf(spatial.mask, prj)

if(inherits(spatial.mask, "sfc_POLYGON")){
spatial.mask <- terra::vect(spatial.mask)
}

}


Expand Down Expand Up @@ -499,7 +504,7 @@ dynamic_proj <- function(dates,

binaryrast <- terra::rast(as.matrix(cbind(projection_df[, "x"],
projection_df[, "y"],
SDMbinary), crs = prj),
SDMbinary)), crs = prj,
type="xyz")

}
Expand Down Expand Up @@ -528,7 +533,7 @@ dynamic_proj <- function(dates,
if (cov.file.type == "csv") {
abundancerast <- terra::rast(as.matrix(cbind(projection_df[, "x"],
projection_df[, "y"],
SAMpred), crs = prj),
SAMpred)), crs = prj,
type="xyz")

}
Expand All @@ -554,8 +559,8 @@ dynamic_proj <- function(dates,
if (cov.file.type == "csv") {
proportionalrast <- terra::rast(as.matrix(cbind(projection_df[, "x"],
projection_df[, "y"],
SDMpred), crs = prj),
type="xyz")
SDMpred)), crs = prj,
type = "xyz")
}

if (!missing(spatial.mask)) {
Expand All @@ -579,7 +584,7 @@ dynamic_proj <- function(dates,
if (cov.file.type == "csv") {
stackedrast <- terra::rast(as.matrix(cbind(projection_df[, "x"],
projection_df[, "y"],
stacked), crs = prj),
stacked)), crs = prj,
type="xyz")
}

Expand Down
81 changes: 50 additions & 31 deletions R/dynamic_proj_covariates.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#'stacked and then exported as a covariate data frame or raster stack for each projection date.
#'
#'@param dates a character string, vector of dates in format "YYYY-MM-DD".
#'@param spatial.ext optional; the spatial extent to crop explanatory variable rasters to. Object of
#' class `Extent`, `RasterLayer`, `sf`, `SpatialPolygonsDataFrame`, `polygon` or numeric vector
#' listing xmin, xmax, ymin and ymax in order.
#'@param spatial.ext optional; the spatial extent to crop explanatory variable
#' rasters to. Object of class `SpatExtent`, `SpatRaster`, an `sf` polygon or
#' numeric vector listing xmin, xmax, ymin and ymax in order.
#'@param varnames a character string, the unique names for each explanatory variable.
#'@param spatial.res.degrees optional; a numeric value, the spatial resolution in degrees for
#' projection rasters to be resampled to. Required if `spatial.ext` given.
Expand All @@ -24,7 +24,7 @@
#'@param save.drive.folder optional; a character string, Google Drive folder to save projection
#' covariates to. Folder must be uniquely named within Google Drive. Do not provide path.
#'@param cov.file.type a character string, the type of file to export projection covariates as. One
#' of: `tif` (raster stack) or `csv`(data frame).
#' of: `tif` (SpatRaster with multiple layers) or `csv`(data frame).
#'@param prj a character string, the coordinate reference system desired for projection covariates.
#' Default is "+proj=longlat +datum=WGS84".
#'@param cov.prj a character string, the coordinate reference system desired for output projection
Expand Down Expand Up @@ -54,9 +54,10 @@
#'date and explanatory variable name, then the function will error.
#'
#'# Processing rasters
#'If required, rasters are cropped and resampled to the same spatial extent and resolution. If
#'`spatial.mask` is given, then cells with NA in this mask layer are removed from the returned
#'projection covariates. See `raster::mask()` in R package `raster` for details.
#'If required, rasters are cropped and resampled to the same spatial extent and
#'resolution. If `spatial.mask` is given, then cells with NA in this mask layer
#'are removed from the returned projection covariates. See `terra::mask()` in R
#'package `terra` for details (Hijmans et al., 2022).
#'
#'Rasters are then stacked and reprojected if `cov.prj` is different to `prj`.
#'
Expand Down Expand Up @@ -105,7 +106,7 @@
#'
#'#' # Spatial buffering of static rasters (optional)
#'
#' Using the `focal` function from `raster` R package (Hijmans et al., 2015),
#' Using the `focal` function from `terra` R package (Hijmans et al., 2022),
#' `GEE.math.fun` is calculated across the spatial buffer area from the record
#' co-ordinate. The spatial buffer area used is specified by the argument
#' `moving.window.matrix`, which dictates the neighbourhood of cells
Expand All @@ -129,6 +130,9 @@
#'@returns Exports combined covariates in "csv" or "tif" file for each projection date to the local
#'directory or Google Drive folder.
#'@export
#'@references
#'Hijmans, R.J., Bivand, R., Forner, K., Ooms, J., Pebesma, E. and Sumner, M.D.,
#'2022. Package 'terra'. Maintainer: Vienna, Austria.
#'@examplesIf googledrive::drive_has_token()
#'
#'data("sample_extent_data")
Expand Down Expand Up @@ -238,33 +242,13 @@ dynamic_proj_covariates <- function(dates,
stop("Provide save.directory or save.drive.folder to export data.frame.")
}

# If mask if sf polygon, convert to spatial polygons dataframe for raster:: mask
#if (!missing(spatial.mask)) {
## if (any(class(spatial.mask) == "sf")) {
# spatial.mask<-sf::as_Spatial(spatial.mask)
# }
# }

# Process spatial extent given for cropping rasters before stacking into covariate data frame

# Check spatial.ext appropriate object class.

if (!missing(spatial.ext)) {

if (!any(class(spatial.ext) %in% c("numeric",
"SpatialPolygonsDataFrame",
"Extent",
"RasterLayer",
"Polygon",
"SpatRaster",
"SpatExtent",
"sf"))) {

stop("Please check spatial.ext is of the correct class")

}



# Numeric extent to co-ords
if (is.numeric(spatial.ext) && length(spatial.ext) != 4) {
Expand Down Expand Up @@ -436,9 +420,18 @@ dynamic_proj_covariates <- function(dates,
r <- terra::setValues(r, 1:terra::ncell(r))

if(!missing(spatial.mask)) {

if(inherits(spatial.mask, "sfc_POLYGON")){
spatial.mask <- terra::vect(spatial.mask)
}

if("sf" %in% class(spatial.mask)){
spatial.mask <- terra::vect(spatial.mask)
}

# Mask to original spatial.ext, if fails keep original. Depending on spatial.ext type.
tryCatch({
r <- terra::mask(r, spatial.ext)
r <- terra::mask(r, spatial.mask)
}, error = function(error_message) {
r <- r
message("spatial.mask could not be used. Check valid input type.")
Expand All @@ -453,6 +446,16 @@ dynamic_proj_covariates <- function(dates,
if (!length(static.resample.method) == 1) {
raster <- terra::resample(raster, r, method = static.resample.method[sr])
}

if(!missing(spatial.mask)) {
tryCatch({
raster <- terra::mask(raster, spatial.mask)
}, error = function(error_message) {
raster <- raster
message("spatial.mask could not be used. Check valid input type.")
})}


}

terra::crs(raster) <- prj
Expand Down Expand Up @@ -522,10 +525,15 @@ dynamic_proj_covariates <- function(dates,
terra::res(r) <- spatial.res.degrees
r <- terra::setValues(r, 1:terra::ncell(r))


if(!missing(spatial.mask)) {

if(inherits(spatial.mask, "sfc_POLYGON")){
spatial.mask <- terra::vect(spatial.mask)
}
# Mask to original spatial.ext, if fails keep original. Depending on spatial.ext type.
tryCatch({
r <- terra::mask(r, spatial.ext)
r <- terra::mask(r, spatial.mask)
}, error = function(error_message) {
r <- r
message("spatial.mask could not be used. Check valid input type.")
Expand All @@ -540,6 +548,17 @@ dynamic_proj_covariates <- function(dates,
if (!length(resample.method) == 1) {
raster <- terra::resample(raster, r, method = resample.method[v])
}

if(!missing(spatial.mask)) {
# Mask to original spatial.ext, if fails keep original. Depending on spatial.ext type.
tryCatch({
raster <- terra::mask(raster, spatial.mask)
}, error = function(error_message) {
raster <- raster
message("spatial.mask could not be used. Check valid input type.")
})}


}

terra::crs(raster) <- prj
Expand All @@ -557,7 +576,7 @@ dynamic_proj_covariates <- function(dates,
names(stack) <- varnames_all # Label each layer in stack as variable

if (!prj == cov.prj) {
stack <- terra::project(stack, crs = cov.prj)
stack <- terra::project(stack, cov.prj)
}

cov.file.type <- match.arg(cov.file.type, choices = c("tif", "csv"))
Expand Down
9 changes: 4 additions & 5 deletions R/extract_buffered_coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#'
#' # Spatial dimension
#'
#' Using the `focal` function from `raster` R package (Hijmans et al., 2015), `GEE.math.fun` is
#' Using the `focal` function from `terra` R package (Hijmans et al., 2022), `GEE.math.fun` is
#' calculated across the spatial buffer area from the record co-ordinate. The spatial buffer area
#' used is specified by the argument `moving.window.matrix`, which dictates the neighbourhood of
#' cells surrounding the cell containing the occurrence record to include in this calculation.
Expand Down Expand Up @@ -101,7 +101,7 @@
#' # Aggregation factor
#'
#' `agg.factor` given represents the factor to aggregate `RasterLayer` data with function
#' `aggregate` in `raster` R package (Hijmans et al., 2015). Aggregation uses the `GEE.math.fun` as
#' `aggregate` in `terra` R package (Hijmans et al., 2022). Aggregation uses the `GEE.math.fun` as
#' the function. Following aggregation spatial buffering using the moving window matrix occurs.
#' This is included to minimise computing time if data are of high spatial resolution and a large
#' spatial buffer is needed. Ensure to calculate `get_moving_window()` with the spatial resolution
Expand Down Expand Up @@ -151,9 +151,8 @@
#' D'Agostino McGowan L., and Bryan J., 2022. googledrive: An Interface to Google Drive.
#' <https://googledrive.tidyverse.org>, <https://github.com/tidyverse/googledrive>.
#'
#' Hijmans, R. J., Van Etten, J., Cheng, J., Mattiuzzi, M., Sumner, M., Greenberg, J. A.,
#' Lamigueiro, O. P., Bevan, A., Racine, E. B. & Shortridge, A. 2015. Package ‘raster’. R package,
#' 734.
#'Hijmans, R.J., Bivand, R., Forner, K., Ooms, J., Pebesma, E. and Sumner, M.D.,
#'2022. Package 'terra'. Maintainer: Vienna, Austria.
#'@return Returns details of successful explanatory variable extractions.
#'@export
#'@examplesIf googledrive::drive_has_token()
Expand Down
15 changes: 7 additions & 8 deletions R/extract_buffered_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#'Extract rasters for spatially buffered and temporally dynamic explanatory variables at each
#'projection date using Google Earth Engine.
#'@param dates a character string, vector of dates in format "YYYY-MM-DD".
#'@param spatial.ext the spatial extent for the extracted raster. Object from which extent can be
#' extracted of class `Extent`, `RasterLayer`,`SpatialPolygonsDataFrame`, `sf` or `polygon` or
#'@param spatial.ext the spatial extent for the extracted raster. Object from which extent
#' can be extracted of class `SpatExtent`, `SpatRaster`, `sf` polygon or
#' numeric vector listing xmin, xmax, ymin and ymax in order.
#'@param datasetname a character string, the Google Earth Engine dataset to extract data from.
#'@param bandname a character string, the Google Earth Engine dataset bandname to extract data for.
Expand Down Expand Up @@ -62,7 +62,7 @@
#'
#' # Spatial dimension
#'
#' Using the `focal` function in `raster` R package (Hijmans et al., 2015), `GEE.math.fun` is
#' Using the `focal` function in `terra` R package (Hijmans et al., 2022), `GEE.math.fun` is
#' calculated across the spatial buffer area from each cell in `spatial.ext`. The spatial buffer
#' area used is defined by `moving.window matrix`, which dictates the neighbourhood of cells
#' surrounding each cell in `spatial.ext` to include in the calculation.
Expand Down Expand Up @@ -90,8 +90,8 @@
#'
#' # Aggregation factor
#'
#' `agg.factor` given represents the factor to aggregate `RasterLayer` data with function
#' `aggregate` in `raster` R package (Hijmans et al., 2015). Aggregation uses the `GEE.math.fun` as
#' `agg.factor` given represents the factor to aggregate `SpatRaster` data with function
#' `aggregate` in `terra` R package (Hijmans et al., 2022). Aggregation uses the `GEE.math.fun` as
#' the function. Following aggregation spatial buffering using the moving window matrix occurs.
#' This is included to minimise computing time if data are of high spatial resolution and a large
#' spatial buffer is needed. Ensure to calculate `get_moving_window()` with the spatial resolution
Expand Down Expand Up @@ -158,9 +158,8 @@
#' D'Agostino McGowan L., and Bryan J., 2022. googledrive: An Interface to Google Drive.
#' <https://googledrive.tidyverse.org>, <https://github.com/tidyverse/googledrive>.
#'
#' Hijmans, R. J., Van Etten, J., Cheng, J., Mattiuzzi, M., Sumner, M., Greenberg, J. A.,
#' Lamigueiro, O. P., Bevan, A., Racine, E. B. & Shortridge, A. 2015. Package ‘raster’. R package,
#' 734.
#'Hijmans, R.J., Bivand, R., Forner, K., Ooms, J., Pebesma, E. and Sumner, M.D.,
#'2022. Package 'terra'. Maintainer: Vienna, Austria.
#'@return Returns details of successful explanatory variable raster extractions for each projection
#' date.
#'@export
Expand Down
2 changes: 1 addition & 1 deletion R/extract_dynamic_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'Earth Engine.
#'@param dates a character string, vector of dates in format "YYYY-MM-DD".
#'@param spatial.ext the spatial extent for the extracted raster. Object from which extent can be
#' extracted of class `Extent`, `RasterLayer`, `SpatialPolygonsDataFrame`, `sf` or `polygon` or
#' extracted of class `SpatExtent`, `SpatRaster`, an `sf` polygon or a
#' numeric vector listing xmin, xmax, ymin and ymax in order.
#'@param datasetname a character string, the Google Earth Engine dataset to extract data from.
#'@param bandname a character string, the Google Earth Engine dataset bandname to extract data for.
Expand Down
Loading

0 comments on commit ddecca5

Please sign in to comment.