/
BackgroundSamplingFunctions.R
353 lines (328 loc) · 10.8 KB
/
BackgroundSamplingFunctions.R
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#' @title Occurrence cell removal
#'
#' @description Removes cells from raster that contain occurrences
#'
#' @param occs A `data.frame` with at least two columns
#' named "longitude" and "latitude" or that
#' can be coerced into this format.
#'
#' @param rasterTemplate A `SpatRaster` object to serve
#' as a template for cells to be removed.
#'
#' @details This is an internal function to remove cells that
#' intersect with occurrences from a `SpatRaster` template object. This
#' template can then be overlaid onto a `SpatRaster` vector to
#' remove occurrences from all layers.
#'
#' @return A `SpatRaster`
#'
#' @examples
#'
#' library(terra)
#' # Create sample raster
#' r <- rast(ncol=10, nrow=10)
#' values(r) <- 1:100
#'
#' # Create test occurrences
#' set.seed(0)
#' longitude <- sample(ext(r)[1]:ext(r)[2],
#' size = 10, replace = FALSE)
#' set.seed(0)
#' latitude <- sample(ext(r)[3]:ext(r)[4],
#' size = 10, replace = FALSE)
#' occs <- data.frame(longitude, latitude)
#'
#' # Here's the function
#' result <- occCellRemoval(occs = occs, rasterTemplate = r)
#'
#' @import terra
#'
#' @keywords internal
#'
#' @noRd
occCellRemoval <- function(occs, rasterTemplate){
# Handling alternative column names for occurrences
colNames <- colnames(occs)
cp <- columnParse(occs)
xIndex <- cp$xIndex
yIndex <- cp$yIndex
# Meat of function
occCells <- cellFromXY(object = rasterTemplate, occs[,c(xIndex,yIndex)])
rasterTemplate[occCells] <- NA
return(rasterTemplate)
}
#' @title 2D background sampling
#'
#' @description Samples in 2D at resolution of raster
#'
#' @param occs A dataframe with at least two columns
#' named "longitude" and "latitude", or that can be
#' coerced into this format.
#'
#' @param rasterTemplate A `SpatRaster` object to serve
#' as a template for generating background sampling
#' coordinates.
#'
#' @param mShp A shapefile defining the area from
#' which background points should be sampled.
#'
#' @param verbose `logical`. Switching to `FALSE` mutes message describing
#' which columns in `occs` are interpreted as x and y coordinates.
#'
#' @details This function is designed to sample background points
#' for distributional modeling in two dimensions. The returned
#' `data.frame` contains all points from across the designated
#' background. It is up to the user to determine how to
#' appropriately sample from those background points.
#'
#' @return A `data.frame` with 2D coordinates of points
#' for background sampling.
#'
#' @examples
#' library(terra)
#'
#' # Create sample raster
#' r <- rast(ncol=10, nrow=10)
#' values(r) <- 1:100
#'
#' # Create test occurrences
#' set.seed(0)
#' longitude <- sample(ext(r)[1]:ext(r)[2],
#' size = 10, replace = FALSE)
#' set.seed(0)
#' latitude <- sample(ext(r)[3]:ext(r)[4],
#' size = 10, replace = FALSE)
#' occurrences <- data.frame(longitude,latitude)
#'
#' # Generate background sampling buffer
#' buffPts <- vect(occurrences,
#' c("longitude", "latitude"))
#' crs(buffPts) <- crs(r)
#' mShp <- aggregate(buffer(buffPts, width = 1000000))
#'
#' # Here's the function
#' result <- mSampling2D(occs = occurrences, rasterTemplate = r, mShp = mShp)
#'
#' @import terra
#'
#' @keywords backgroundSampling
#'
#' @export
mSampling2D <- function(occs, rasterTemplate, mShp, verbose = TRUE){
if(!is.data.frame(occs)){
warning(paste0("'occs' must be an object of class 'data.frame'.\n"))
return(NULL)
}
if(!grepl("SpatRaster", class(rasterTemplate))){
warning(paste0("'rasterTemplate' must be of class 'SpatRaster'.\n"))
return(NULL)
}
if(!is(mShp, "SpatVector")){
warning(paste0("'mShp' must be of class 'SpatVector'.\n"))
return(NULL)
}
if (!is.logical(verbose)) {
warning(paste0("Argument 'verbose' is not of type 'logical'.\n"))
return(NULL)
}
# Parse columns
colNames <- colnames(occs)
colParse <- columnParse(occs)
if(is.null(colParse)){
return(NULL)
}
xIndex <- colParse$xIndex
yIndex <- colParse$yIndex
interp <- colParse$reportMessage
if(verbose){
message(interp)
}
rasterTemplate <- crop(x = mask(x = rasterTemplate, mask = mShp), y = mShp)
rawLayer <- occCellRemoval(occs = occs[,c(xIndex,yIndex)],
rasterTemplate)
mPts <- data.frame(xyFromCell(rawLayer, cell = 1:ncell(rawLayer)))
mPts$extract <- extract(x = rawLayer, y = mPts)[,2]
mPts <- mPts[!is.na(mPts$extract),1:2]
colnames(mPts) <- colNames[c(xIndex, yIndex)]
return(mPts)
}
#' @title 3D background sampling
#'
#' @description Samples XYZ coordinates from a shapefile
#' from maximum to minimum occurrence depth at XYZ
#' resolution of envBrick.
#'
#' @param occs A `data.frame` with at least three columns
#' named "longitude", "latitude", and "depth", or that
#' can be coerced into this format.
#'
#' @param envBrick A `SpatRaster` vector object to serve
#' as a template for generating background sampling
#' coordinates.
#'
#' @param mShp A shapefile defining the area from
#' which background points should be sampled.
#'
#' @param depthLimit An argument controlling the depth
#' extent of sampling. Refer to `Details` for more information.
#'
#' @param verbose `logical`. Switching to `FALSE` mutes message describing
#' which columns in `occs` are interpreted as x, y, and z coordinates.
#'
#' @details This function is designed to sample background points for
#' distributional modeling in three dimensions. If a voxel (3D pixel)
#' in the `SpatRaster` vector intersects with an occurrence from
#' `occs`, it is removed. Note that this function returns points
#' representing every voxel in the background area within the
#' specified depth range. It is up to the user to downsample from
#' these data as necessary, depending on the model type being used.
#'
#' `depthLimit` argument options:
#' \itemize{
#' \item `occs` Samples background from the full depth extent of `occs`.
#' \item `all` Samples background from the full depth extent of `envBrick`.
#' \item A `vector` of length 2 with maximum and minimum depth values from
#' which to sample.
#' }
#'
#' @return A `data.frame` with 3D coordinates of points for background
#' sampling.
#'
#' @examples
#' library(terra)
#'
#' # Create test raster
#' r1 <- rast(ncol=10, nrow=10)
#' values(r1) <- 1:100
#' r2 <- rast(ncol=10, nrow=10)
#' values(r2) <- c(rep(20, times = 50), rep(60, times = 50))
#' r3 <- rast(ncol=10, nrow=10)
#' values(r3) <- 8
#' envBrick <- c(r1, r2, r3)
#' names(envBrick) <- c(0, 10, 30)
#'
#' # Create test occurrences
#' set.seed(0)
#' longitude <- sample(ext(envBrick)[1]:ext(envBrick)[2],
#' size = 10, replace = FALSE)
#' set.seed(0)
#' latitude <- sample(ext(envBrick)[3]:ext(envBrick)[4],
#' size = 10, replace = FALSE)
#' set.seed(0)
#' depth <- sample(0:35, size = 10, replace = TRUE)
#' occurrences <- data.frame(longitude,latitude,depth)
#'
#' # Generate background sampling buffer
#' buffPts <- vect(occurrences,
#' c("longitude", "latitude"))
#' crs(buffPts) <- crs(envBrick)
#' mShp <- aggregate(buffer(buffPts, width = 1000000))
#'
#' # Here's the function
#' occSample3d <- mSampling3D(occs = occurrences,
#' envBrick = envBrick,
#' mShp = mShp,
#' depthLimit = "occs")
#'
#' @import terra
#'
#' @keywords backgroundSampling
#'
#' @export
mSampling3D <- function(occs, envBrick, mShp, depthLimit = "all", verbose = TRUE){
if(!is.data.frame(occs)){
warning(paste0("'occs' must be an object of class 'data.frame'.\n"))
return(NULL)
}
if(ncol(occs) < 3){
warning(paste0("'occs' must have at least three columns.\n"))
return(NULL)
}
if(!is(envBrick, "SpatRaster")){
warning(paste0("'envBrick' must be of class 'SpatRaster'.\n"))
return(NULL)
}
if(!is(mShp, "SpatVector")){
warning(paste0("'mShp' must be of class 'SpatVector'.\n"))
return(NULL)
}
if(!(class(depthLimit) %in% c("character","numeric"))){
warning(paste0("'depthLimit' must be of class 'character' or 'numeric'.\n"))
return(NULL)
}
if(is(depthLimit, "numeric")){
if(length(depthLimit) != 2){
warning(paste0("'depthLimit' arguments of 'numeric' must be of length 2.\n"))
return(NULL)
}
}
if(is(depthLimit, "character")){
if(length(depthLimit) > 1){
warning(paste0(depthLimit, " is not a valid value for 'depthLimit'\n"))
return(NULL)
} else if(!(depthLimit %in% c("all", "occs"))){
warning(paste0(depthLimit, " is not a valid value for 'depthLimit'\n"))
return(NULL)
}
}
if (!is.logical(verbose)) {
warning(paste0("Argument 'verbose' is not of type 'logical'.\n"))
return(NULL)
}
# Parse columns
colNames <- colnames(occs)
colParse <- columnParse(occs, wDepth = TRUE)
if(is.null(colParse)){
return(NULL)
}
xIndex <- colParse$xIndex
yIndex <- colParse$yIndex
zIndex <- colParse$zIndex
interp <- colParse$reportMessage
if(verbose){
message(interp)
}
# Checking for appropriate environmental layer names
layerNames <- as.numeric(gsub("[X]", "", names(envBrick)))
if(sum(is.na(layerNames)) > 0){
message("\nInput SpatRaster vector names inappropriate: \n",
paste(names(envBrick), collapse = ", "), "\n",
"Names must follow the format 'X' ",
"followed by a number corresponding to ",
"the starting depth of the layer.")
return(NULL)
}
# Depth slice indices for occurrences
occs$index <- unlist(lapply(occs[,zIndex],
FUN = function(x) which.min(abs(layerNames - x))))
# Get depth range
if(is(depthLimit, "numeric")){
depthRange <- c(which.min(abs(layerNames - min(depthLimit))),
which.min(abs(layerNames - max(depthLimit))))
} else if(depthLimit == "occs"){
depthRange <- c(min(occs$index), max(occs$index))
} else {
depthRange <- c(1, nlyr(envBrick))
}
envBrick <- crop(x = mask(x = envBrick[[depthRange[[1]]:depthRange[[2]]]],
mask = mShp), y = mShp)
mPts <- data.frame()
for(i in 1:length(names(envBrick))){
rawLayer <- envBrick[[i]]
layerDepth <- as.numeric(gsub("[X]", "", names(rawLayer)))
occsAtLayerDepth <- occs[occs$index == match(layerDepth, layerNames),]
if (nrow(occsAtLayerDepth) > 0){
rawLayer <- occCellRemoval(occs = occsAtLayerDepth[,c(xIndex,yIndex)],
rawLayer)
}
tempPoints <- data.frame(xyFromCell(rawLayer,
cell = 1:ncell(rawLayer)))
tempPoints$extract <- extract(x = rawLayer, y = tempPoints)[,2]
tempPoints <- tempPoints[!is.na(tempPoints$extract),1:2]
colnames(tempPoints) <- colNames[c(xIndex, yIndex)]
tempPoints[,zIndex] <- rep(layerDepth, times = nrow(tempPoints))
mPts <- rbind(mPts, tempPoints)
}
colnames(mPts)[[3]] <- colNames[[zIndex]]
return(mPts)
}