R package for downloading OpenStreetMap data
1# ------------------- void values
2test_that ("osm_points-void", {
3 expect_error (
4 osm_points (),
5 "osm_points can not be extracted without data"
6 )
7 expect_error (
8 osm_points (1),
9 "id must be given to extract points"
10 )
11 expect_error (
12 osm_points (1, 1),
13 "dat must be of class `osmdata_sf`"
14 )
15 q0 <- opq (bbox = c (1, 1, 5, 5))
16 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
17 expect_error (
18 osm_points (x),
19 "id must be given to extract points"
20 )
21 expect_error (
22 osm_points (id = x),
23 "osm_points can not be extracted without data"
24 )
25 expect_error (
26 osm_points (x, id = x),
27 "id must be of class character or numeric"
28 )
29})
30
31test_that ("osm_lines-void", {
32 expect_error (
33 osm_lines (),
34 "osm_lines can not be extracted without data"
35 )
36 expect_error (
37 osm_lines (1),
38 "id must be given to extract lines"
39 )
40 expect_error (
41 osm_lines (1, 1),
42 "dat must be of class `osmdata_sf`"
43 )
44 q0 <- opq (bbox = c (1, 1, 5, 5))
45 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
46 expect_error (
47 osm_lines (x),
48 "id must be given to extract lines"
49 )
50 expect_error (
51 osm_lines (id = x),
52 "osm_lines can not be extracted without data"
53 )
54 expect_error (
55 osm_lines (x, id = x),
56 "id must be of class character or numeric"
57 )
58})
59
60test_that ("osm_polygons-void", {
61 expect_error (
62 osm_polygons (),
63 "osm_polygons can not be extracted without data"
64 )
65 expect_error (
66 osm_polygons (1),
67 "id must be given to extract polygons"
68 )
69 expect_error (
70 osm_polygons (1, 1),
71 "dat must be of class `osmdata_sf`"
72 )
73 q0 <- opq (bbox = c (1, 1, 5, 5))
74 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
75 expect_error (
76 osm_polygons (x),
77 "id must be given to extract polygons"
78 )
79 expect_error (
80 osm_polygons (id = x),
81 "osm_polygons can not be extracted without data"
82 )
83 expect_error (
84 osm_polygons (x, id = x),
85 "id must be of class character or numeric"
86 )
87})
88
89test_that ("osm_multilines-void", {
90 expect_error (
91 osm_multilines (),
92 "osm_multilines can not be extracted without data"
93 )
94 expect_error (
95 osm_multilines (1),
96 "id must be given to extract multilines"
97 )
98 expect_error (
99 osm_multilines (1, 1),
100 "dat must be of class `osmdata_sf`"
101 )
102 q0 <- opq (bbox = c (1, 1, 5, 5))
103 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
104 expect_error (
105 osm_multilines (x),
106 "id must be given to extract multilines"
107 )
108 expect_error (
109 osm_multilines (id = x),
110 "osm_multilines can not be extracted without data"
111 )
112 expect_error (
113 osm_multilines (x, id = x),
114 "id must be of class character or numeric"
115 )
116})
117
118test_that ("osm_multipolygons-void", {
119 expect_error (
120 osm_multipolygons (),
121 "osm_multipolygons can not be extracted without data"
122 )
123 expect_error (
124 osm_multipolygons (1),
125 "id must be given to extract multipolygons"
126 )
127 expect_error (
128 osm_multipolygons (1, 1),
129 "dat must be of class `osmdata_sf`"
130 )
131 q0 <- opq (bbox = c (1, 1, 5, 5))
132 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
133 expect_error (
134 osm_multipolygons (x),
135 "id must be given to extract multipolygons"
136 )
137 expect_error (
138 osm_multipolygons (id = x),
139 "osm_multipolygons can not be extracted without data"
140 )
141 expect_error (
142 osm_multipolygons (x, id = x),
143 "id must be of class character or numeric"
144 )
145})
146
147# ------------------- points
148
149test_that ("points-from-multipolygons", {
150 q0 <- opq (bbox = c (1, 1, 5, 5))
151 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
152 pts <- osm_points (x, rownames (x$osm_multipolygons))
153 expect_equal (dim (pts), c (16, 6))
154})
155
156test_that ("points-from-multilines", {
157 q0 <- opq (bbox = c (1, 1, 5, 5))
158 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
159 pts <- osm_points (x, rownames (x$osm_multilines))
160 expect_equal (dim (pts), c (10, 6))
161})
162
163test_that ("points-from-polygons", {
164 q0 <- opq (bbox = c (1, 1, 5, 5))
165 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
166 pts <- osm_points (x, rownames (x$osm_polygons))
167 expect_equal (dim (pts), c (4, 6))
168})
169
170test_that ("points-from-lines", {
171 q0 <- opq (bbox = c (1, 1, 5, 5))
172 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
173 pts <- osm_points (x, rownames (x$osm_lines))
174 expect_equal (dim (pts), c (12, 6))
175
176 # Only lines have multiples features
177 ids <- lapply (seq (x$osm_lines$geometry), function (i) {
178 rownames (osm_points (x, rownames (x$osm_lines) [i]))
179 })
180 ids <- sort (unique (as.vector (unlist (ids))))
181 ids_all <- sort (rownames (osm_points (
182 x,
183 rownames (x$osm_lines)
184 )))
185 expect_identical (ids, ids_all)
186})
187
188# ------------------- lines
189
190test_that ("lines-from-multipolygons", {
191 q0 <- opq (bbox = c (1, 1, 5, 5))
192 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
193 lns <- osm_lines (x, rownames (x$osm_multipolygons))
194 expect_equal (dim (lns), c (4, 8))
195})
196
197test_that ("lines-from-multilines", {
198 q0 <- opq (bbox = c (1, 1, 5, 5))
199 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
200 lns <- osm_lines (x, rownames (x$osm_multilines))
201 expect_equal (dim (lns), c (3, 8))
202})
203
204test_that ("lines-from-lines", {
205 q0 <- opq (bbox = c (1, 1, 5, 5))
206 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
207 lns <- osm_lines (x, rownames (x$osm_lines) [1])
208 expect_equal (dim (lns), c (3, 8))
209})
210
211test_that ("lines-from-points", {
212 q0 <- opq (bbox = c (1, 1, 5, 5))
213 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
214 lns <- osm_lines (x, rownames (x$osm_points) [1])
215 expect_equal (dim (lns), c (2, 8))
216})
217
218# ------------------- polygons
219
220test_that ("polygons-from-multipolygons", {
221 q0 <- opq (bbox = c (1, 1, 5, 5))
222 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
223 pls <- osm_polygons (x, rownames (x$osm_multipolygons))
224 expect_equal (dim (pls), c (1, 8))
225})
226
227test_that ("polygons-from-multilines", {
228 q0 <- opq (bbox = c (1, 1, 5, 5))
229 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
230 expect_error (
231 osm_polygons (x, rownames (x$osm_multilines)),
232 "MULTILINESTRINGS do not contain polygons by definition"
233 )
234})
235
236test_that ("polygons-from-lines", {
237 q0 <- opq (bbox = c (1, 1, 5, 5))
238 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
239 pls <- osm_polygons (x, rownames (x$osm_lines) [1])
240 expect_equal (dim (pls), c (0, 8)) # no polygons contain lines
241})
242
243test_that ("polygons-from-points", {
244 q0 <- opq (bbox = c (1, 1, 5, 5))
245 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
246 pls <- osm_polygons (x, rownames (x$osm_points) [8])
247 expect_equal (dim (pls), c (1, 8))
248})
249
250# ------------------- multilines
251
252test_that ("multilines-from-lines", {
253 q0 <- opq (bbox = c (1, 1, 5, 5))
254 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
255 mls <- osm_multilines (x, rownames (x$osm_lines) [1])
256 expect_equal (dim (mls), c (1, 7))
257})
258
259test_that ("multilines-from-points", {
260 q0 <- opq (bbox = c (1, 1, 5, 5))
261 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
262 mls <- osm_multilines (x, rownames (x$osm_points) [1])
263 expect_equal (dim (mls), c (1, 7))
264})
265
266# ------------------- multipolygons
267
268test_that ("multipolygons-from-polygons", {
269 q0 <- opq (bbox = c (1, 1, 5, 5))
270 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
271 mps <- osm_multipolygons (x, rownames (x$osm_polygons) [1])
272 expect_equal (dim (mps), c (1, 6))
273})
274
275test_that ("multipolygons-from-lines", {
276 q0 <- opq (bbox = c (1, 1, 5, 5))
277 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
278 mps <- osm_multipolygons (x, rownames (x$osm_lines) [2])
279 expect_equal (dim (mps), c (1, 6))
280})
281
282test_that ("multipolygons-from-points", {
283 q0 <- opq (bbox = c (1, 1, 5, 5))
284 x <- osmdata_sf (q0, test_path ("fixtures", "osm-multi.osm"))
285 mps <- osm_multipolygons (x, rownames (x$osm_points) [1])
286 expect_equal (dim (mps), c (1, 6))
287})