DatabaseGISR

Free data in R – OSM

We often use data from the OpenStreetMap project in our posts.

Where do we get them from? We get them via R libraries.

Today we will show you how to do that.

We download the osmdata package and install it:

install.packages('osmdata')
library(osmdata)

We will download data for a specific area. We will create this area as a vector of geographic coordinates:

bb = c(20.9, 52.2, 21.0, 52.3)

From the OSM project we can take all the data or select certain ones. We have to keep in mind that limiting the selection will speed up the download. All available layers can be found at https://wiki.openstreetmap.org/wiki/Map_Features. We will choose our area of restaurants. We will do it with this line of code:

dt <- opq (bbox = bb) %>%
 add_osm_feature (key = 'cuisine') %>%
 osmdata_sf()

, where

opq is a query to OSM

add_osm_feature adds layers containing cuisine related objects key=’cuisine’ to the query.

osmdata_sf writes the results to the class sf

The results are layers with points and polygons:

> dt
Object of class 'osmdata' with:
                $bbox : 52.2,20.9,52.3,21
       $overpass_call : The call submitted to the overpass API
                $meta : metadata including timestamp and version numbers
          $osm_points : 'sf' Simple Features Collection with 479 points
           $osm_lines : NULL
        $osm_polygons : 'sf' Simple Features Collection with 21 polygons
      $osm_multilines : NULL
   $osm_multipolygons : NULL

Display the result on the map using leaflet:

leaflet() %>% addTiles() %>% addCircleMarkers(data = dt$osm_points, radius = 1)
osmdata_1

We can also define an area by using the name of the area for which we want to retrieve data:

bb = getbb("Warsaw")

We retrieve the data, this time delimiting the restaurants that serve pizza, and store it in the class sp:

dt <- opq (bbox = bb) %>%
 add_osm_feature (key = 'cuisine',value = 'pizza') %>%
 osmdata_sp()

Display result:

leaflet() %>% addTiles() %>% addCircleMarkers(data = dt$osm_points, radius = 1)
osmdata_2

In the next exercises, you can work with data selected for your region of residence, for example. Test the newly learned library.

Leave a Reply

Your email address will not be published. Required fields are marked *