-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME.Rmd
199 lines (150 loc) · 9.57 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# abstr
<!-- badges: start -->
[![R-CMD-check](https://github.com/a-b-street/abstr/workflows/R-CMD-check/badge.svg)](https://github.com/a-b-street/abstr/actions)
<!-- badges: end -->
<img src="https://user-images.githubusercontent.com/22789869/129973263-5fc74ae3-ed17-4155-9a8c-7f382f7796cc.png" align="left" height="164px" width="164px" margin="10%" />
--
**abstr provides an R interface to the [A/B Street](https://github.com/a-b-street/abstreet#ab-street) transport system simulation and network editing software. It provides functions for converting origin-destination data, combined with data on buildings representing origin and destination locations, into `.json` files that can be directly imported into the A/B Street city simulation.**
See the formats page in the [A/B Street documentation](https://a-b-street.github.io/docs/tech/dev/formats/scenarios.html) for details of the schema that the package outputs.
## Installation
You can install the released version of abstr from
[CRAN](https://CRAN.R-project.org) with:
```{r, eval=FALSE}
install.packages("abstr")
```
Install the development version from GitHub as follows:
```{r, eval=FALSE}
remotes::install_github("a-b-street/abstr")
```
## Usage
The example below shows how `abstr` can be used.
The input datasets include `sf` objects representing buildings, origin-destination (OD) data represented as desire lines and administrative zones representing the areas within which trips in the desire lines start and end.
With the exception of OD data, each of the input datasets is readily available for most cities.
The input datasets are illustrated in the plots below, which show example data shipped in the package, taken from the Seattle, U.S.
```{r input, fig.cap="Example data that can be used as an input by functions in abstr to generate trip-level scenarios that can be imported by A/B Street."}
library(abstr)
library(tmap) # for map making
tm_shape(montlake_zones) + tm_polygons(col = "grey") +
tm_shape(montlake_buildings) + tm_polygons(col = "blue") +
tm_style("classic")
```
The map above is a graphical representation of the Montlake residential neighborhood in central Seattle, Washington. Here, `montlake_zones` represents neighborhood residential zones declared by Seattle local government and `montlake_buildings` being the accumulation of buildings listed in <a link href="https://www.openstreetmap.org/#map=5/54.910/-3.432"> OpenStreetMap</a>
The final piece of the `abstr` puzzle is OD data.
```{r output-sf, message=FALSE, warning=FALSE}
head(montlake_od)
```
In this example, the first two columns correspond to the origin and destination zones in Montlake, with the subsequent columns representing the transport mode share between these zones.
Let's combine each of the elements outlined above, the zone, building and OD data.
We do this using the `ab_scenario()` function in the `abstr` package, which generates a data frame representing tavel between the `montlake_buildings`.
While the OD data contains information on origin and destination zone, `ab_scenario()` 'disaggregates' the data and randomly selects building within each origin and destination zone to simulate travel at the individual level, as illustrated in the chunk below which uses only a sample of the `montlake_od` data, showing travel between three pairs of zones, to illustrate the process:
```{r message=FALSE,warning=FALSE}
set.seed(42)
montlake_od_minimal = subset(montlake_od, o_id == "373" |o_id == "402" | o_id == "281" | o_id == "588" | o_id == "301" | o_id == "314")
output_sf = ab_scenario(
od = montlake_od_minimal,
zones = montlake_zones,
zones_d = NULL,
origin_buildings = montlake_buildings,
destination_buildings = montlake_buildings,
pop_var = 3,
time_fun = ab_time_normal,
output = "sf",
modes = c("Walk", "Bike", "Drive", "Transit")
)
```
The `output_sf` object created above can be further transformed to match [A/B Street's schema](https://a-b-street.github.io/docs/tech/dev/formats/scenarios.html) and visualised in A/B Street, or visualised in R (using the `tmap` package in the code chunk below):
```{r outputplot}
tm_shape(output_sf) + tmap::tm_lines(col = "mode", lwd = .8, lwd.legeld.col = "black") +
tm_shape(montlake_zones) + tmap::tm_borders(lwd = 1.2, col = "gray") +
tm_text("id", size = 0.6) +
tm_style("cobalt")
```
Each line in the plot above represents a single trip, with the color representing each transport mode. Moreover, each trip is configured with an associated departure time, that can be represented in A/B Street.
The `ab_save` and `ab_json` functions conclude the `abstr` workflow by outputting a local JSON file, matching the [A/B Street's schema](https://a-b-street.github.io/docs/tech/dev/formats/scenarios.html).
```{r message=FALSE, warning=FALSE}
output_json = ab_json(output_sf, time_fun = ab_time_normal, scenario_name = "Montlake Example")
ab_save(output_json, f = "montlake.json")
```
Let's see what is in the file:
```r
file.edit("montlake.json")
```
The first trip schedule should look something like this, matching [A/B Street's schema](https://a-b-street.github.io/docs/tech/dev/formats/scenarios.html).
```json
{
"scenario_name": "Montlake Example",
"people": [
{
"trips": [
{
"departure": 317760000,
"origin": {
"Position": {
"longitude": -122.3139,
"latitude": 47.667
}
},
"destination": {
"Position": {
"longitude": -122.3187,
"latitude": 47.6484
}
},
"mode": "Walk",
"purpose": "Shopping"
}
]
}
```
## Importing scenario files into A/B Street
![](https://user-images.githubusercontent.com/22789869/128907563-4aa95b30-a98d-4fbc-9275-97e0b30dd227.gif)
After generating a `ab_scenario.json`, you can import and simulate it as follows.
1. Install the [latest build](https://a-b-street.github.io/docs/user/index.html) of A/B Street for your platform.
2. Run the software, and choose "Sandbox" on the title screen.
3. If necessary, change the map to the Montlake district of Seattle, or whichever map your JSON scenario covers.
4. Change the scenario from the default "weekday" pattern. Choose "import JSON scenario," then select your `ab_scenario.json` file.
After you successfully import this file once, it will be available in the list of scenarios, under the "Montlake Example" name, or whatever `name` specified by the JSON file.
You can generate scenarios for any city in the world. See [here](https://a-b-street.github.io/docs/user/new_city.html) for how to import new cities into A/B Street.
Note: Instead of installing a pre-built version of A/B Street in the first step, feel free to [build from source](https://a-b-street.github.io/docs/tech/dev/index.html), but it's not necessary for any integration with the `abstr` package.
### Advanced: scripting imports
If you're generating many JSON scenarios, you might not want to manually use A/B Street's user interface to import each file.
You can instead run a command to do the import.
See the docs at [a-b-street.github.io/docs/tech/dev/](https://a-b-street.github.io/docs/tech/dev/index.html) for details, but the basic steps are:
1. Install the [latest build](https://a-b-street.github.io/docs/user/index.html) of A/B Street for your platform, or [build from source](https://a-b-street.github.io/docs/tech/dev/index.html).
2. From the main A/B Street repo directory import the scenario
These steps can be achieved by running the following lines of code (run the commented lines of code to install Rust, clone the A/B Street repo and set the working directory, you can also replace `../montlake.json` with a different path to the scenario file):
```bash
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # install rust
# git clone [email protected]:a-b-street/abstreet
# cd abstreet
# cargo run --bin updater -- download --minimal
cargo run --bin cli -- import-scenario --input ../montlake.json --map data/system/us/seattle/maps/montlake.bin
cargo run --bin game --release
```
If you're using Windows, you'll instead run `cli.exe`.
If you're building from source use the following command:
```bash
cargo run --release --bin cli -- import-scenario --input path/to/montlake.json --map data/system/us/seattle/maps/montlake.bin
```
```{r, include=FALSE}
file.remove("montlake.json")
```
## Next steps
For a more comprehensive guide in the art of collecting, transforming and saving data for A/B Street, check out the `abstr` [documentation](https://a-b-street.github.io/abstr/).
The package website, hosted at [a-b-street.github.io/abstr](https://a-b-street.github.io/abstr/), contains articles that will help you get going with `abstr`.
See the following articles for reproducible examples that will help you getting your valuable origin-destination and activity data into a dynamic transport simulation environment for visualisation, model exaperiments and more:
- The [`abstr` vignette](https://a-b-street.github.io/abstr/articles/abstr.html) for more detail on getting started with the package and context
- The [`activity` vignette](https://a-b-street.github.io/abstr/articles/activity.html) on representing multi-trip-per-person activity models in R and A/B Street
- The [`pct_to_abstr` vignette](https://a-b-street.github.io/abstr/articles/pct_to_abstr.html) on importing output from an established project, the Propensity to Cycle Tool, into A/B Street