Skip to content

Commit 0133db8

Browse files
committed
Merge branch 'main' of https://github.com/idem-lab/sdmtools
2 parents 40bab61 + d7c2f51 commit 0133db8

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

R/inside_mask.R

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
2-
#' @title title
1+
#' @title inside mask
32
#' @description Checks whether longitude and latitude coincide with
43
#' 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
65
#' longitude' and 'latitude' (but could include other attributes),
76
#' and mask is a raster. Returns a dataframe of the same dimensions as the
87
#' input object, containing only those rows with points falling on
@@ -18,33 +17,58 @@
1817
#' @export
1918
#'
2019
#' @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+
)
2230

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+
)
2734

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]]
3040
)
41+
}
3142

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+
)
3549

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+
)
4058

41-
# subset these points
42-
inside_points <- points[inside_idx, ]
59+
# subset the points according to inside or outside criteria
4360

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+
}
4966

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')
5071
}
72+
73+
return(subset_points)
74+
}

0 commit comments

Comments
 (0)