Slopes: a package for reproducible slope calculation, analysis and visualisation
2021-09-30, 16:30–17:00, Buenos Aires

Slopes are important for many purposes, including flood risk, agriculture, geology, and infrastructure constructions.
In transport planning, consideration of gradient is especially important for walking and cycling routes, which provided our initial motivation for developing this package.
Slope gradient attributes of vector geometries can be calculated using proprietary products such as ArcMap. But to enable transparency, reproducibility and accessibility of used methods, we want to be able to calculate slopes using free and open source software.
We developed an R package - slopes - due prior experience with the language and the mature 'R-spatial' community which has developed mature codebases for working with geographic data in a reproducible command line environment, including sf (for working with vector datasets representing roads and other linear features) and raster (for representing digital elevation models, DEMs).

Building on these foundations the slopes package is now working and has been used to calculate slopes on thousand of roads in several cities across the globe, as long as it has elevation data to work with.
Comparison with ArcMap's 3D analyst show that the approach is competitive with the go-to proprietary produce in terms of computational speed and that we can reproduce ArcMap's results: tests show an R-squared value of 0.977.
We hope the package will be of use and interest to the FOSS4G and in the talk will discuss ideas for taking the work forward, e.g. by implementing the logic into other languages/environments such as Rust, Python and even as a QGIS plugin.
Could there be scope for an inter-disciplinary and language-agnostic community interested in slope analysis?
We would like support efforts to strengthen links between geospatial developers who use R and the wider FOSS4G community, for example by comparing the slopes package with other open source approaches for slope calculation and analysis for mutually beneficial learning.
We will conclude with discussion of possible future directions of travel for the project, including possibilities for 3D visualisation, auto-download of elevation point data sampled across linear features (currently the package automates the download of DEM data but requires a MapBox API key) and using the slope values to enter as a variable in route choice algorithms in route networks, as well as generate evidence in support of sustainable transport policies.

The package calculates longitudinal steepness of linear features such as roads and rivers, based on two main inputs: vector linestring geometries and raster digital elevation model (DEM) datasets.

After installing R, it can be installed as follows:

r remotes::install_github("itsleeds/slopes")

The minimum data requirements for using the package are elevation points, either as a vector stored in 'xyz' coordinates, a matrix or as a digital elevation model (DEM) encoded as a raster dataset.
Typically linear features, the slopes of which are to be calculated, will be imported from a .geojson, .gpkg or similar vector file format.
The process of calculating slopes for roads in Lisbon, Portugal, with vector and raster input datasets is shown in the below.


input data

class(dem_lisbon_raster) # digital elevation model
class(lisbon_road_segments) # vector dataset

visualize input data

plot(sf::st_geometry(lisbon_road_segments), add = TRUE)
fig 1


calculate average gradient of each road segment

lisbon_road_segments$slope = slope_raster(lisbon_road_segments, e = dem_lisbon_raster)

visualise slopes

plot(lisbon_road_segments["slope"], add = TRUE, lwd = 5)
fig 2

This created a new column, slope that represents the average, distance weighted slope associated with each road segment. The units represent the percentage incline, that is the change in elevation divided by distance. This result is equivalent to that returned by ESRI’s Slope_3d() in the 3D Analyst extension, with a correlation between the ArcMap implementation and our implementation of more than 0.95 on our test dataset (we find higher correlations on larger datasets).

Many slopes' values can be retrieved with this package, such as average slope, max, min, direction, and so on.

Authors and Affiliations

Rosa Félix (1)
Robin Lovelace (2)

(1) Instituto Superior Técnico, University of Lisbon, Portugal
(2) Institute for Transport Studies, University of Leeds, UK

Requirements for the Attendees





Open and Reproducible Science


1 - Principiants. No required specific knowledge is needed.

Language of the Presentation


Rosa Félix is a PhD in Transportation Systems and urban cycling mobility researcher at U-Shift lab. Rosa is skilled in GIS and R. She is a cycling activist in Lisbon, and project coordinator of a local community bicycle shop.