In this note I take a look at the OSMAR package. The basic purpose of the package is get data from OpenStreetMaps (OSM) and convert it into R-object that can be manipulated in R. For the current note I will be reading

  1. http://osmar.r-forge.r-project.org/
  2. http://osmar.r-forge.r-project.org/RJpreprint.pdf
  3. https://journal.r-project.org/archive/2013-1/eugster-schlesinger.pdf

The reason I am interested in this package is because it allows us to take a .pbf file from for example Geofabrik. In this way I can download OSM data for an entire country and not be constrained to relatively small bounding box as when using the package OSMDATA. There are other alternatives available such as for example OSMEXTRACT.

The first thing to remember is that OSM data is structured as nodes, ways and relations. Whereas typical data wrangling in R uses points, lines and polygons. This implies that when data is loaded into R from OSM the basic objects are transformed and when saved again the file format will also change.

This is important simply because different spatial operations performed in R will usually be performed using several packages that may or may not use different types of R-objects and read and write different types of files.

The osmar package extends the R ecosystem with infrastructure to work together with the OpenStreetMap project. So far, functionality is available to get data from different sources (e.g., planet file and API v0.6), to consolidate the data as an R osmar object, to work with the osmar object (e.g., subsetting and plotting), and to convert it to objects based on classes provided by other packages (e.g., to igraph and sp objects)

One limitation here is that the objects can be transformed into sp or igraph but not sf. This is not necessarily a big limitation since sp object can be tranformed in sf objects.

Getting data from OSM

To get the data from OSM the funtion get_osm() is used to get the data either from the OSM API or from a downloaded file.

The osmar package has not been updated since 2013. The osmsoruce_api() does not work. So I instead try to download an osm.pbf file from Geofabrik here

http://download.geofabrik.de/europe/denmark.html

and store that in some directory.

library(osmar)
mypath <- "C://Users//np83zg//Documents"
# The filename is: denmark-latest.osm.pbf
setwd(mypath)
src <- osmsource_osmosis("denmark-latest.osm.pbf")
cph_bbox <- center_bbox(12.4,55.5,12.8,55.9)
# cph <- get_osm(cph_bbox, src)

But this does not work either …. So either find some other way to work on large downloaded data from OSM or try to install osmosis and provide the path for osmosis for the call osmsource_osmosis(“denmark-latest.osm.pbf”)