-
Notifications
You must be signed in to change notification settings - Fork 66
/
README.Rmd
273 lines (205 loc) · 11.5 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# stplanr <a href='https://docs.ropensci.org/stplanr/'><img src='man/figures/logo.png' align="right" height=215/></a>
<!-- [![Build Status](https://travis-ci.org/ropensci/stplanr.svg?branch=master)](https://travis-ci.org/ropensci/stplanr) -->
[![rstudio mirror downloads](https://cranlogs.r-pkg.org/badges/stplanr)](https://github.com/r-hub/cranlogs.app)
[![](https://cranlogs.r-pkg.org/badges/grand-total/stplanr)](https://cran.r-project.org/package=stplanr)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/stplanr)](https://cran.r-project.org/package=stplanr)
[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![](https://badges.ropensci.org/10_status.svg)](https://github.com/ropensci/software-review/issues/10)
[![R-CMD-check](https://github.com/ropensci/stplanr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ropensci/stplanr/actions/workflows/R-CMD-check.yaml)
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(stplanr)
```
**stplanr** is a package for sustainable transport planning with R.
It provides functions for solving common problems in transport planning and modelling, such as how to best get from point A to point B.
The overall aim is to provide a reproducible, transparent and accessible toolkit to help people better understand transport systems and inform policy, as outlined in a [paper](https://journal.r-project.org/archive/2018/RJ-2018-053/index.html) about the package, and the potential for open source software in transport planning in general, published in the [R Journal](https://journal.r-project.org/).
The initial work on the project was funded by the Department of Transport
([DfT](https://www.gov.uk/government/organisations/department-for-transport))
as part of the development of the Propensity to Cycle Tool
(PCT), a web application to explore current travel patterns and cycling potential at zone, desire line, route and route network levels (see [www.pct.bike](https://www.pct.bike/) and click on a region to try it out).
The basis of the methods underlying the PCT is origin-destination data, which are used to highlight where many short distance trips are being made, and estimate how many could switch to cycling.
The results help identify where cycleways are most needed, an important component of sustainable transport planning infrastructure engineering and policy design.
See the package vignette (e.g. via `vignette("introducing-stplanr")`)
or an [academic paper on the Propensity to Cycle Tool (PCT)](https://dx.doi.org/10.5198/jtlu.2016.862)
for more information on how it can be used.
This README provides some basics.
Much of the work supports research undertaken at the Leeds' Institute for Transport Studies ([ITS](https://environment.leeds.ac.uk/transport)) but **stplanr** should be useful to transport researchers and practitioners needing free, open and reproducible methods for working with geographic data everywhere.
## Key functions
Data frames representing flows between origins and destinations
must be combined with geo-referenced zones or points to generate meaningful
analyses and visualisations of 'flows' or origin-destination (OD) data.
**stplanr** facilitates this with
`od2line()`, which takes flow and geographical data as inputs and
outputs spatial data. Some example data is provided in the package:
```{r, results='hide', message=FALSE}
library(stplanr)
```
Let's take a look at this data:
```{r}
od_data_sample[1:3, 1:3] # typical form of flow data
cents_sf[1:3,] # points representing origins and destinations
```
These datasets can be combined as follows:
```{r plot1, warning=FALSE}
travel_network <- od2line(flow = od_data_sample, zones = cents_sf)
w <- flow$all / max(flow$all) *10
plot(travel_network, lwd = w)
```
**stplanr** has many functions for working with OD data.
See the [`stplanr-od`](https://docs.ropensci.org/stplanr/articles/stplanr-od.html) vignette for details.
The package can also allocate flows to the road network, e.g. with [CycleStreets.net](https://www.cyclestreets.net/api/) and the OpenStreetMap Routing Machine ([OSRM](https://github.com/Project-OSRM/osrm-backend)) API interfaces.
These are supported in `route_*()` functions such as `route_cyclestreets` and `route_osrm()`:
Routing can be done using a range of back-ends and using lat/lon or desire line inputs with the `route()` function, as illustrated by the following commands which calculates the route between Fleet Street and Southwark Street over the River Thames on Blackfriars Bridge in London:
```{r, eval=FALSE, echo=FALSE}
tmaptools::geocode_OSM("fleet street london")
tmaptools::geocode_OSM("southwark street london")
```
```{r}
library(osrm)
trip <- route(
from = c(-0.11, 51.514),
to = c(-0.10, 51.506),
route_fun = osrmRoute,
returnclass = "sf"
)
plot(trip)
```
You can also use and place names, found using the Google Map API:
```{r cycle-trip, message=FALSE, warning=FALSE, eval=FALSE, echo=FALSE}
if(!Sys.getenv("CYCLESTREETS") == ""){
trip <- route_cyclestreets("Bradford, UK", "Leeds, Yorkshire", plan = "balanced")
plot(trip)
}
```
```{r}
trip2 <- route(
from = "Leeds",
to = "Bradford",
route_fun = osrmRoute,
returnclass = "sf"
)
plot(trip2)
```
We can replicate this call multiple times with the `l` argument in `route()`:
```{r}
desire_lines <- travel_network[2:6, ]
```
```{r, echo=FALSE}
# Sys.sleep(2) # wait a moment
```
Next, we'll calculate the routes:
```{r plot2, results='hide', message=FALSE}
routes <- route(
l = desire_lines,
route_fun = osrmRoute,
returnclass = "sf"
)
plot(sf::st_geometry(routes))
plot(desire_lines, col = "red", add = TRUE)
```
<!-- The resulting routes will look something like this: -->
```{r routes, echo=FALSE, eval=FALSE}
lwd <- desire_lines$foot
routes <- routes_fast_sf[2:6, ]
plot(routes$geometry, lwd = lwd)
plot(desire_lines$geometry, col = "green", lwd = lwd, add = TRUE)
```
For more examples, `example("route")`.
`overline()` takes a series of route-allocated lines,
splits them into unique segments and aggregates
the values of overlapping lines. This can represent where there will be
most traffic on the transport system, as demonstrated in the following code chunk.
```{r rnet, warning=FALSE}
routes$foot <- desire_lines$foot
rnet <- overline(routes, attrib = "foot")
```
The resulting route network, with segment totals calculated from overlapping parts for the routes for walking, can be visualised as follows:
```{r}
plot(rnet["foot"], lwd = rnet$foot)
```
The above plot represents the number walking trips made (the 'flow') along particular segments of a transport network.
<!-- (results not shown): -->
```{r, routes-leaf, eval=FALSE, echo=FALSE}
library(leaflet)
pal = leaflet::colorNumeric(palette = "YlGnBu", domain = rnet$all)
leaflet(data = rnet) %>%
addProviderTiles(providers$OpenStreetMap.BlackAndWhite) %>%
addPolylines(weight = rnet$all / 3, color = ~pal(all), opacity = 0.9) %>%
addLegend(pal = pal, values = ~all)
```
## Policy applications
The examples shown above, based on tiny demonstration datasets, may not seem particularly revolutionary.
At the city scale, however, this type of analysis can be used to inform sustainable transport policies, as described in papers [describing the Propensity to Cycle Tool](https://www.jtlu.org/index.php/jtlu/article/view/862/859) (PCT), and its [application to calculate cycling to school potential](https://doi.org/10.1016/j.jth.2019.01.008) across England.
Results generated by **stplanr** are now part of national government policy: the PCT is the recommended tool for local and regional authorities developing strategic cycle network under the Cycling and Walking Infrastructure Strategy ([CWIS](https://www.gov.uk/government/publications/cycling-and-walking-investment-strategy)), which is part of the Infrastructure Act [2015](https://www.legislation.gov.uk/ukpga/2015/7/contents/enacted).
**stplanr** is helping dozens of local authorities across the UK to answer the question: where to prioritise investment in cycling?
In essence, stplanr was designed to support sustainable transport policies.
There are many other research and policy questions that functions in **stplanr**, and other open source software libraries and packages, can help answer.
At a time of climate, health and social crises, it is important that technology is not only sustainable itself (e.g. as enabled by open source communities and licenses) but that it contributes to a sustainable future.
## Installation
To install the stable version, use:
```{r, eval=FALSE}
install.packages("stplanr")
```
The development version can be installed using **devtools**:
```{r, eval=FALSE}
# install.packages("devtools") # if not already installed
devtools::install_github("ropensci/stplanr")
library(stplanr)
```
### Installing stplanr on Linux and Mac
**stplanr** depends on **sf**. Installation instructions for Mac, Ubuntu and other Linux distros can be found here: https://github.com/r-spatial/sf#installing
## Funtions, help and contributing
The current list of available functions can be seen on the package's website at [docs.ropensci.org/stplanr/](https://docs.ropensci.org/stplanr/), or with the following command:
```{r, eval=FALSE}
lsf.str("package:stplanr", all = TRUE)
```
To get internal help on a specific function, use the standard way.
```{r, eval=FALSE}
?od2line
```
To contribute, report bugs or request features, see the [issue tracker](https://github.com/ropensci/stplanr/issues).
```{r, eval=FALSE, echo=FALSE}
# Aim: explore dependencies
desc = read.dcf("DESCRIPTION")
headings = dimnames(desc)[[2]]
fields = which(headings %in% c("Depends", "Imports", "Suggests"))
pkgs = paste(desc[fields], collapse = ", ")
pkgs = gsub("\n", " ", pkgs)
strsplit(pkgs, ",")[[1]]
install.packages("miniCRAN")
library(miniCRAN)
tags <- "stplanr"
pkgDep(tags)
dg <- makeDepGraph(tags, enhances = TRUE)
set.seed(1)
plot(dg, legendPosition = c(-1, 1), vertex.size = 20)
library(DiagrammeR)
DiagrammeR::visnetwork(dg)
visNetwork::visIgraph(dg)
```
## Further resources / tutorials
Want to learn how to use open source software for reproducible sustainable transport planning work?
Now is a great time to learn.
Transport planning is a relatively new field of application in R.
However, there are already some good resources on the topic, including (any further suggestions: welcome):
- The Transport chapter of *Geocomputation with R*, which provides a broad introduction from a geographic data perspective: https://r.geocompx.org/transport.html
- The **stplanr** paper, which describes the context in which the package was developed: https://journal.r-project.org/archive/2018/RJ-2018-053/index.html (please cite this if you use **stplanr** in your work)
- The `dodgr` vignette, which provides an introduction to routing in R: https://github.com/UrbanAnalyst/dodgr
## Meta
* Please report issues, feature requests and questions to the [github issue tracker](https://github.com/ropensci/stplanr/issues)
* License: MIT
* Get citation information for **stplanr** in R doing `citation(package = 'stplanr')`
* This project is released with a [Contributor Code of Conduct](https://github.com/ropensci/stplanr/blob/master/CONDUCT.md).
By participating in this project you agree to abide by its terms.
[![rofooter](https://ropensci.org/public_images/github_footer.png)](https://ropensci.org)