-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-gridding.R
412 lines (344 loc) · 11.1 KB
/
test-gridding.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
## par_pad_grid ####
testthat::test_that("par_pad_grid with sf inputs", {
withr::local_package("sf")
withr::local_package("stars")
withr::local_package("terra")
withr::local_options(list(sf_use_s2 = FALSE))
# starts from sf/stars
nc <- system.file(package = "sf", "shape/nc.shp")
nc <- sf::read_sf(nc)
nc <- sf::st_transform(nc, "EPSG:5070")
# main tests
ncgrid <- par_pad_grid(nc, mode = "grid", padding = 3e4L)
testthat::expect_s3_class(ncgrid$original, "sf")
})
testthat::test_that("par_pad_grid with terra inputs", {
nc <- system.file(package = "sf", "shape/nc.shp")
nc <- sf::read_sf(nc)
nc <- sf::st_transform(nc, "EPSG:5070")
nctr <- terra::vect(nc)
ncgridtr <- par_pad_grid(nctr, mode = "grid", padding = 3e4L)
testthat::expect_s4_class(ncgridtr$original, "SpatVector")
# non-integer nx, ny
testthat::expect_error(
par_pad_grid(nctr, mode = "grid", nx = 3.6, ny = 10L, padding = 3e4L),
"nx, ny must be integer."
)
testthat::expect_error(
testthat::expect_warning(
testthat::expect_message(
par_pad_grid(nctr, mode = "grid", nx = 4L, ny = 10L, padding = "july"),
"padding should be numeric. Try converting padding to numeric..."
),
"NAs introduced by coercion"
),
"padding is not convertible to numeric or converted to NA."
)
})
testthat::test_that("par_pad_grid with other modes", {
withr::local_package("sf")
withr::local_package("stars")
withr::local_package("terra")
withr::local_options(list(sf_use_s2 = FALSE))
nc <- system.file(package = "sf", "shape/nc.shp")
nc <- sf::read_sf(nc)
nc <- sf::st_transform(nc, "EPSG:5070")
ncp <- readRDS(system.file("extdata/nc_random_point.rds", package = "chopin"))
ncp <- sf::st_transform(ncp, "EPSG:5070")
ncrp <- sf::st_as_sf(sf::st_sample(nc, 1000L))
# Points
testthat::expect_message(
testthat::expect_message(
ppg_adv <-
par_pad_grid(
nx = 10L, ny = 10L,
input = ncp,
mode = "grid_advanced",
padding = 3e4L,
grid_min_features = 10L
),
"Switch sf class to terra..."
),
"Switch terra class to sf..."
)
testthat::expect_lt(nrow(ppg_adv$original), 100)
# Points -- grid_quantile mode
testthat::expect_no_error(
par_pad_grid(
ncp,
mode = "grid_quantile",
padding = 3e4L,
quantiles = chopin:::par_def_q(5L)
)
)
})
## par_make_grid
testthat::test_that("par_make_grid file path input", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_options(list(sf_use_s2 = FALSE))
nc <- system.file("shape/nc.shp", package = "sf")
nc <- sf::read_sf(nc)
nc <- sf::st_transform(nc, "EPSG:5070")
suppressWarnings(ncc <- sf::st_centroid(nc, of_largest_polygon = TRUE))
tmpdir <- tempdir()
filepath <- file.path(tmpdir, "cents.shp")
sf::st_write(ncc, filepath, append = FALSE)
testthat::expect_no_error(
ncc_mg <- chopin:::par_make_grid(filepath, ncutsx = 10L, ncutsy = 10L)
)
testthat::expect_true(inherits(ncc_mg, "sf"))
})
## grid merge ####
testthat::test_that("Grid merging internal function too low threshold-- ", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_package("igraph")
withr::local_package("dplyr")
withr::local_options(list(sf_use_s2 = FALSE))
withr::local_seed(202403)
nc <- system.file("shape/nc.shp", package = "sf")
nc <- sf::read_sf(nc)
nc <- sf::st_transform(nc, "EPSG:5070")
nctr <- terra::vect(nc)
ncp <- readRDS(system.file("extdata/nc_random_point.rds", package = "chopin"))
ncp <- sf::st_transform(ncp, "EPSG:5070")
ncrp <- sf::st_as_sf(sf::st_sample(nc, 1600L))
gridded <-
par_pad_grid(
ncrp,
mode = "grid",
nx = 8L, ny = 5L,
padding = 1e4L
)
# messaging: too low threshold for merging
# in the case below, the grids with less than 2 points
# are too dispersed to be merged (i.e., no rook neighbors among them)
testthat::expect_message(
par_merge_grid(ncrp, gridded$original, 2L),
paste0(
"Threshold is too low. Return the original grid.\n",
"Please try higher threshold. your threshold: 2\n",
"Top-10 non-zero number of points in grids: ",
"2, 2, 5, 6, 11, 12, 15, 16, 25, 29"
)
)
ncrp2 <- sf::st_as_sf(sf::st_sample(nc, 10000L))
# points are too dense such that there are no grids to merge
testthat::expect_message(
par_merge_grid(ncrp2, gridded$original, 10L),
"No grid to merge."
)
# terra SpatVector input
ncptr <- terra::vect(ncrp2)
griddedtr <-
par_pad_grid(
ncptr,
mode = "grid",
nx = 8L, ny = 5L,
padding = 1e4L
)
testthat::expect_message(
par_merge_grid(ncptr, griddedtr$original, 10L)
)
})
testthat::test_that("Grid merging internal -- Polsby-Popper test fails.", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_package("igraph")
withr::local_package("dplyr")
# pp test fails
# Then expect warnings of "all sub-geometries for which ..."
data("ncpoints", package = "chopin")
ncptr2 <- terra::vect(ncpoints, geom = c("X", "Y"), keepgeom = TRUE)
griddedtr22 <-
par_pad_grid(
ncptr2,
mode = "grid",
nx = 40L, ny = 20L,
padding = 1e4L
)
testthat::expect_message(
{
gridmergedx <-
par_merge_grid(
ncptr2,
griddedtr22$original,
10L,
merge_max = 10L
)
},
"The merged polygons have too complex shapes.\nIncrease threshold or use the original grids."
)
})
testthat::test_that("par_pad_balanced -- input validity", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_package("anticlust")
withr::local_options(list(sf_use_s2 = FALSE))
# generate length of 500 vector
rv <- terra::vect(matrix(rnorm(1000, 40, 10), ncol = 2), crs = "EPSG:4326")
rs <- sf::st_as_sf(rv)
# n_clusters type condition
testthat::expect_error(
par_pad_balanced(rv, "NUMBER", 0L),
"n_clusters should be numeric."
)
# n_clusters range condition
testthat::expect_error(
par_pad_balanced(rv, 1L, 1000L),
"n_clusters should be greater than 1."
)
# n_clusters can be nonnumeric which is convertible to numeric
testthat::expect_message(
par_pad_balanced(rv, 5L, "10000"),
"padding should be numeric. Try converting padding to numeric..."
)
# par_make_balanced: ID assignment
testthat::expect_true(
"CGRIDID" %in% names(chopin:::par_make_balanced(rv, 10))
)
})
testthat::test_that("par_pad_balanced -- output check", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_package("anticlust")
withr::local_options(list(sf_use_s2 = FALSE))
# generate length of 500 vector
rv <- terra::vect(matrix(rnorm(1000, 40, 10), ncol = 2), crs = "EPSG:4326")
rs <- sf::st_as_sf(rv)
# gridded
pgg_terra <- par_pad_balanced(rv, 10, 100)
pgg_sf <- par_pad_balanced(rs, 10, 100)
# #fields
testthat::expect_equal(length(pgg_terra), 2)
testthat::expect_equal(length(pgg_sf), 2)
# equal partitioning
testthat::expect_true(all(table(pgg_terra$original$CGRIDID) == 50))
})
testthat::test_that("par_make_balanced internal -- input validity", {
# testthat::skip_on_ci()
withr::local_package("sf")
withr::local_package("terra")
withr::local_package("anticlust")
withr::local_options(list(sf_use_s2 = FALSE))
testthat::expect_error(
chopin:::par_make_balanced(rv, NULL)
)
testthat::expect_error(
chopin:::par_make_balanced(rv, "five")
)
testthat::expect_error(
suppressWarnings(chopin:::par_make_balanced(rv, 5L, "radius"))
)
testthat::expect_error(
chopin:::par_make_balanced(rv, 5L, NA)
)
})
testthat::test_that("Quantile cut internal tests", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_options(list(sf_use_s2 = FALSE))
rv <- terra::vect(matrix(rpois(100, 8), ncol = 2))
rs <- sf::st_as_sf(rv)
# numeric cases
randpoints <- data.frame(
x = runif(1000, 0, 100),
y = runif(1000, 0, 100)
)
N <- 4L
quantiles <- chopin:::par_def_q(N)
testthat::expect_equal(length(quantiles), 5)
testthat::expect_error(
chopin:::par_def_q(1L),
"steps should be greater than 1."
)
testthat::expect_error(
chopin:::par_cut_coords(randpoints$x, randpoints$y[seq(1, 10)], quantiles),
"x and y should have the same length."
)
testthat::expect_error(
chopin:::par_cut_coords(randpoints$x, c(1, 0, 4), quantiles),
"x and y should have the same length."
)
# output should have Nquantiles * Nquantiles elements
testthat::expect_equal(
nrow(chopin:::par_cut_coords(randpoints$x, randpoints$y, quantiles)), N^2
)
# polygon case
ncpath <- system.file("gpkg/nc.gpkg", package = "sf")
nc <- sf::st_read(ncpath)
testthat::expect_warning(
testthat::expect_warning(
chopin:::par_cut_coords(nc, NULL, chopin:::par_def_q(3L)),
"st_centroid assumes attributes are constant over geometries"
),
"st_centroid does not give correct centroids for longitude/latitude"
)
})
testthat::test_that("par_pad_balanced exceptions", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_options(list(sf_use_s2 = FALSE))
ncpath <- system.file("shape/nc.shp", package = "sf")
nc <- read_sf(ncpath)
nc <- st_transform(nc, "EPSG:5070")
suppressWarnings(
ncc <- st_centroid(nc, of_largest_polygon = TRUE)
)
testthat::expect_error(
chopin::par_pad_balanced(ncc, padding = 1300)
)
testthat::expect_error(
chopin::par_pad_balanced(ncc, ngroups = 10L)
)
# tmpdir <- tempdir()
# filepath <- file.path(tmpdir, "cents.shp")
# sf::st_write(ncc, filepath, append = FALSE)
# testthat::expect_no_error(
# ncc_ppb <- chopin::par_pad_balanced(filepath, ngroups = 10L, padding = 1e4)
# )
# testthat::expect_true(inherits(ncc_ppb[[1]], "SpatVector"))
})
# par_split_list tests ####
testthat::test_that("par_split_list returns correct output", {
withr::local_package("sf")
withr::local_package("terra")
withr::local_options(list(sf_use_s2 = FALSE))
ncpath <- system.file("shape/nc.shp", package = "sf")
nc <- read_sf(ncpath)
nc <- st_transform(nc, "EPSG:5070")
nc_comp_region <-
par_pad_grid(
nc,
mode = "grid",
nx = 4L, ny = 2L,
padding = 10000
)
ps <- par_split_list(nc_comp_region)
testthat::expect_true(is.list(ps))
testthat::expect_equal(length(ps), 8)
# WKT mode
nc_comp_region_wkt <-
par_pad_grid(
nc,
mode = "grid",
nx = 4L, ny = 2L,
padding = 10000,
return_wkt = TRUE
)
psw <- par_split_list(nc_comp_region_wkt)
testthat::expect_true(is.list(psw))
testthat::expect_equal(length(psw), 8)
testthat::expect_true(is.character(psw[[1]]$original))
# Create sample input
gridlist <- list(data.frame(x = c(1, 2, 3, 4, 5),
y = c(1, 2, 3, 4, 5)),
data.frame(x = c(8, 6, 4, 0, -1),
y = c(7, 5, 3, 0, -1)))
# Call the function
result <- par_split_list(gridlist)
# Check the output
testthat::expect_equal(result[[1]]$original, data.frame(x = 1, y = 1))
testthat::expect_equal(result[[1]]$padded, data.frame(x = 8, y = 7))
})