Archives de catégorie : R-EN

Blog posts in english

New version of osrm

The osrm package is an interface between R and the OSRM API. OSRM is a routing service based on OpenStreetMap data.
This package allows computing shortest paths, travel time and travel distance matrices between points.

The osrm package functions are:

  • osrmTable(): travel time and travel distance matrices between points.
  • osrmRoute(): shortest path between two points.
  • osrmTrip(): shortest trip between multiple unordered points.
  • osrmIsochrone(): polygons of isochrones

This package relies on the use of an OSRM server (tested with version 5.22.0 of OSRM).
By default the package uses the OSRM demo server (API usage policy). It is possible to use a different server if you want to make intensive use of the API. You can run your own instance of OSRM following guidelines provided on the OSRM GitHub repository. The simplest solution is the one based on docker containers.

The main change introduced by this update is the support of sf objects as input and output in all functions: using the argument returnclass = "sf" in osrmRoute(), osrmIsochrone() and osrmTrip() allows to output sf objects.

The algorithm for isochrones has been changed to a more robust one that uses isoband package.

In the following example more than 800 shortest paths to the useR2019 conference in Toulouse are displayed:

In the next example isochrones around Toulouse are displayed:

Code for the figures is in this gist.

The popcircle package

The popcircle package has been released on GitHub. This one-function package computes circles with areas scaled to a variable and displays them using a compact layout (higher values in the center, lower values at the periphery). Original polygons are scaled to fit inside these circles (size are roughly proportional, not strictly).

The circles creation relies on packcircles, spatial data manipulation relies on sf.

## Package install:
library(remotes)
install_github("rCarto/popcircle")

This is a typical example of the package usage based on the dataset shipped with the package. We use cartography to display labels.

library(sf)
library(popcircle)
library(cartography)
mtq <- st_read(system.file("gpkg/mtq.gpkg", 
                           package="popcircle"))
res <- popcircle(x = mtq, var = "POP")
circles <- res$circles
shapes <- res$shapes
par(mar = c(0,0,0,0))
plot(st_geometry(circles), col = "#bcd39c", 
     border = "white", bg = "#eafdcf")
plot(st_geometry(shapes), col = "#fffc99", 
     border = "#fffc99", add = TRUE)
labelLayer(x = circles[1:20,], txt = "LIBGEO",
           halo = TRUE, col ="#8e8358", 
           cex = seq(.8,.4, length.out = 20),
           font = 2, bg = "white", r = .15, 
           overlap = FALSE)

The next example was a bit more difficult to design. We had to work on some multipolygons countries (e.g. France, USA or Russia) in order to keep only the largest polygon.

Code for the figure
Code for the figure

As popcircle produces sf objects it is possible to display them interactively:

You can find here an example of interactive visualisation using leaflet.

popcircle changes the position of spatial units. It will work better with regions already well known to the target audience. Chances are that the first figure on Martinique municipalities will be more appropriate and effective for the inhabitants of Martinique.

The tanaka package

The tanaka package has been released on CRAN. This package is a simplified implementation of the Tanaka method.
Also called “relief contours method”, “illuminated contour method” or “shaded contour lines method”, the Tanaka method enhances the representation of topography on a map using shaded contour lines.
North-west white contours represent illuminated topography and south-east black contours represent shaded topography.

The contour lines creation relies on isoband, spatial data manipulation and display rely on sf.

tanaka is a small package with only two functions:

  • tanaka() uses a raster object and displays the map directly;
  • tanaka_contour() builds the isopleth polygon layer.

This is a typical example of the package usage based on the dataset shipped with the package.

library(tanaka)
library(raster)
ras <- raster(system.file("grd/elev.grd", 
                          package = "tanaka"))
tanaka(x = ras, breaks = seq(80,400,20), 
       legend.pos = "topright", 
       legend.title = "Elevation\n(meters)")

In the second example, the elevatr package is used to download an elevation raster on a specific area. Then the tanaka_contour() function is used to create an isopleth layer and finally the tanaka()function is used to to display the map with a custom color palette.

library(tanaka)
library(elevatr)
# use elevatr to get elevation data
ras <- get_elev_raster(
  locations = data.frame(
    x = c(6.7, 7), y = c(45.8,46)
  ),
  z = 10, prj = "+init=epsg:4326", 
  clip = "locations"
)
# create the isopleth layer
iso <- tanaka_contour(
  x = ras, 
  breaks = seq(500,4800,250)
)
# display the isopleth layer
plot(st_geometry(iso))
# create a custom color palette
pal <- colorRampPalette(colors = c("#F9D3A1", "#1E315B"))
# display the map
tanaka(iso, col = pal(nrow(iso)))

The last example illustrates the use of tanaka with non-topographical data. This map is based on the Global Human Settlement Population Grid (1km).

Code for this figure

Cartographic Explorations of the OpenStreetMap Database with R

This post exposes some cartographic explorations of the OpenStreetMap (OSM) database with R.
These explorations begin with the downloading and the cleaning of OSM data. Then I propose a set of map visualizations of the spatial distributions of bars and restaurants in Paris. Of course, these examples could be adapted to other spatial contexts and thematics (e.g. pharmacies in Roma, bike parkings in Dublin…).

This reproducible analysis is hosted on GitHub (code + data + walk-through).

Continuer la lecture de Cartographic Explorations of the OpenStreetMap Database with R

New version of the cartography package

A new version of the cartography package (v2.0.1) has arrived on CRAN.

cartography allows various cartographic representations such as proportional symbols, chroropleth, typology, flows or discontinuities maps. It also offers several features enhancing the graphic presentation of maps like cartographic palettes, layout elements (scale, north arrow, title…), labels, legends or access to some cartographic APIs.

Up to version 1.4.2 cartography was mainly based on sp and rgeos for its spatial data management and geoprocessing operations. These dependencies have been as much as possible replaced by sf functions since version 2.0.0.
Most functions are kept unchanged except for the addition of an x argument used to take sf objects as inputs.
See the NEWS file for the full list of changes and see sf README in case of installation problems with sf.
Continuer la lecture de New version of the cartography package

Create and integrate maps in your R workflow with the cartography package

The cartography package allows various cartographic representations such as proportional symbols, chroropleth, typology, flows or discontinuities. In addition it also proposes some useful features like cartographic palettes, layout (scale, north arrow, title…), labels, legends or access to cartographic API to ease the graphic presentation of maps. Continuer la lecture de Create and integrate maps in your R workflow with the cartography package