Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R osmdata query returns object contaminated with results from an old query

I have made the following query using the osmdata package:

library(osmdata)
library(sf)

# Get map boundaries for UK:
bb <- getbb ('uk', format_out = 'polygon')

# Fetch data for selected boundaries from osmdata:
ukbasemap <- opq(bbox = bb) %>% # set boundary box
  add_osm_feature(key = 'admin_level', value = '4') %>% # add feature
  osmdata_sf () %>% # convert to sf object
  trim_osmdata (bb) # trim to edges of boundary (i.e. UK borders)

This returned the following warning, but otherwise created the sf object without apparent issue:
bb_poly has more than one polygon; the first will be selected.

I then tried to plot the data, but the below code produces a blank plot:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> sf::plot_sf(ukbasemap$osm_lines)
> sf::plot_sf(ukbasemap$osm_polygons)
> sf::plot_sf(ukbasemap$osm_multipolygons)
> sf::plot_sf(ukbasemap$osm_points)

When I inspected the sf object, I was surprised to see some place names from Central African Republic in it. I ran a query for CAR a few months ago, and the only thing I can think of is that I accidentally set some global options to refer to CAR data – but I don’t remember doing that. Here is a snippet of what the sf object looks like:

> ukbasemap$osm_points
Simple feature collection with 2 features and 23 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 20.54475 ymin: 5.768001 xmax: 20.67557 ymax: 6.311055
Geodetic CRS:  WGS 84
               osm_id    name   GNS.id alt_name capital created_by  ele ford is_in.continent
2222388003 2222388003   Ouaka     <NA>     <NA>    <NA>       <NA> <NA> <NA>            <NA>
4680378478 4680378478 Bambari -1339188     <NA>       4       <NA> <NA> <NA>            <NA>
           is_in.country name.de name.en   name.ko  name.ru name.sg  name.uk place
2222388003          <NA>    <NA>    <NA>      <NA> Уака  Wäkä Уака state
4680378478          <NA>    <NA>    <NA> 밤바리     <NA>    <NA>     <NA>  city
           population  ref source state_code wikidata wikipedia                  geometry
2222388003       <NA>   UK   <NA>         UK  Q848560  fr:Ouaka POINT (20.54475 6.311055)
4680378478      41356 <NA>    GNS       <NA>  Q805946      <NA> POINT (20.67557 5.768001)

"Ouaka" and "Bambari" are place names in Central African Republic.

Does anyone know what could be going on here and how can I fix it?

>Solution :

This is because your bounding box does not actually refer to the United Kingdom. It is the case because the default featuretype = "settlement" combines results from all intermediate levels below "country" and above "streets" (see here).

You can get the bounding box for a country by adding feature_type = "country".

bb <- getbb("uk", format_out = 'polygon', featuretype = "country")

You can check whether your bounding box is correct by mapping it like this.

library(leaflet)

bb <- getbb("uk", format_out = 'sf_polygon', featuretype = "country")

leaflet(bb$multipolygon) %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addPolygons()

UK map

While with bb <- getbb ('uk', format_out = 'polygon'), you get:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading