-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer_sampling.R
More file actions
57 lines (56 loc) · 1.9 KB
/
buffer_sampling.R
File metadata and controls
57 lines (56 loc) · 1.9 KB
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
#' Distance sample
#'
#' Function to sample buffer points in XY space:
#' Returns the original data table with buffered points removed.
#'
#'
#' @param foo a data.frame to select from with columns x, y
#' @param buffer the minimum distance between output points
#' @param reps the number of repetitions for the points selection
#'
#' @return data frame sample
#'
#' @author David R. Roberts
#'
#' @references https://davidrroberts.wordpress.com/2015/09/25/spatial-buffering-of-points-in-r-while-retaining-maximum-sample-size/
#'
#' Runs numerous iterations, as the random point selection can result in more/fewer output points.
#' 1) Randomly select a single point
#' 2) Remove points within 50km of that point
#' 3) Randomly select of the remaining points
#' 4) ...
#'
buffer.f <- function(foo, buffer, reps){
# Make list of suitable vectors
suitable <- list()
for(k in 1:reps){
# Make the output vector
outvec <- as.numeric(c())
# Make the vector of dropped (buffered out) points
dropvec <- c()
for(i in 1:nrow(foo)){
# Stop running when all points exhausted
if(length(dropvec)<nrow(foo)){
# Set the rows to sample from
if(i>1){
rowsleft <- (1:nrow(foo))[-c(dropvec)]
} else {
rowsleft <- 1:nrow(foo)
}
# Randomly select point
outpoint <- as.numeric(sample(as.character(rowsleft),1))
outvec[i] <- outpoint
# Remove points within buffer
outcoord <- foo[outpoint,c("x","y")]
dropvec <- c(dropvec, which(sqrt((foo$x-outcoord$x)^2 + (foo$y-outcoord$y)^2)<buffer))
# Remove unnecessary duplicates in the buffered points
dropvec <- dropvec[!duplicated(dropvec)]
}
}
# Populate the suitable points list
suitable[[k]] <- outvec
}
# Go through the iterations and pick a list with the most data
best <- unlist(suitable[which.max(lapply(suitable,length))])
foo[best,]
}