1
2test_that("wk_vertices() works", {
3  expect_identical(
4    wk_vertices(wkt(c("POINT (0 0)", "POINT (1 1)", NA))),
5    wkt(c("POINT (0 0)", "POINT (1 1)", NA))
6  )
7  expect_identical(
8    wk_vertices(wkt(c("LINESTRING (0 0, 1 1)", NA))),
9    wkt(c("POINT (0 0)", "POINT (1 1)", NA))
10  )
11  expect_error(wk_vertices(new_wk_wkt("POINT ENTPY")), "ENTPY")
12
13  # we need this one to trigger a realloc on the details list
14  xy_copy <- wk_handle(
15    as_wkt(xy(1:1025, 1)),
16    wk_vertex_filter(xy_writer(), add_details = TRUE)
17  )
18  expect_identical(
19    attr(xy_copy, "wk_details"),
20    list(feature_id = 1:1025, part_id = 1:1025, ring_id = rep(0L, 1025))
21  )
22  attr(xy_copy, "wk_details") <- NULL
23  expect_identical(xy_copy, xy(1:1025, 1))
24})
25
26test_that("wk_vertices() works for data.frame", {
27  expect_identical(
28    wk_vertices(data.frame(geom = wkt(c("POINT (0 0)", "POINT (1 1)")))),
29    data.frame(geom = wkt(c("POINT (0 0)", "POINT (1 1)")))
30  )
31})
32
33test_that("wk_coords() works", {
34  # point
35  expect_identical(
36    wk_coords(wkt("POINT (30 10)")),
37    data.frame(
38      feature_id = 1L,
39      part_id = 1L,
40      ring_id = 0L,
41      x = 30,
42      y = 10
43    )
44  )
45
46  # point zm
47  expect_identical(
48    wk_coords(wkt("POINT ZM (30 10 1 2)")),
49    data.frame(
50      feature_id = 1L,
51      part_id = 1L,
52      ring_id = 0L,
53      x = 30,
54      y = 10,
55      z = 1,
56      m = 2
57    )
58  )
59
60  # linestring
61  expect_identical(
62    wk_coords(wkt("LINESTRING (30 10, 20 11)")),
63    data.frame(
64      feature_id = c(1L, 1L),
65      part_id = c(1L, 1L),
66      ring_id = c(0L, 0L),
67      x = c(30, 20),
68      y = c(10, 11)
69    )
70  )
71
72  # polygon
73  expect_identical(
74    wk_coords(wkt("POLYGON ((30 10, 20 11, 0 0, 30 10))")),
75    data.frame(
76      feature_id = c(1L, 1L, 1L, 1L),
77      part_id = c(1L, 1L, 1L, 1L),
78      ring_id = c(1L, 1L, 1L, 1L),
79      x = c(30, 20, 0, 30),
80      y = c(10, 11, 0, 10)
81    )
82  )
83
84  # multipoint
85  expect_identical(
86    wk_coords(wkt("MULTIPOINT ((30 10), (20 11))")),
87    data.frame(
88      feature_id = c(1L, 1L),
89      part_id = c(2L, 3L),
90      ring_id = c(0L, 0L),
91      x = c(30, 20),
92      y = c(10, 11)
93    )
94  )
95
96  # collection
97  # point
98  expect_identical(
99    wk_coords(wkt("GEOMETRYCOLLECTION (POINT (30 10))")),
100    data.frame(
101      feature_id = 1L,
102      part_id = 2L,
103      ring_id = 0L,
104      x = 30,
105      y = 10
106    )
107  )
108})
109
110test_that("wk_vertices() communicates correct size and type", {
111  expect_identical(
112    wk_handle(wkt("POINT (0 0)"), wk_vertex_filter(wk_vector_meta_handler())),
113    list(geometry_type = 1L, size = NA_real_, has_z = NA, has_m = NA)
114  )
115
116  skip_if_not_installed("sf")
117  # need sf because these objects carry vector-level types
118  expect_identical(
119    wk_handle(sf::st_as_sfc("POINT (0 0)"), wk_vertex_filter(wk_vector_meta_handler())),
120    list(geometry_type = 1L, size = 1, has_z = FALSE, has_m = FALSE)
121  )
122  expect_identical(
123    wk_handle(sf::st_as_sfc("MULTIPOINT EMPTY"), wk_vertex_filter(wk_vector_meta_handler())),
124    list(geometry_type = 1L, size = NA_real_, has_z = FALSE, has_m = FALSE)
125  )
126  expect_identical(
127    wk_handle(sf::st_as_sfc("MULTILINESTRING EMPTY"), wk_vertex_filter(wk_vector_meta_handler())),
128    list(geometry_type = 1L, size = NA_real_, has_z = FALSE, has_m = FALSE)
129  )
130  expect_identical(
131    wk_handle(sf::st_as_sfc("MULTIPOLYGON EMPTY"), wk_vertex_filter(wk_vector_meta_handler())),
132    list(geometry_type = 1L, size = NA_real_, has_z = FALSE, has_m = FALSE)
133  )
134  expect_identical(
135    wk_handle(sf::st_as_sfc("GEOMETRYCOLLECTION EMPTY"), wk_vertex_filter(wk_vector_meta_handler())),
136    list(geometry_type = 1L, size = NA_real_, has_z = FALSE, has_m = FALSE)
137  )
138})
139