ropensci / osmdata

R package for downloading OpenStreetMap data
https://docs.ropensci.org/osmdata
317 stars 45 forks source link

How to download relations #45

Closed Robinlovelace closed 7 years ago

Robinlovelace commented 7 years ago

Not sure if this is possible already, if it is should be documented in the vignette.

Example: https://github.com/ropensci/osmplotr/issues/4

mpadge commented 7 years ago

Yeah, it should be entirely possible in current state, and it'd be great to have an example in the vignette. Relations are stored either in osm_multipolygons or osm_multilinestrings, depending on their nature, but all relational data should be accessible from there. It'd be great if you could flesh out a nice example in the vignette, and report here on how it goes.

mpadge commented 7 years ago

You can't specifically search for relations, but nor can you specifically search for ways. Relations will simply be returned either as osm_multilinestrings or osm_multipolygons objects, and ways will be returned either as osm_lines or osm_polygons. Specifically getting relations just requires knowing, for example, what they're called. Here's your canonical example:

> q <- opq ('greater london uk')
> q1 <- add_feature (q, key="name", value="London.Cycle.Network.Route.9", exact=FALSE)
> osmdata_sf (q1)
Object of class 'osmdata' with:
                 $bbox : 51.2867602,-0.510375,51.6918741,0.3340155
        $overpass_call : The call submitted to the overpass API
            $timestamp : [ Mon Feb 27 16:55:41 2017 ]
           $osm_points : 'sf' Simple Features Collection with 1006 points
            $osm_lines : 'sf' Simple Features Collection with 246 linestrings
         $osm_polygons : 'sf' Simple Features Collection with 0 polygons
       $osm_multilines : 'sf' Simple Features Collection with 3 multilinestrings
    $osm_multipolygons : 'sf' Simple Features Collection with 0 multipolygons

The osm_lines contain all of the individual components which make up the route, while the osm_multilines are the actual relations. There are three of them because

> d <- osmdata_sf (q1)
> d1$osm_multilines$name
           1429834-(no role)             1429834-backward 
London Cycle Network Route 9 London Cycle Network Route 9 
             1429834-forward 
London Cycle Network Route 9 
Levels: London Cycle Network Route 9

... and that serves to support Edzer's thoughts on factors - without the factors that key information would be much harder to extract.

Now note this:

> q1 <- add_feature (q, key="name", value="London.Cycle.Network.route.9", exact=FALSE)
> osmdata_sf (q1)

... returns no data, because queries are still case sensitive even with exact=FALSE. The only way (at present) to make a truly case-insensitive query would be the standard overpass way:

> q1 <- add_feature (q, key="name", value="[Ll]ondon.[Cc]ycle.[Nn]etwork.[Rr]oute.9", 
                     exact=FALSE)

It'd be pretty easy to modify add_feature, so exact=FALSE also automatically added the relevant case options immediately following all spaces or periods, but note that that would remove any ability not to specify that, which is a bit of a negative side effect. What do you think?

Robinlovelace commented 7 years ago

Will add that to the vignette - great work, can't wait to test this.