-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_pt_gen.R
163 lines (144 loc) · 8.85 KB
/
random_pt_gen.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
#'Randomly generate sampling points within a supplied area
#'
#'Randomly generate sampling points at a specified density and buffer radius, within
#'supplied polygons. An excess of sampling points can be generated,
#'in case some of the generated points are unsuitable for surveys (e.g. within inaccessible areas).
#'If necessary, stratified random sampling can be performed to ensure sufficient representation
#'between sub-areas of interest. By supplying the `sub_areas` of interest,
#'sampling points will be stratified between the areas outside ('normal' areas) and within these 'sub-areas'.
#'
#'@param boundaries Shapefile (polygon or multipolygon) of site boundaries for sampling.
#'@param area_per_pt Area component for point density in square meters (e.g. one point every 50 hectares, area_per_pt = 500000).
#'@param pt_radius Buffer radius of survey points in meters.
#'Ensures that the generated points do not extend beyond the boundaries of the area of interest.
#'@param excess_modifier Numeric value of a multiplicative modifier for excess points to be generated (i.e. a value of 1.5 = 1.5 times the expected number of points will be generated). The default value is '1'.
#'@param sub_areas Shapefile (polygon or multipolygon) of sub-area of interest,
#'where sampling points will be generated separately (stratified random sampling).
#'@param retain Shapefile (point) of previous round of surveys to retain in the new set of survey points.
#'The column `type` must exist, with the value of either `Normal` or `Sub-area`.
#'@param retain_prop Value of 0 to 1 for proportion of points to retain from previous round of surveys.
#'
#'@return The sf object `sampling_points` containing the geographical coordinates of generated points.
#'
#'@import dplyr
#'@importFrom sf st_read st_cast st_buffer st_difference st_area st_sample st_as_sf st_intersection st_combine st_zm
#'@importFrom rlang .data
#'
#'@export
random_pt_gen <- function(boundaries, area_per_pt, pt_radius, excess_modifier = 1,
sub_areas = NULL, retain = NULL, retain_prop = NULL) {
suppressWarnings(bound_buffer <- sf::st_cast(boundaries, "LINESTRING") %>%
sf::st_buffer(pt_radius))
suppressWarnings(boundaries <- sf::st_difference(boundaries, bound_buffer))
rm(bound_buffer)
# cat(sf::st_crs(boundaries))
n <- ceiling(sf::st_area(boundaries)/area_per_pt) %>%
as.numeric()
points <- sf::st_sample(boundaries, ceiling(n * excess_modifier), type = "random") %>%
sf::st_as_sf() %>%
dplyr::mutate(type = "Normal") %>%
dplyr::mutate(id = dplyr::row_number()) %>%
dplyr::mutate(id = paste0("normal", id))
cat("\n", paste0("Total number of points required: ", n, ".\n"))
if (!is.null(sub_areas)) {
sub_areas <- sub_areas %>%
sf::st_union()
boundaries_all <- boundaries
suppressWarnings(boundaries <- sf::st_difference(boundaries_all, sub_areas))
suppressWarnings(sub_areas <- sf::st_intersection(boundaries_all, sub_areas))
rm(boundaries_all)
n <- ceiling(sf::st_area(boundaries)/area_per_pt) %>%
as.numeric()
n_subarea <- ceiling(sf::st_area(sub_areas)/area_per_pt) %>%
as.numeric()
points <- sf::st_sample(boundaries, ceiling(n * excess_modifier),
type = "random") %>%
sf::st_as_sf() %>%
dplyr::mutate(type = "Normal") %>%
dplyr::mutate(id = dplyr::row_number()) %>%
dplyr::mutate(id = paste0("normal", id))
points_subarea <- sf::st_sample(sub_areas, ceiling(n_subarea * excess_modifier),
type = "random") %>%
sf::st_as_sf() %>%
dplyr::mutate(type = "Sub-area") %>%
dplyr::mutate(id = dplyr::row_number()) %>%
dplyr::mutate(id = paste0("subarea", id))
cat("\n", paste0("Number of points required within 'normal' areas: ", n, "."))
cat("\n", paste0("Number of points required within 'sub-areas': ", n_subarea, "."))
cat("\n")
if (!is.null(retain) & !is.null(retain_prop)) {
retain <- retain %>%
sf::st_as_sf() %>%
dplyr::rename(id = 1) %>%
dplyr::select(id, .data$type, .data$geometry) %>%
sf::st_zm(drop = TRUE, what = "ZM")
retain_subarea <- dplyr::filter(retain, .data$type == "Sub-area") %>%
dplyr::select(-.data$type) %>%
dplyr::mutate(status = "Old")
retain <- retain %>%
dplyr::select(-.data$type) %>%
dplyr::mutate(status = "Old")
retain_n <- retain_prop * n
retain_n_subarea <- retain_prop * n_subarea
points <- sf::st_sample(boundaries, ceiling(ceiling(n - retain_n) *
excess_modifier), type = "random") %>%
sf::st_as_sf() %>%
dplyr::rename(geometry = .data$x) %>%
dplyr::mutate(id = dplyr::row_number()) %>%
dplyr::mutate(id = paste0("normal", id)) %>%
dplyr::mutate(status = "New") %>%
rbind(., retain[sample(1:nrow(retain), ceiling(retain_n)), ]) %>%
dplyr::mutate(type = "Normal")
points_subarea <- sf::st_sample(sub_areas, ceiling(ceiling(n_subarea - retain_n_subarea) *
excess_modifier), type = "random") %>%
sf::st_as_sf() %>%
dplyr::rename(geometry = .data$x) %>%
dplyr::mutate(id = dplyr::row_number()) %>%
dplyr::mutate(id = paste0("subarea", id)) %>%
dplyr::mutate(status = "New") %>%
rbind(., retain_subarea[sample(1:nrow(retain_subarea), ceiling(retain_n_subarea)),
]) %>%
dplyr::mutate(type = "Sub-area")
cat("\n", paste0("Number of points within 'normal' areas that were retained: ", ceiling(retain_n), " (n = ", n, ", retain_prop = ", retain_prop, ")."))
cat("\n", paste0("Number of points within 'sub-areas' that were retained: ", ceiling(retain_n_subarea), " (n = ", n_subarea, ", retain_prop = ", retain_prop, ")."))
cat("\n", paste0("Number of new points within 'normal' areas that were generated: ", ceiling((n-retain_n)*excess_modifier), " (n = ", (n-retain_n), ", excess_modifier = ", excess_modifier, ")."))
cat("\n", paste0("Number of new points within 'sub-areas' that were generated: ", ceiling((n_subarea-retain_n_subarea)*excess_modifier), " (n = ", (n_subarea-retain_n_subarea), ", excess_modifier = ", excess_modifier, ").\n"))
rm(retain_subarea, retain_n, retain_n_subarea)
} else if (is.null(retain) & !is.null(retain_prop) | !is.null(retain) &
is.null(retain_prop)) {
stop("Either arguments 'retain' or 'retain_prop' is missing.")
} else {
cat("\n", paste0("Number of points within 'normal' areas that were generated: ", ceiling(n*excess_modifier), " (n = ", n, ", excess_modifier = ", excess_modifier, ")."))
cat("\n", paste0("Number of points within 'sub-areas' that were generated: ", ceiling(n_subarea*excess_modifier), " (n = ", n_subarea, ", excess_modifier = ", excess_modifier, ").\n"))
}
points <- rbind(points, points_subarea)
rm(n, n_subarea, points_subarea)
} else {
if (!is.null(retain) & !is.null(retain_prop)) {
retain <- retain %>%
sf::st_as_sf() %>%
dplyr::rename(id = 1) %>%
dplyr::select(id, .data$geometry) %>%
sf::st_zm(drop = TRUE, what = "ZM") %>%
dplyr::mutate(status = "Old")
retain_n <- retain_prop * n
points <- sf::st_sample(boundaries, ceiling(ceiling(n - retain_n) *
excess_modifier), type = "random") %>%
sf::st_as_sf() %>%
dplyr::rename(geometry = x) %>%
dplyr::mutate(id = dplyr::row_number()) %>%
dplyr::mutate(id = paste0("normal", id)) %>%
dplyr::mutate(status = "New") %>%
rbind(., retain[sample(1:nrow(retain), retain_n), ]) %>%
dplyr::mutate(type = "Normal") %>%
cat("\n", paste0("Number of points within 'normal' areas that were retained: ", retain_n, " (n = ", n, ", retain_prop = ", retain_prop, ")."))
cat("\n", paste0("Number of points within 'normal' areas that were generated: ", ceiling((n-retain_n)*excess_modifier), " (n = ", (n-retain_n), ", excess_modifier = ", excess_modifier, ").\n"))
} else if (is.null(retain) & !is.null(retain_prop) | !is.null(retain) &
is.null(retain_prop)) {
stop("Either arguments 'retain' or 'retain_prop' is missing.")
} else {
cat("\n", paste0("Number of points within 'normal' areas that were generated: ", ceiling(n*excess_modifier), " (n = ", n, ", excess_modifier = ", excess_modifier, ").\n"))
}
}
return(points)
}