R package for downloading OpenStreetMap data
at main 426 lines 14 kB view raw
1has_internet <- curl::has_internet () 2 3test_that ("query-construction", { 4 5 q0 <- opq (bbox = c (-0.12, 51.51, -0.11, 51.52)) 6 expect_error (q1 <- add_osm_feature (q0), "key must be provided") 7 expect_silent (q1 <- add_osm_feature (q0, key = "aaa")) # bbox from qry 8 q0$bbox <- NULL 9 expect_error ( 10 q1 <- add_osm_feature (q0, key = "aaa"), 11 "Bounding box has to either be set in opq or must be set here" 12 ) 13 q0 <- opq (bbox = c (-0.12, 51.51, -0.11, 51.52)) 14 q1 <- add_osm_feature (q0, key = "aaa") 15 expect_false (grepl ("=", q1$features)) 16 q1 <- add_osm_feature (q0, key = "aaa", value = "bbb") 17 expect_true (grepl ("=", q1$features)) 18 expect_message ( 19 q1 <- add_osm_feature (q0, 20 key = "aaa", value = "bbb", 21 key_exact = FALSE 22 ), 23 "key_exact = FALSE can only combined with value_exact = FALSE;" 24 ) 25 expect_silent ( 26 q1 <- add_osm_feature (q0, 27 key = "aaa", value = "bbb", 28 key_exact = FALSE, value_exact = FALSE 29 ) 30 ) 31}) 32 33test_that ("add feature", { 34 35 qry <- opq (bbox = c (-0.118, 51.514, -0.115, 51.517)) 36 qry1 <- add_osm_feature (qry, key = "highway") 37 qry2 <- add_osm_feature (qry, key = "highway", value = "primary") 38 qry3 <- add_osm_feature (qry, 39 key = "highway", 40 value = c ("primary", "tertiary") 41 ) 42 qry4 <- add_osm_feature (qry, key = "highway", value = "!primary") 43 qry5 <- add_osm_feature (qry, 44 key = "highway", value = "primary", 45 match_case = FALSE 46 ) 47 expect_identical (qry1$features, "[\"highway\"]") 48 expect_identical (qry2$features, "[\"highway\"=\"primary\"]") 49 expect_identical ( 50 qry3$features, 51 "[\"highway\"~\"^(primary|tertiary)$\"]" 52 ) 53 expect_identical (qry4$features, "[\"highway\"!=\"primary\"]") 54 expect_identical (qry5$features, "[\"highway\"~\"^(primary)$\",i]") 55 56 bbox <- c (-0.118, 51.514, -0.115, 51.517) 57 qry <- opq (bbox = bbox) 58 bbox2 <- bbox + c (0.01, 0.01, -0.01, -0.01) 59 qry6 <- add_osm_feature ( 60 qry, 61 bbox = bbox2, 62 key = "highway", 63 value = "!primary" 64 ) 65 expect_true (!identical (qry$bbox, qry6$bbox)) 66 67 qry7 <- opq ("relation(id:74310)") |> # "Vinçà" 68 add_osm_feature (key = c ("name", "!name:ca")) 69 qry8 <- opq ("relation(id:11755232)") |> # "el Carxe" 70 add_osm_feature (key = "natural", value = "peak") |> 71 add_osm_feature (key = "!ele") 72 expect_warning ( 73 qry9 <- opq ("relation(id:11755232)") |> # "el Carxe" 74 add_osm_feature (key = "!ele") |> 75 add_osm_feature (key = "natural", value = "peak"), 76 "The query will request objects whith only a negated key " 77 ) 78 expect_identical (qry7$features, "[\"name\"] [!\"name:ca\"]") 79 expect_identical (qry8$features, "[\"natural\"=\"peak\"] [!\"ele\"]") 80 expect_identical (qry9$features, "[!\"ele\"] [\"natural\"=\"peak\"]") 81}) 82 83test_that ("query_errors", { 84 85 expect_error ( 86 osmdata_xml (), 87 "argument \"q\" is missing, with no default" 88 ) 89 expect_error ( 90 expect_warning (osmdata_sp (), "Deprecated"), 91 'arguments "q" and "doc" are missing, with no default. ' 92 ) 93 expect_error ( 94 osmdata_sf (), 95 'arguments "q" and "doc" are missing, with no default. ' 96 ) 97 expect_error ( 98 osmdata_sc (), 99 'arguments "q" and "doc" are missing, with no default. ' 100 ) 101 expect_error ( 102 osmdata_data_frame (), 103 'arguments "q" and "doc" are missing, with no default. ' 104 ) 105 106 expect_error ( 107 osmdata_xml (q = NULL), 108 "q must be an overpass query or a character string" 109 ) 110 expect_error ( 111 expect_warning (osmdata_sp (q = NULL), "Deprecated"), 112 "q must be an overpass query or a character string" 113 ) 114 expect_error ( 115 osmdata_sf (q = NULL), 116 "q must be an overpass query or a character string" 117 ) 118 expect_error ( 119 osmdata_sc (q = NULL), 120 "q must be an overpass query or a character string" 121 ) 122 expect_error ( 123 osmdata_data_frame (q = NULL), 124 "q must be an overpass query or a character string" 125 ) 126}) 127 128test_that ("not implemented queries", { 129 130 qadiff <- opq ( 131 bbox = c (1.8374527, 41.5931579, 1.8384799, 41.5936434), 132 datetime = "2014-09-11T00:00:00Z", 133 datetime2 = "2017-09-11T00:00:00Z", 134 adiff = TRUE 135 ) 136 osm_adiff2 <- test_path ("fixtures", "osm-adiff2.osm") 137 doc <- xml2::read_xml (osm_adiff2) 138 139 expect_error ( 140 expect_warning (osmdata_sp (q = qadiff, doc = doc), "Deprecated"), 141 "adiff queries not yet implemented." 142 ) 143 expect_error ( 144 osmdata_sf (q = qadiff, doc = doc), 145 "adiff queries not yet implemented." 146 ) 147 expect_error ( 148 osmdata_sc (q = qadiff, doc = doc), 149 "adiff queries not yet implemented." 150 ) 151 152 153 qtags <- opq ( 154 bbox = c (1.8374527, 41.5931579, 1.8384799, 41.5936434), 155 out = "tags" 156 ) 157 osm_tags <- test_path ("fixtures", "osm-tags.osm") 158 doc <- xml2::read_xml (osm_tags) 159 160 expect_error ( 161 expect_warning (osmdata_sp (q = qtags, doc = doc), "Deprecated"), 162 "Queries returning no geometries \\(out tags/ids\\) not accepted." 163 ) 164 expect_error ( 165 osmdata_sf (q = qtags, doc = doc), 166 "Queries returning no geometries \\(out tags/ids\\) not accepted." 167 ) 168 expect_error ( 169 osmdata_sc (q = qtags, doc = doc), 170 "Queries returning no geometries \\(out tags/ids\\) not accepted." 171 ) 172 173 qmeta <- opq ( 174 bbox = c (1.8374527, 41.5931579, 1.8384799, 41.5936434), 175 out = "meta" 176 ) 177 osm_meta <- test_path ("fixtures", "osm-meta.osm") 178 doc <- xml2::read_xml (osm_meta) 179 180 expect_warning ( 181 osmdata_sp (q = qmeta, doc = doc), 182 "`out meta` queries not yet implemented." 183 ) 184 expect_warning ( 185 osmdata_sc (q = qmeta, doc = doc), 186 "`out meta` queries not yet implemented." 187 ) 188 189 qcsv <- opq (bbox = c (1.8374527, 41.5931579, 1.8384799, 41.5936434)) |> 190 opq_csv (fields = c ("name")) 191 expect_error ( 192 osmdata_xml (q = qcsv), 193 "out:csv queries only work with osmdata_data_frame()." 194 ) 195 expect_error ( 196 expect_warning (osmdata_sp (q = qcsv), "Deprecated"), 197 "out:csv queries only work with osmdata_data_frame()." 198 ) 199 expect_error ( 200 osmdata_sf (q = qcsv), 201 "out:csv queries only work with osmdata_data_frame()." 202 ) 203 expect_error ( 204 osmdata_sc (q = qcsv), 205 "out:csv queries only work with osmdata_data_frame()." 206 ) 207 208}) 209 210test_that ("osmdata without query", { 211 osm_multi <- test_path ("fixtures", "osm-multi.osm") 212 doc <- xml2::read_xml (osm_multi) 213 214 expect_silent ( 215 expect_warning (x_sp <- osmdata_sp (doc = doc), "Deprecated") 216 ) 217 expect_silent (x_sf <- osmdata_sf (doc = doc)) 218 expect_silent (x_sc <- osmdata_sc (doc = doc)) 219 expect_silent (x_df <- osmdata_data_frame (doc = doc)) 220 221 expect_s3_class (x_sp, "osmdata") 222 expect_s3_class (x_sf, "osmdata") 223 expect_s3_class (x_sc, c ("SC", "osmdata_sc")) 224 expect_s3_class (x_df, "data.frame") 225 226 expect_message ( 227 expect_warning (x_sp <- osmdata_sp (doc = doc, quiet = FALSE), "Deprecated"), 228 "q missing: osmdata object will not include query" 229 ) 230 expect_message ( 231 x_sf <- osmdata_sf (doc = doc, quiet = FALSE), 232 "q missing: osmdata object will not include query" 233 ) 234 expect_message ( 235 x_sc <- osmdata_sc (doc = doc, quiet = FALSE), 236 "q missing: osmdata object will not include query" 237 ) 238 expect_message ( 239 x_df <- osmdata_data_frame (doc = doc, quiet = FALSE), 240 "q missing: osmdata object will not include query" 241 ) 242 243 expect_s3_class (x_sp, "osmdata") 244 expect_s3_class (x_sf, "osmdata") 245 expect_s3_class (x_sc, c ("SC", "osmdata_sc")) 246 expect_s3_class (x_df, "data.frame") 247}) 248 249test_that ("make_query", { 250 251 qry <- opq (bbox = c (-0.116, 51.516, -0.115, 51.517)) 252 qry <- add_osm_feature (qry, key = "highway") 253 254 if (!has_internet) { 255 expect_error ( 256 osmdata_xml (qry), 257 "Overpass query unavailable without internet" 258 ) 259 expect_error ( 260 osmdata_sf (qry), 261 "Overpass query unavailable without internet" 262 ) 263 expect_error ( 264 osmdata_sp (qry), 265 "Overpass query unavailable without internet" 266 ) 267 expect_error ( 268 osmdata_sc (qry), 269 "Overpass query unavailable without internet" 270 ) 271 expect_error ( 272 osmdata_data_frame (qry), 273 "Overpass query unavailable without internet" 274 ) 275 } else { 276 277 doc <- with_mock_dir ("mock_osm_xml", { 278 osmdata_xml (qry) 279 }) 280 expect_true (is (doc, "xml_document")) 281 expect_silent ( 282 doc2 <- with_mock_dir ("mock_osm_xml2", { 283 osmdata_xml (qry, file = "junk.osm") 284 }) 285 ) 286 expect_equal (doc, doc2) 287 288 289 res <- with_mock_dir ("mock_osm_sp", { 290 expect_warning (osmdata_sp (qry), "Deprecated") 291 }) 292 expect_message (print (res), "Object of class 'osmdata' with") 293 expect_silent ( 294 expect_warning (res <- osmdata_sp (qry, doc), "Deprecated") 295 ) 296 expect_message (print (res), "Object of class 'osmdata' with") 297 expect_silent (expect_warning (res <- osmdata_sp (qry, "junk.osm"), "Deprecated")) 298 expect_message ( 299 expect_warning (res <- osmdata_sp (qry, "junk.osm", quiet = FALSE), "Deprecated") 300 ) 301 302 expect_s3_class (res, "osmdata") 303 nms <- c ( 304 "bbox", "overpass_call", "meta", "osm_points", 305 "osm_lines", "osm_polygons", "osm_multilines", 306 "osm_multipolygons" 307 ) 308 expect_named (res, expected = nms, ignore.order = FALSE) 309 nms <- c ("timestamp", "OSM_version", "overpass_version") 310 expect_named (res$meta, expected = nms) 311 312 res <- with_mock_dir ("mock_osm_sf", { 313 osmdata_sf (qry) 314 }) 315 expect_message (print (res), "Object of class 'osmdata' with") 316 expect_silent (res <- osmdata_sf (qry, doc)) 317 expect_message (print (res), "Object of class 'osmdata' with") 318 expect_silent (res <- osmdata_sf (qry, "junk.osm")) 319 expect_message (res <- osmdata_sf (qry, "junk.osm", quiet = FALSE)) 320 expect_s3_class (res, "osmdata") 321 nms <- c ( 322 "bbox", "overpass_call", "meta", "osm_points", 323 "osm_lines", "osm_polygons", "osm_multilines", 324 "osm_multipolygons" 325 ) 326 expect_named (res, expected = nms, ignore.order = FALSE) 327 328 res <- with_mock_dir ("mock_osm_df", { 329 osmdata_data_frame (qry) 330 }) 331 expect_s3_class (res, "data.frame") 332 expect_silent (res <- osmdata_data_frame (qry, doc)) 333 expect_s3_class (res, "data.frame") 334 expect_silent (res <- osmdata_data_frame (qry, "junk.osm")) 335 expect_message (res <- osmdata_data_frame (qry, "junk.osm", quiet = FALSE)) 336 337 nms <- c ( 338 "names", "row.names", "class", "bbox", "overpass_call", "meta" 339 ) 340 expect_named (attributes (res), expected = nms, ignore.order = FALSE) 341 342 if (file.exists ("junk.osm")) invisible (file.remove ("junk.osm")) 343 } 344}) 345 346test_that ("query-no-quiet", { 347 348 qry <- opq (bbox = c (-0.116, 51.516, -0.115, 51.517)) 349 qry <- add_osm_feature (qry, key = "highway") 350 351 with_mock_dir ("mock_osm_xml", { 352 expect_message ( 353 x <- osmdata_xml (qry, quiet = FALSE), 354 "Issuing query to Overpass API" 355 ) 356 }) 357 with_mock_dir ("mock_osm_sp", { 358 expect_message ( 359 expect_warning (x <- osmdata_sp (qry, quiet = FALSE), "Deprecated"), 360 "Issuing query to Overpass API" 361 ) 362 }) 363 with_mock_dir ("mock_osm_sf", { 364 expect_message ( 365 x <- osmdata_sf (qry, quiet = FALSE), 366 "Issuing query to Overpass API" 367 ) 368 }) 369 with_mock_dir ("mock_osm_sc", { 370 expect_message ( 371 x <- osmdata_sc (qry, quiet = FALSE), 372 "Issuing query to Overpass API" 373 ) 374 }) 375 with_mock_dir ("mock_osm_df", { 376 expect_message ( 377 x <- osmdata_data_frame (qry, quiet = FALSE), 378 "Issuing query to Overpass API" 379 ) 380 }) 381}) 382 383test_that ("add_osm_features", { 384 385 qry <- opq (bbox = c (-0.118, 51.514, -0.115, 51.517)) 386 expect_error ( 387 qry <- add_osm_features (qry), 388 "features must be provided" 389 ) 390 391 qry$bbox <- NULL 392 expect_error ( 393 qry <- add_osm_features (qry, features = "a"), 394 "Bounding box has to either be set in opq or must be set here" 395 ) 396 397 qry <- opq (bbox = c (-0.118, 51.514, -0.115, 51.517)) 398 399 expect_error ( 400 qry <- add_osm_features (qry, features = "a"), 401 "features must be a named list or vector or a character vector enclosed in escape delimited quotations \\(see examples\\)" 402 ) 403 404 bbox <- c (-0.118, 51.514, -0.115, 51.517) 405 bbox_mod <- bbox + c (-0.001, -0.001, 0.001, 0.001) 406 qry0 <- opq (bbox = bbox) 407 qry1 <- add_osm_features (qry0, features = "\"amenity\"=\"restaurant\"") 408 qry2 <- add_osm_features (qry0, 409 features = "\"amenity\"=\"restaurant\"", 410 bbox = bbox_mod 411 ) 412 expect_false (identical (qry1$bbox, qry2$bbox)) 413 414 qry3 <- add_osm_features (qry0, features = c ("amenity" = "restaurant")) 415 expect_identical (qry1, qry3) 416 417 qry4 <- add_osm_features (qry0, 418 features = c ("amenity" = "restaurant", "amentity" = "pub") 419 ) 420 expect_s3_class (qry4, "overpass_query") 421 422 qry5 <- add_osm_features (qry0, 423 features = list ("amenity" = "restaurant", "amentity" = "pub") 424 ) 425 expect_s3_class (qry5, "overpass_query") 426})