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",
  ...
)

Arguments

desire_lines

OD data represented as geographic lines created by ab_scenario().

mode_column

The column name in the desire lines data that contains the mode of transport. "mode_baseline" by default.

time_fun

The function used to calculate departure times. ab_time_normal() by default.

scenario_name

The name of the scenario to appear in A/B Street. The default value is "test", which generates a message to tell you to think of a more imaginative scenario name!

default_purpose

In case a purpose column is not present in the input, or there are missing values in the purpose column, this argument sets the default, fallback purpose, as "Work" by default, reflecting the prevalence of work-based data and thinking in transport models.

...

Additional arguments to pass to ab_json()

Value

A list that can be saved as a JSON file with ab_save()

Details

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.

Examples

# Starting with tabular data
od = leeds_od
od[[1]] = c("E02006876")
zones = leeds_zones
ablines = ab_scenario(od, zones = zones)
#> 0 origins with no match in zone ids
#> 0 destinations with no match in zone ids
#>  points not in od data removed.
#> Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
ab_list = ab_json(ablines, mode_column = "mode", scenario_name = "test")
#> Default scenario name of 'test' used.
ab_list$scenario
#> [1] "test"
f = tempfile(fileext = ".json")
ab_save(ab_list, f)
readLines(f)[1:30]
#>  [1] "{"                                    
#>  [2] "  \"scenario_name\": \"test\","       
#>  [3] "  \"people\": ["                      
#>  [4] "    {"                                
#>  [5] "      \"trips\": ["                   
#>  [6] "        {"                            
#>  [7] "          \"departure\": 305040000,"  
#>  [8] "          \"origin\": {"              
#>  [9] "            \"Position\": {"          
#> [10] "              \"longitude\": -1.5005,"
#> [11] "              \"latitude\": 53.7681"  
#> [12] "            }"                        
#> [13] "          },"                         
#> [14] "          \"destination\": {"         
#> [15] "            \"Position\": {"          
#> [16] "              \"longitude\": -1.5583,"
#> [17] "              \"latitude\": 53.8085"  
#> [18] "            }"                        
#> [19] "          },"                         
#> [20] "          \"mode\": \"Walk\","        
#> [21] "          \"purpose\": \"Work\""      
#> [22] "        }"                            
#> [23] "      ]"                              
#> [24] "    },"                               
#> [25] "    {"                                
#> [26] "      \"trips\": ["                   
#> [27] "        {"                            
#> [28] "          \"departure\": 294480000,"  
#> [29] "          \"origin\": {"              
#> [30] "            \"Position\": {"          

# 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
#> Simple feature collection with 4 features and 4 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -122.319 ymin: 47.63728 xmax: -122.3037 ymax: 47.64048
#> Geodetic CRS:  WGS 84
#>   person departure mode    purpose                       geometry
#> 1      1       0.1 Bike       Meal LINESTRING (-122.3037 47.63...
#> 2      1    1000.0 Walk Recreation LINESTRING (-122.3191 47.63...
#> 3      2       1.1 Walk       Meal LINESTRING (-122.3037 47.63...
#> 4      2    1001.0 Bike       Work LINESTRING (-122.3191 47.63...
json_list = ab_json(desire_lines)
#> Default scenario name of 'test' used.
json_list
#> $scenario_name
#> [1] "test"
#> 
#> $people
#> $people[[1]]
#> $people[[1]]$trips
#> $people[[1]]$trips[[1]]
#> $people[[1]]$trips[[1]]$departure
#> [1] 1000
#> 
#> $people[[1]]$trips[[1]]$origin
#> $people[[1]]$trips[[1]]$origin$Position
#> $people[[1]]$trips[[1]]$origin$Position$longitude
#> [1] -122.3037
#> 
#> $people[[1]]$trips[[1]]$origin$Position$latitude
#> [1] 47.63728
#> 
#> 
#> 
#> $people[[1]]$trips[[1]]$destination
#> $people[[1]]$trips[[1]]$destination$Position
#> $people[[1]]$trips[[1]]$destination$Position$longitude
#> [1] -122.3191
#> 
#> $people[[1]]$trips[[1]]$destination$Position$latitude
#> [1] 47.63786
#> 
#> 
#> 
#> $people[[1]]$trips[[1]]$mode
#> [1] "Bike"
#> 
#> $people[[1]]$trips[[1]]$purpose
#> [1] "Meal"
#> 
#> 
#> $people[[1]]$trips[[2]]
#> $people[[1]]$trips[[2]]$departure
#> [1] 1e+07
#> 
#> $people[[1]]$trips[[2]]$origin
#> $people[[1]]$trips[[2]]$origin$Position
#> $people[[1]]$trips[[2]]$origin$Position$longitude
#> [1] -122.3191
#> 
#> $people[[1]]$trips[[2]]$origin$Position$latitude
#> [1] 47.63786
#> 
#> 
#> 
#> $people[[1]]$trips[[2]]$destination
#> $people[[1]]$trips[[2]]$destination$Position
#> $people[[1]]$trips[[2]]$destination$Position$longitude
#> [1] -122.3076
#> 
#> $people[[1]]$trips[[2]]$destination$Position$latitude
#> [1] 47.63948
#> 
#> 
#> 
#> $people[[1]]$trips[[2]]$mode
#> [1] "Walk"
#> 
#> $people[[1]]$trips[[2]]$purpose
#> [1] "Recreation"
#> 
#> 
#> 
#> 
#> $people[[2]]
#> $people[[2]]$trips
#> $people[[2]]$trips[[1]]
#> $people[[2]]$trips[[1]]$departure
#> [1] 11000
#> 
#> $people[[2]]$trips[[1]]$origin
#> $people[[2]]$trips[[1]]$origin$Position
#> $people[[2]]$trips[[1]]$origin$Position$longitude
#> [1] -122.3037
#> 
#> $people[[2]]$trips[[1]]$origin$Position$latitude
#> [1] 47.63828
#> 
#> 
#> 
#> $people[[2]]$trips[[1]]$destination
#> $people[[2]]$trips[[1]]$destination$Position
#> $people[[2]]$trips[[1]]$destination$Position$longitude
#> [1] -122.3191
#> 
#> $people[[2]]$trips[[1]]$destination$Position$latitude
#> [1] 47.63886
#> 
#> 
#> 
#> $people[[2]]$trips[[1]]$mode
#> [1] "Walk"
#> 
#> $people[[2]]$trips[[1]]$purpose
#> [1] "Meal"
#> 
#> 
#> $people[[2]]$trips[[2]]
#> $people[[2]]$trips[[2]]$departure
#> [1] 10010000
#> 
#> $people[[2]]$trips[[2]]$origin
#> $people[[2]]$trips[[2]]$origin$Position
#> $people[[2]]$trips[[2]]$origin$Position$longitude
#> [1] -122.3191
#> 
#> $people[[2]]$trips[[2]]$origin$Position$latitude
#> [1] 47.63886
#> 
#> 
#> 
#> $people[[2]]$trips[[2]]$destination
#> $people[[2]]$trips[[2]]$destination$Position
#> $people[[2]]$trips[[2]]$destination$Position$longitude
#> [1] -122.3076
#> 
#> $people[[2]]$trips[[2]]$destination$Position$latitude
#> [1] 47.64048
#> 
#> 
#> 
#> $people[[2]]$trips[[2]]$mode
#> [1] "Bike"
#> 
#> $people[[2]]$trips[[2]]$purpose
#> [1] "Work"
#> 
#> 
#> 
#> 
#>