-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_src_generated.R
66 lines (59 loc) · 2.05 KB
/
example_src_generated.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
library(testthat)
library(wk)
src <- yaml::read_yaml("example/example_src.yaml")
# Check wk roundtrip to make sure we have valid WKT and that we have the WKT
# that is generated by most tools
for (item in src) {
is_null <- vapply(item, is.null, logical(1))
item[is_null] <- NA_character_
item <- as.character(item)
roundtrip <- wk_handle(wk::wkt(item), wkt_writer()) |> unclass()
expect_equal(roundtrip, item)
}
# Generate the portion Z, M, and ZM versions of the WKT
make_z <- function(item) {
is_null <- vapply(item, is.null, logical(1))
item[is_null] <- NA_character_
item <- wkt(item) |> wk_set_z(1)
coords <- wk_coords(item)
coords$z <- coords$x + coords$y
wk_coords(item) <- coords
item_lst <- item |> as.character() |> as.list()
item_lst[vapply(item_lst, identical, logical(1), NA_character_)] <- list(NULL)
item_lst
}
make_m <- function(item) {
is_null <- vapply(item, is.null, logical(1))
item[is_null] <- NA_character_
item <- wkt(item) |> wk_set_m(1)
coords <- wk_coords(item)
coords$m <- coords$x * coords$y
wk_coords(item) <- coords
item_lst <- item |> as.character() |> as.list()
item_lst[vapply(item_lst, identical, logical(1), NA_character_)] <- list(NULL)
item_lst
}
make_zm <- function(item) {
is_null <- vapply(item, is.null, logical(1))
item[is_null] <- NA_character_
item <- wkt(item) |> wk_set_z(1) |> wk_set_m(1)
coords <- wk_coords(item)
coords$z <- coords$x + coords$y
coords$m <- coords$x * coords$y
wk_coords(item) <- coords
item_lst <- item |> as.character() |> as.list()
item_lst[vapply(item_lst, identical, logical(1), NA_character_)] <- list(NULL)
item_lst
}
src_z <- lapply(src, make_z)
names(src_z) <- paste0(names(src_z), "-z")
src_m <- lapply(src, make_m)
names(src_m) <- paste0(names(src_m), "-m")
src_zm <- lapply(src, make_zm)
names(src_zm) <- paste0(names(src_zm), "-zm")
out <- file("example/example_src_generated.yaml", "w")
writeLines("# generated by example_src_generated.R", out)
yaml::write_yaml(src_z, out)
yaml::write_yaml(src_m, out)
yaml::write_yaml(src_zm, out)
close(out)