Title: | R Interface to the A/B Street Transport System Simulation Software |
---|---|
Description: | Provides functions to convert origin-destination data, represented as straight 'desire lines' in the 'sf' Simple Features class system, into JSON files that can be directly imported into A/B Street <https://www.abstreet.org>, a free and open source tool for simulating urban transport systems and scenarios of change <doi:10.1007/s10109-020-00342-2>. |
Authors: | Nathanael Sheehan [aut, cre] |
Maintainer: | Nathanael Sheehan <[email protected]> |
License: | Apache License (>= 2) |
Version: | 0.4.1 |
Built: | 2025-03-12 05:01:08 UTC |
Source: | https://github.com/a-b-street/abstr |
This function takes outputs from ab_scenario()
and returns a list that
can be saved as a JSON file for import into A/B Street.
ab_json( desire_lines, mode_column = NULL, time_fun = ab_time_normal, scenario_name = "test", default_purpose = "Work", ... )
ab_json( desire_lines, mode_column = NULL, time_fun = ab_time_normal, scenario_name = "test", default_purpose = "Work", ... )
desire_lines |
OD data represented as geographic lines created by
|
mode_column |
The column name in the desire lines data that contains
the mode of transport. |
time_fun |
The function used to calculate departure times.
|
scenario_name |
The name of the scenario to appear in A/B Street.
The default value is |
default_purpose |
In case a |
... |
Additional arguments to pass to |
Note: the departure time in seconds is multiplied by 10000 on conversion to a .json list object for compatibility with the A/B Street schema.
A list that can be saved as a JSON file with ab_save()
# Starting with tabular data od = leeds_od od[[1]] = c("E02006876") zones = leeds_zones ablines = ab_scenario(od, zones = zones) ab_list = ab_json(ablines, mode_column = "mode", scenario_name = "test") ab_list$scenario f = tempfile(fileext = ".json") ab_save(ab_list, f) readLines(f)[1:30] # Starting with JSON data from A/B Street (multiple trips per person) f = system.file("extdata/minimal_scenario2.json", package = "abstr") desire_lines = ab_sf(f) desire_lines json_list = ab_json(desire_lines) json_list
# Starting with tabular data od = leeds_od od[[1]] = c("E02006876") zones = leeds_zones ablines = ab_scenario(od, zones = zones) ab_list = ab_json(ablines, mode_column = "mode", scenario_name = "test") ab_list$scenario f = tempfile(fileext = ".json") ab_save(ab_list, f) readLines(f)[1:30] # Starting with JSON data from A/B Street (multiple trips per person) f = system.file("extdata/minimal_scenario2.json", package = "abstr") desire_lines = ab_sf(f) desire_lines json_list = ab_json(desire_lines) json_list
Save OD data as JSON files for import into A/B Street
ab_save(x, f)
ab_save(x, f)
x |
A list object produced by |
f |
A filename, e.g. |
A JSON file containing scenarios from ab_scenario()
This function takes a data frame representing origin-destination trip data in 'long' form, zones and, optionally, buildings from where trips can start and end as inputs.
ab_scenario( od, zones, zones_d = NULL, origin_buildings = NULL, destination_buildings = NULL, pop_var = 3, time_fun = ab_time_normal, output = "sf", modes = c("Walk", "Bike", "Transit", "Drive"), ... )
ab_scenario( od, zones, zones_d = NULL, origin_buildings = NULL, destination_buildings = NULL, pop_var = 3, time_fun = ab_time_normal, output = "sf", modes = c("Walk", "Bike", "Transit", "Drive"), ... )
od |
Origin destination data with the first 2 columns containing zone code of origin and zone code of destination. Subsequent columns should be mode names such as All and Walk, Bike, Transit, Drive, representing the number of trips made by each mode of transport for use in A/B Street. |
zones |
Zones with IDs that match the desire lines. Class: |
zones_d |
Optional destination zones with IDs
that match the second column of the |
origin_buildings |
Polygons where trips will originate ( |
destination_buildings |
Polygons where trips can end, represented as |
pop_var |
The variable containing the total population of each desire line. |
time_fun |
The function used to calculate departure times.
|
output |
Which output format?
|
modes |
Character string containing the names of the modes of travel to
include in the outputs. These must match column names in the |
... |
Additional arguments to pass to |
An sf
object by default representing individual trips
between randomly selected points (or buildings when available)
between the zones represented in the OD data.
od = leeds_od zones = leeds_zones od[[1]] = c("E02006876") ablines = ab_scenario(od, zones = zones) plot(ablines) table(ablines$mode) colSums(od[3:7]) # 0.17 vs 0.05 for ab_scenario ablines = ab_scenario(od, zones = zones, origin_buildings = leeds_buildings) plot(leeds_zones$geometry) plot(leeds_buildings$geometry, add = TRUE) plot(ablines["mode"], add = TRUE) ablines_json = ab_json(ablines, scenario_name = "test") od = leeds_desire_lines names(od)[4:6] = c("Walk", "Bike", "Drive") ablines = ab_scenario( od = od, zones = leeds_site_area, zones_d = leeds_zones, origin_buildings = leeds_houses, destination_buildings = leeds_buildings, output = "sf" ) plot(ablines) plot(ablines$geometry) plot(leeds_site_area$geometry, add = TRUE) plot(leeds_buildings$geometry, add = TRUE)
od = leeds_od zones = leeds_zones od[[1]] = c("E02006876") ablines = ab_scenario(od, zones = zones) plot(ablines) table(ablines$mode) colSums(od[3:7]) # 0.17 vs 0.05 for ab_scenario ablines = ab_scenario(od, zones = zones, origin_buildings = leeds_buildings) plot(leeds_zones$geometry) plot(leeds_buildings$geometry, add = TRUE) plot(ablines["mode"], add = TRUE) ablines_json = ab_json(ablines, scenario_name = "test") od = leeds_desire_lines names(od)[4:6] = c("Walk", "Bike", "Drive") ablines = ab_scenario( od = od, zones = leeds_site_area, zones_d = leeds_zones, origin_buildings = leeds_houses, destination_buildings = leeds_buildings, output = "sf" ) plot(ablines) plot(ablines$geometry) plot(leeds_site_area$geometry, add = TRUE) plot(leeds_buildings$geometry, add = TRUE)
This function takes a path to a JSON file representing an A/B Street
scenario, or an R representation of the JSON in a list, and returns
an sf
object with the same structure as objects returned by
ab_scenario()
.
ab_sf(json)
ab_sf(json)
json |
Character string or list representing a JSON file or list that has been read into R and converted to a data frame. |
Note: the departure time in seconds is divided by 10000 on conversion to represent seconds, which are easier to work with that 10,000th of a second units.
An sf
data frame representing travel behaviour scenarios
from, and which can be fed into, A/B Street. Contains the following
columns: person (the ID of each agent in the simulation),
departure (seconds after midnight of the travel starting),
mode (the mode of transport, being Walk
, Bike
, Transit
and Drive
),
purpose (what the trip was for, e.g. Work
), and
geometry (a linestring showing the start and end point of the trip/stage).
file_name = system.file("extdata/minimal_scenario2.json", package = "abstr") ab_sf(file_name) json = jsonlite::read_json(file_name, simplifyVector = TRUE) ab_sf(json)
file_name = system.file("extdata/minimal_scenario2.json", package = "abstr") ab_sf(file_name) json = jsonlite::read_json(file_name, simplifyVector = TRUE) ab_sf(json)
Generate times for A/B scenarios
ab_time_normal(hr = 8.5, sd = 0.5, n = 1)
ab_time_normal(hr = 8.5, sd = 0.5, n = 1)
hr |
Number representing the hour of day of departure (on average). 8.5, for example represents 08:30. |
sd |
The standard deviation in hours of the distribution |
n |
The number of numbers representing times to return |
An integer representing the time since midnight in seconds
time_lunch = ab_time_normal(hr = 12.5, sd = 0.25) time_lunch # Back to a formal time class as.POSIXct(trunc(Sys.time(), units="days") + time_lunch) time_morning = ab_time_normal(hr = 8.5, sd = 0.5) as.POSIXct(trunc(Sys.time(), units="days") + time_morning) time_afternoon = ab_time_normal(hr = 17, sd = 0.75) as.POSIXct(trunc(Sys.time(), units="days") + time_afternoon)
time_lunch = ab_time_normal(hr = 12.5, sd = 0.25) time_lunch # Back to a formal time class as.POSIXct(trunc(Sys.time(), units="days") + time_lunch) time_morning = ab_time_normal(hr = 8.5, sd = 0.5) as.POSIXct(trunc(Sys.time(), units="days") + time_morning) time_afternoon = ab_time_normal(hr = 17, sd = 0.75) as.POSIXct(trunc(Sys.time(), units="days") + time_afternoon)
Each row of this table contains a building that exists within a
zone in the montlake_zones
table.
A sf dataframe with columns:
OSM ID assigned to each building.
OSM name assigned to each building (might be NA).
OSM building category assigned to each building.
Simple feature collection (sfc) contain multipolygons, each representing the boundaries of a building.
These buildings were retrieved using osmextract::oe_read()
. See the code used to
create this data in data-raw/montlake-test-data.R
.
OpenStreetMap
library(sf) names(montlake_buildings) head(montlake_buildings$osm_way_id) head(montlake_buildings$name) head(montlake_buildings$building) nrow(montlake_buildings) plot(montlake_buildings)
library(sf) names(montlake_buildings) head(montlake_buildings$osm_way_id) head(montlake_buildings$name) head(montlake_buildings$building) nrow(montlake_buildings) plot(montlake_buildings)
Each row of this table contains a count of how many trips started in
column o_id
and ended in column d_id
according to different modes.
This example table has modes that match what A/B Street currently uses:
"Drive", "Transit", "Bike", and "Walk".
montlake_od
montlake_od
A data frame with columns:
Trip origin zone ID (must match an ID in montlake_zone
).
Trip destination zone ID (must match an ID in montlake_zone
).
Count of how many trips were made using cars.
Count of how many trips were made using public transit.
Count of how many trips were made using bikes.
Count of how many trips were made on foot.
See the code used to create this data in "data-raw/montlake-test-data.R"
Each row of this table contains the zone ID and geometry for a TAZ near
Montlake. Includes all zones that start or end within the Montlake
boundary and have at least 25 trips across all modes of transit in
montlake_od
.
montlake_zones
montlake_zones
A sf dataframe with columns:
Zone ID (must match o_id
or d_id
in montlake_od
).
Simple feature collection (sfc) contain multipolygons, each representing the boundaries of a TAZ near Montlake.
See the code used to create this data in "data-raw/montlake-test-data.R"
Each row of this table contains a single trip of people in the São Paulo city.
sao_paulo_activity_df_2
represents the movement of 2 people,
sao_paulo_activity_df_20
represents the movement of 20 people.
sao_paulo_activity_df_20
sao_paulo_activity_df_20
A data frame with columns:
Person identifier.
Origin coordinate X.
Origin coordinate Y.
Destination coordinate X.
Destination coordinate Y.
Main mode.
Departure hour.
Departure minute.
See the code used to create this data, and the full open dataset with 128 variables, the file data-raw/sao-paulo-activity-data.R in the package's GitHub repo.
dim(sao_paulo_activity_df_20) names(sao_paulo_activity_df_20) head(sao_paulo_activity_df_20) dim(sao_paulo_activity_df_2) names(sao_paulo_activity_df_2) head(sao_paulo_activity_df_2)
dim(sao_paulo_activity_df_20) names(sao_paulo_activity_df_20) head(sao_paulo_activity_df_20) dim(sao_paulo_activity_df_2) names(sao_paulo_activity_df_2) head(sao_paulo_activity_df_2)
Each row of this table contains a single trip of people in the São Paulo city.
sao_paulo_activity_sf_2
represents the movement of 2 people,
sao_paulo_activity_sf_20
represents the movement of 20 people.
sao_paulo_activity_sf_20
sao_paulo_activity_sf_20
A data frame with columns:
Person identifier.
Departure time in seconds past midnight
Mode of travel in A/B Street terms
Purpose of travel in A/B Street terms
Geometry of the linestring representing the OD pair
See the code used to create this data, and the full open dataset with 128 variables, the file data-raw/sao-paulo-activity-data.R in the package's GitHub repo.
dim(sao_paulo_activity_sf_20) names(sao_paulo_activity_sf_20) head(sao_paulo_activity_sf_20) table(sao_paulo_activity_sf_20$mode) table(sao_paulo_activity_sf_20$purpose) dim(sao_paulo_activity_sf_2) names(sao_paulo_activity_sf_2) head(sao_paulo_activity_sf_2)
dim(sao_paulo_activity_sf_20) names(sao_paulo_activity_sf_20) head(sao_paulo_activity_sf_20) table(sao_paulo_activity_sf_20$mode) table(sao_paulo_activity_sf_20$purpose) dim(sao_paulo_activity_sf_2) names(sao_paulo_activity_sf_2) head(sao_paulo_activity_sf_2)