-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest-wbt_source.R
More file actions
120 lines (73 loc) · 2.52 KB
/
test-wbt_source.R
File metadata and controls
120 lines (73 loc) · 2.52 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
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
test_that("wbt_source (raster) works", {
skip_if_not_installed("terra")
expect_error(wbt_source("does_not_exist.tif"))
f <- sample_dem_data()
skip_if(!file.exists(f))
# raster source from geotiff path
src <- wbt_source(f)
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.tif$", x))
expect_true(file.exists(x))
dem <- terra::rast(f)
# raster source from spatraster (with source file)
src <- wbt_source(dem)
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.tif$", x))
expect_true(file.exists(x))
dem2 <- dem*2
# raster source from spatraster (in memory)
src <- wbt_source(dem2)
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.tif$", x))
expect_true(file.exists(x))
tf <- tempfile(fileext = ".gpkg")
terra::writeRaster(dem, tf, gdal = c("RASTER_TABLE=one"))
terra::writeRaster(dem2, tf, gdal = c("RASTER_TABLE=two", "APPEND_SUBDATASET=YES"))
# raster source from non-geotiff
src <- wbt_source(tf)
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.tif$", x))
expect_true(file.exists(x))
# raster source from non-geotiff
src <- wbt_source(tf, layer = "two")
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.tif$", x))
expect_true(file.exists(x))
unlink(tf)
})
test_that("wbt_source (vector) works", {
skip_if_not_installed("terra")
f <- sample_soils_data()
skip_if(!file.exists(f))
# vector source from shapefile
src <- wbt_source(f)
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.shp$", x))
expect_true(file.exists(x))
vf <- terra::vect(f)
# vector source from spatvector (in memory)
src <- wbt_source(vf)
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.shp$", x))
expect_true(file.exists(x))
vf2 <- terra::vect(f, proxy = TRUE)
# vector source from spatvectorproxy
src <- wbt_source(vf2)
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.shp$", x))
expect_true(file.exists(x))
tf <- tempfile(fileext = ".gpkg")
terra::writeVector(vf, tf, layer = "one")
terra::writeVector(vf, tf, layer = "two", insert = TRUE)
# vector source from non-shapefile
src <- suppressWarnings(wbt_source(tf))
# terra warning for multiple layers but layer unspecified
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.shp$", x))
expect_true(file.exists(x))
src <- wbt_source(tf, layer = "two")
x <- attr(src, "wbt_dsn")
expect_true(grepl("\\.shp$", x))
expect_true(file.exists(x))
unlink(tf)
})