|
1 | | - |
2 | | -#' @title title |
| 1 | +#' @title inside mask |
3 | 2 | #' @description Checks whether longitude and latitude coincide with |
4 | 3 | #' non-missing pixels of a raster. The function takes two arguments: |
5 | | -#' points, a dataframe containing at a minimum columns named |
| 4 | +#' points, a dataframe containing at a minimum columns representing |
6 | 5 | #' longitude' and 'latitude' (but could include other attributes), |
7 | 6 | #' and mask is a raster. Returns a dataframe of the same dimensions as the |
8 | 7 | #' input object, containing only those rows with points falling on |
|
18 | 17 | #' @export |
19 | 18 | #' |
20 | 19 | #' @examples |
21 | | -inside_mask <- function(points, mask){ |
| 20 | +inside_mask <- function( |
| 21 | + points, |
| 22 | + mask, |
| 23 | + var_names = c("longitude", "latitude"), |
| 24 | + inside = TRUE |
| 25 | +) { |
| 26 | + # check whether arguments are in the correct format |
| 27 | + stopifnot( |
| 28 | + "mask must be a SpatRaster" = inherits(mask, 'SpatRaster') |
| 29 | + ) |
22 | 30 |
|
23 | | - # check whether arguments are in the correct format |
24 | | - stopifnot( |
25 | | - "mask must be a SpatRaster" = inherits(mask, 'SpatRaster') |
26 | | - ) |
| 31 | + stopifnot( |
| 32 | + "points object must be a data.frame." = inherits(points, 'data.frame') |
| 33 | + ) |
27 | 34 |
|
28 | | - stopifnot( |
29 | | - "points object must be a data.frame." = inherits(points, 'data.frame') |
| 35 | + if (sum(colnames(points) %in% c("longitude", "latitude")) != 2) { |
| 36 | + points <- points %>% |
| 37 | + rename( |
| 38 | + longitude = var_names[[1]], |
| 39 | + latitude = var_names[[2]] |
30 | 40 | ) |
| 41 | + } |
31 | 42 |
|
32 | | - stopifnot( |
33 | | - "points data.frame must contain columns 'longitude' and 'latitude'." = sum(colnames(points) %in% c("longitude", "latitude"))==2 |
34 | | - ) |
| 43 | + stopifnot( |
| 44 | + "var_names for longitude and latitude from points data.frame must be numeric" = sum( |
| 45 | + colnames(points) %in% c("longitude", "latitude") |
| 46 | + ) == |
| 47 | + 2 |
| 48 | + ) |
35 | 49 |
|
36 | | - # get indexes of points which fall inside the mask |
37 | | - inside_idx <- which( |
38 | | - !(is.na(terra::extract(mask, points[,c('longitude', 'latitude')], ID=FALSE))) |
39 | | - ) |
| 50 | + # get indexes of points which fall inside the mask |
| 51 | + inside_idx <- which( |
| 52 | + !(is.na(terra::extract( |
| 53 | + mask, |
| 54 | + points[, c('longitude', 'latitude')], |
| 55 | + ID = FALSE |
| 56 | + ))) |
| 57 | + ) |
40 | 58 |
|
41 | | - # subset these points |
42 | | - inside_points <- points[inside_idx, ] |
| 59 | + # subset the points according to inside or outside criteria |
43 | 60 |
|
44 | | - # if raster values exist for one point or more, return the point/s, |
45 | | - # otherwise throw an error |
46 | | - if (nrow(inside_points)==0) {stop('all points outside mask')} |
47 | | - |
48 | | - return (inside_points) |
| 61 | + if (inside) { |
| 62 | + subset_points <- points[inside_idx, ] |
| 63 | + } else { |
| 64 | + subset_points <- points[!inside_idx, ] |
| 65 | + } |
49 | 66 |
|
| 67 | + # if raster values exist for one point or more, return the point/s, |
| 68 | + # otherwise throw an error |
| 69 | + if (nrow(points[inside_idx, ]) == 0) { |
| 70 | + stop('all points outside mask') |
50 | 71 | } |
| 72 | + |
| 73 | + return(subset_points) |
| 74 | +} |
0 commit comments