I'm trying to extract administrative boundary data from OpenStreetMap using osmdata in R. The Overpass API lets you determine the element group (i.e. nodes, ways, relations) to return. For example, in the following code, only relations are returned:
area(3600062761)->.searchArea;
(
relation["admin_level"="6"]["boundary"="administrative"](area.searchArea);
);
out geom;
However, I couldn't replicate this with osmdata in R. The following R code:
osm_bounds <- getbb(place_name = 'NRW', format_out = 'polygon')[[1]] %>%
opq(timeout = 1000) %>%
add_osm_feature(key = 'admin_level', value = '6') %>%
add_osm_feature(key = 'boundary', value = 'administrative') %>%
osmdata_sf()
... generates an Overpass query that returns nodes, ways and relations:
[out:xml][timeout:1000];
(
node ["admin_level"="6"] ["boundary"="administrative"] (50.3226897,5.8663153,52.5314923,9.4617417);
way ["admin_level"="6"] ["boundary"="administrative"] (50.3226897,5.8663153,52.5314923,9.4617417);
relation ["admin_level"="6"] ["boundary"="administrative"] (50.3226897,5.8663153,52.5314923,9.4617417);
);
(._;>;);
out body;
Thus, I have to download all elements and select the ones of interest afterwards. The first Overpass query downloads approximately 120 MB while the second query downloads 700 MB and takes an eternity.
I also tried formatting the Overpass code as a string and then passing it to osmdata::osmdata_sf()
:
osm_id <- tmaptools::geocode_OSM('NRW', details = TRUE)$osm_id
overpass_string <- paste0('area(%s)->.searchArea;',
'(relation["admin_level"="%s"]["boundary"="administrative"](area.searchArea);',
'); out geom;') %>%
sprintf(3600000000 + as.integer(osm_id), 6)
osmdata_sf(overpass_string)
However, this returns an empty osmdata object or an error (seemingly randomly one of these two outcomes):
Object of class 'osmdata' with:
$bbox :
$overpass_call : The call submitted to the overpass API
$meta : metadata including timestamp and version numbers
$osm_points : 'sf' Simple Features Collection with 0 points
$osm_lines : NULL
$osm_polygons : 'sf' Simple Features Collection with 0 polygons
$osm_multilines : NULL
$osm_multipolygons : NULL
Error in rcpp_osmdata_sf(doc): way can not be found
My question is if there are other ways of determining the element groups that are to be selected using osmdata or if there are workarounds to accomplish this.