ropensci / osmdata

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

[FEATURE] getbb with 'sf_polygon' format #338

Closed RegularnaMatrica closed 3 months ago

RegularnaMatrica commented 3 months ago

When using getbb with format_out="sf_polygon" result is sf object with only geometry column and not as with format_out="data.frame" where additional metadata is contained. It seems that it should be easy to incorporate metadata in format_out="sf_polygon" because Nominatim returns that metadata, for example:

httr::GET("https://nominatim.openstreetmap.org/search?format=json&q=london&featuretype=settlement&polygon_text=1&limit=3") %>%
  httr::content()

where I just copied URL that was printed using getbb("london", format_out = "sf_polygon", silent = FALSE).

Metadata would be useful to display to the user so he can choose (in shiny) which polygon to pick.

mpadge commented 3 months ago

Good idea @RegularnaMatrica, and already implemented via the above commits. Now looks like this:

library (osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
packageVersion ("osmdata")
#> [1] '0.2.5.16'
library (sf) # For print method
#> Linking to GEOS 3.12.1, GDAL 3.8.5, PROJ 9.4.0; sf_use_s2() is TRUE

getbb("london", format_out = "sf_polygon")
#> Simple feature collection with 11 features and 13 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -93.27163 ymin: 35.31695 xmax: 0.3340155 ymax: 51.69187
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>     place_id
#> 1  274010846
#> 2  274010846
#> 3  273135560
#> 4  325636495
#> 5  325272631
#> 6  325272631
#> 7  325272631
#> 8  325272631
#> 9  325272631
#> 10 320503585
#>                                                                  licence
#> 1  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 2  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 3  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 4  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 5  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 6  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 7  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 8  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 9  Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#> 10 Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright
#>    osm_type osm_id        lat         lon    class           type place_rank
#> 1  relation  65606 51.5074456  -0.1277653    place           city         16
#> 2  relation  65606 51.5074456  -0.1277653    place           city         16
#> 3  relation  51800 51.5156177  -0.0919983 boundary administrative         12
#> 4  relation 130591 37.1283343 -84.0835576 boundary administrative         16
#> 5  relation 182481 39.8864493  -83.448253 boundary administrative         16
#> 6  relation 182481 39.8864493  -83.448253 boundary administrative         16
#> 7  relation 182481 39.8864493  -83.448253 boundary administrative         16
#> 8  relation 182481 39.8864493  -83.448253 boundary administrative         16
#> 9  relation 182481 39.8864493  -83.448253 boundary administrative         16
#> 10 relation 111457  35.328973 -93.2529553 boundary administrative         16
#>    importance addresstype           name
#> 1   0.8307828        city         London
#> 2   0.8307828        city         London
#> 3   0.5865112        city City of London
#> 4   0.4412922        city         London
#> 5   0.4050655        city         London
#> 6   0.4050655        city         London
#> 7   0.4050655        city         London
#> 8   0.4050655        city         London
#> 9   0.4050655        city         London
#> 10  0.3982358        city         London
#>                                               display_name
#> 1          London, Greater London, England, United Kingdom
#> 2          London, Greater London, England, United Kingdom
#> 3  City of London, Greater London, England, United Kingdom
#> 4    London, Laurel County, Kentucky, 40741, United States
#> 5       London, Madison County, Ohio, 43140, United States
#> 6       London, Madison County, Ohio, 43140, United States
#> 7       London, Madison County, Ohio, 43140, United States
#> 8       London, Madison County, Ohio, 43140, United States
#> 9       London, Madison County, Ohio, 43140, United States
#> 10            London, Pope County, Arkansas, United States
#>                          geometry
#> 1  POLYGON ((-0.5103751 51.468...
#> 2  POLYGON ((-0.1138295 51.518...
#> 3  POLYGON ((-0.1138295 51.518...
#> 4  POLYGON ((-84.12626 37.1497...
#> 5  POLYGON ((-83.47892 39.8738...
#> 6  POLYGON ((-83.45828 39.8996...
#> 7  POLYGON ((-83.43131 39.8942...
#> 8  POLYGON ((-83.42567 39.8954...
#> 9  POLYGON ((-83.42347 39.8957...
#> 10 POLYGON ((-93.27163 35.3316...

Created on 2024-06-06 with reprex v2.1.0

That makes the whole function far more useful than before! (And note that rhere are repeats of all metadata in there, because the polygon objects returned by the overpass server are themselves multi-polygons, with each single one containing repeated metadata.)