Skip to contents

Origin-destination ('OD') flow data is often provided in the form of 1 line per flow with zone codes of origin and destination centroids. This can be tricky to plot and link-up with geographical data. This function makes the task easier.

Usage

od2line(
  flow,
  zones,
  destinations = NULL,
  zone_code = names(zones)[1],
  origin_code = names(flow)[1],
  dest_code = names(flow)[2],
  zone_code_d = NA,
  silent = FALSE
)

od2line2(flow, zones)

Arguments

flow

A data frame representing origin-destination data. The first two columns of this data frame should correspond to the first column of the data in the zones. Thus in cents(), the first column is geo_code. This corresponds to the first two columns of flow().

zones

A spatial object representing origins (and destinations if no separate destinations object is provided) of travel.

destinations

A spatial object representing destinations of travel flows.

zone_code

Name of the variable in zones containing the ids of the zone. By default this is the first column names in the zones.

origin_code

Name of the variable in flow containing the ids of the zone of origin. By default this is the first column name in the flow input dataset.

dest_code

Name of the variable in flow containing the ids of the zone of destination. By default this is the second column name in the flow input dataset or the first column name in the destinations if that is set.

zone_code_d

Name of the variable in destinations containing the ids of the zone. By default this is the first column names in the destinations.

silent

TRUE by default, setting it to TRUE will show you the matching columns

Details

Origin-destination (OD) data is often provided in the form of 1 line per OD pair, with zone codes of the trip origin in the first column and the zone codes of the destination in the second column (see the vignette("stplanr-od")) for details. od2line() creates a spatial (linestring) object representing movement from the origin to the destination for each OD pair. It takes data frame containing origin and destination cones (flow) that match the first column in a a spatial (polygon or point) object (zones).

Examples

od_data <- stplanr::flow[1:20, ]
l <- od2line(flow = od_data, zones = cents_sf)
plot(sf::st_geometry(cents_sf))
plot(l, lwd = l$All / mean(l$All), add = TRUE)
#> Warning: ignoring all but the first attribute

l <- od2line(flow = od_data, zones = cents)
#> Warning: CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html
# When destinations are different
head(destinations[1:5])
#>                coordinates    WZ11CD   LAD11CD COWZEW_SG
#> 524  (-1.492713, 53.81042) E33012352 E08000035         3
#> 1936  (-1.53781, 53.80131) E33010351 E08000035         2
#> 2006 (-1.533468, 53.80576) E33012331 E08000035         5
#> 2712 (-1.540438, 53.81724) E33012280 E08000035         3
#> 2730 (-1.540901, 53.80167) E33010390 E08000035         7
#> 2799 (-1.521311, 53.81683) E33012310 E08000035         3
#>                          COWZEW_SGN COWZEW_G
#> 524                   Metro suburbs       3d
#> 1936                       Top jobs       2c
#> 2006 Manufacturing and distribution       5b
#> 2712                  Metro suburbs       3a
#> 2730            Servants of society       7c
#> 2799                  Metro suburbs       3d
od_data2 <- flow_dests[1:12, 1:3]
od_data2
#>        Area.of.residence    WZ11CD All
#> 920573         E02002361 E33010350 109
#> 920575         E02002361 E33010390  38
#> 920578         E02002361 E33010468  10
#> 920582         E02002361 E33012367  44
#> 920587         E02002361 E33012225  34
#> 920591         E02002361 E33012368   7
#> 920601         E02002361 E33012333  94
#> 921220         E02002363 E33012414  30
#> 921222         E02002363 E33012313 183
#> 921225         E02002363 E33010464  11
#> 921228         E02002363 E33012254  72
#> 921233         E02002363 E33012352  13
flowlines_dests <- od2line(od_data2, cents_sf, destinations = destinations_sf)
flowlines_dests
#> Simple feature collection with 12 features and 3 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -1.563202 ymin: 53.79647 xmax: -1.492713 ymax: 53.83587
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    Area.of.residence    WZ11CD All                       geometry
#> 1          E02002361 E33010350 109 LINESTRING (-1.516734 53.82...
#> 2          E02002361 E33010390  38 LINESTRING (-1.516734 53.82...
#> 3          E02002361 E33010468  10 LINESTRING (-1.516734 53.82...
#> 4          E02002361 E33012367  44 LINESTRING (-1.516734 53.82...
#> 5          E02002361 E33012225  34 LINESTRING (-1.516734 53.82...
#> 6          E02002361 E33012368   7 LINESTRING (-1.516734 53.82...
#> 7          E02002361 E33012333  94 LINESTRING (-1.516734 53.82...
#> 8          E02002363 E33012414  30 LINESTRING (-1.535617 53.82...
#> 9          E02002363 E33012313 183 LINESTRING (-1.535617 53.82...
#> 10         E02002363 E33010464  11 LINESTRING (-1.535617 53.82...
plot(flowlines_dests)