osmapiR

osmapiR can fetch and edit all kinds data associated with the OpenStreetMap project. This vignette shows samples of each data type and how to configure the connection to the servers.

Most of the functions have a format argument that allows to modify the result to get the raw xml or a JSON list. By default, the results are data.frames. Check the documentation of each function for details.

Setup

library(osmapiR)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright

For testing editions without breaking the OSM data, you can open and account and make calls to https://master.apis.dev.openstreetmap.org.

# set_osmapi_connection(server = "testing") # lacks data for the following examples

To modify the default user agent of the requests (osmapiR 0.2.2 (https://github.com/ropensci/osmapiR)), set the option osmapir.user_agent:

options(osmapir.user_agent = "my new user agent")

Map objects

## Download objects by bounding box
osm_objs <- osm_bbox_objects(
  bbox = c(2.4166059, 42.5945594, 2.4176574, 42.5962101),
  tags_in_columns = TRUE
)

## View the history of an object
sel <- osm_objs$`name:ca` %in% "Abadia de Sant Miquel de Cuixà"
obj <- osm_objs[sel, ]

obj_history <- osm_history_object(
  osm_type = obj$osm_type, osm_id = obj$osm_id,
  tags_in_columns = FALSE # tags in a list at column `tags`
)
obj_history
#>   type       id visible version changeset           timestamp     user    uid
#> 1  way 50343004    TRUE       1   3882565 2010-02-15 12:14:11  Skywave  10927
#> 2  way 50343004    TRUE       2  13314595 2012-09-30 19:52:28 petrovsk  90394
#> 3  way 50343004    TRUE       3  53623614 2017-11-08 22:08:33    JFK73 662440
#> 4  way 50343004    TRUE       4 103004865 2021-04-15 15:41:02 petrovsk  90394
#>    lat  lon                                                         members
#> 1 <NA> <NA> 44 nodes: 639618609, 639618608, 639618720, 639618717, 639618...
#> 2 <NA> <NA> 44 nodes: 639618609, 639618608, 639618720, 639618717, 639618...
#> 3 <NA> <NA> 44 nodes: 639618609, 639618608, 639618720, 639618717, 639618...
#> 4 <NA> <NA> 48 nodes: 639618609, 639618608, 8632687795, 8632687796, 8632...
#>                                                                                  tags
#> 1 8 tags: amenity=place_of_worship | building=yes | denomination=catholic | histor...
#> 2 8 tags: amenity=place_of_worship | building=yes | denomination=catholic | histor...
#> 3 15 tags: amenity=place_of_worship | building=yes | denomination=catholic | herit...
#> 4 15 tags: amenity=place_of_worship | building=yes | denomination=catholic | herit...
# obj_history$tags # tags in list format

Notice the different formats to represent the tags controlled by the tags_in_columns argument. In osm_objs, every tag is represented by a column in the data.frame (wide format), while in obj_history there is a tag column containing a list of data.frames with key and value columns (list format). See ?tags_list2wide() for details.

Changesets

chsts <- osm_query_changesets(
  bbox = c(-1.241112, 38.0294955, 8.4203171, 42.9186456),
  user = "Mementomoristultus",
  time = "2023-06-22T02:23:23Z",
  time_2 = "2023-06-22T00:38:20Z"
)
chsts
#>  [1] id             created_at     closed_at      open           user          
#>  [6] uid            min_lat        min_lon        max_lat        max_lon       
#> [11] comments_count changes_count  tags          
#> <0 files> (o «row.names» de longitud 0)
osmchange <- osm_download_changeset(changeset_id = 137595351, format = "xml")
osmchange
#> {xml_document}
#> <osmChange version="0.6" generator="openstreetmap-cgimap 2.0.1 (1557706 spike-06.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
#> [1] <modify>\n  <way id="32570900" visible="true" version="9" changeset="1375 ...

# Osmchange file compatible with JOSM and others:
# xml2::write_xml(osmchange, file = tempfile(fileext = ".osc"))

Similar to the map objects, changeset’s data also have the tags as a list column that can be changed with tags_in_columns argument.

Notes

notes <- osm_read_bbox_notes(bbox = "2.4166059,42.5945594,2.4176574,42.5962101", closed = -1)
notes
#>         lon        lat      id
#> 1 2.4170566 42.5948042 1730475
#> 2 2.4170552 42.5949259  628602
#>                                                       url comment_url close_url
#> 1 https://api.openstreetmap.org/api/0.6/notes/1730475.xml        <NA>      <NA>
#> 2  https://api.openstreetmap.org/api/0.6/notes/628602.xml        <NA>      <NA>
#>          date_created status
#> 1 2019-03-31 14:10:04 closed
#> 2 2016-07-15 07:09:19 closed
#>                                                          comments
#> 1                     2 comments from 2019-03-31 by Sherpa66, Syl
#> 2 5 comments from 2016-07-15 to 2018-05-25 by Dolfo54, rainerU...
# notes$comments

GPX data

## Requires authentication
usr_traces <-  osm_list_gpxs()
osm_get_gpx_metadata(gpx_id = 3790367)
#>        id                 name      uid     user visibility pending           timestamp
#> 1 3790367 Airoto_Marimanha.gpx 11725140 jmaspons     public   FALSE 2021-08-20 10:30:15 
#>                 lat                lon               description              tags
#> 1 42.69660229794681 1.0419843904674053 Airoto Marimanha Oriental 1 camp, a, través
pts <- osm_get_data_gpx(gpx_id = 3790367, format = "R")
head(pts)
#>                  lat                lon               ele                time
#> 1  42.69660229794681 1.0419843904674053 2190.199951171875 2021-08-12 09:52:18
#> 2  42.69662777893245   1.04197240434587              2189 2021-08-12 09:52:24
#> 3  42.69664118997753  1.041965363547206  2188.39990234375 2021-08-12 09:52:25
#> 4  42.69665057770908 1.0419651120901108 2187.800048828125 2021-08-12 09:52:26
#> 5 42.696685614064336 1.0419511143118143 2186.800048828125 2021-08-12 09:52:29
#> 6  42.69673557020724 1.0419355239719152              2187 2021-08-12 09:52:34
gpx <- osm_get_points_gps(bbox = c(2.4166059, 42.5945594, 2.4176574, 42.5962101))
gpx
#> [[1]]
#>          lat       lon
#> 1 42.5945734 2.4166662
#> 2 42.5945770 2.4166060
#> 3 42.5945770 2.4166640
#> 4 42.5945770 2.4166880
#> 5 42.5945890 2.4167120
#> 6 42.5945999 2.4166340
#> 7 42.5946030 2.4166390
#> 8 42.5946100 2.4166260
#> 
#> attr(,"gpx_attributes")
#>                             version                             creator 
#>                               "1.0"                 "OpenStreetMap.org" 
#>                               xmlns 
#> "http://www.topografix.com/GPX/1/0" 
#> attr(,"class")
#> [1] "osmapi_gpx" "list"

Users

usrs <- osm_get_user_details(user_id = c(1, 24, 44, 45))
usrs
#>   id        display_name     account_created description  img contributor_terms
#> 1  1               Steve 2005-09-13 15:32:57             <NA>              TRUE
#> 2 24 Petter Reinholdtsen 2005-09-12 16:54:12             <NA>              TRUE
#> 3 44             user_44 2005-03-20 19:55:17             <NA>             FALSE
#> 4 45             user_45 2005-03-20 20:09:58             <NA>             FALSE
#>   roles changesets_count traces_count blocks_received.count
#> 1  <NA>             1156           23                     0
#> 2  <NA>              543           64                     0
#> 3  <NA>                0            0                     0
#> 4  <NA>                0            0                     0
#>   blocks_received.active blocks_issued.count blocks_issued.active
#> 1                      0                  NA                   NA
#> 2                      0                  NA                   NA
#> 3                      0                  NA                   NA
#> 4                      0                  NA                   NA