Learn R Programming

stplanr (version 0.2.8)

od2line: Convert flow data to SpatialLinesDataFrame

Description

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 the flow between two points or zones. 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 SpatialPolygonsDataFrame or SpatialPointsDataFrame 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

The function expects zone codes to be in the 1st column of the zones/destinations datasets and the 1st and 2nd columns of the flow data, respectively.

od2line2() is a faster implementation (around 6 times faster on large datasets) that returns a SpatialLines object, omitting the data and working only when there is no destinations dataset (i.e. when the geography of origins is the same as that of destinations).

See Also

Other od: dist_google, od2odf, od_aggregate, od_coords2line, od_coords, od_dist, od_radiation, points2flow, points2odf, sp_aggregate

Examples

Run this code
# NOT RUN {
l <- od2line(flow = flow, zones = cents)
plot(cents)
plot(l, lwd = l$All / mean(l$All), add = TRUE)
# When destinations are different
head(flow_dests[1:5]) # check data
head(destinations[1:5])
flowlines_dests <- od2line(flow_dests, cents, destinations = destinations)
plot(flowlines_dests)
l <- od2line(flow, zones_sf)
plot(l["All"], lwd = l$All/mean(l$All))
# }

Run the code above in your browser using DataLab