
Last chance! 50% off unlimited learning
Sale ends in
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.
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 = TRUE)od2line2(flow, zones)
A SpatialPolygonsDataFrame or SpatialPointsDataFrame representing origins (and destinations if no separate destinations object is provided) of travel flows.
A SpatialPolygonsDataFrame or SpatialPointsDataFrame representing destinations of travel flows.
Name of the variable in zones
containing the ids of the zone.
By default this is the first column names in the zones.
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.
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.
Name of the variable in destinations
containing the ids of the zone.
By default this is the first column names in the destinations.
TRUE by default, setting it to TRUE will show you the matching columns
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).
# NOT RUN {
data(flow) # load example data - see ?flow for mor information
data(cents)
newflowlines <- od2line(flow = flow, zones = cents)
newflowlines2 <- od2line2(flow = flow, zones = cents)
plot(cents)
lines(newflowlines, lwd = 3)
lines(newflowlines2, col = "white")
nfl_sldf <- sp::SpatialLinesDataFrame(newflowlines, flow, match.ID = FALSE)
identical(nfl_sldf, newflowlines)
# When destinations are different
data(destinations)
head(flow_dests[1:5]) # check data
head(destinations@data[1:5])
flowlines_dests = od2line(flow_dests, cents, destinations = destinations, silent = FALSE)
plot(flowlines_dests)
nfl_sf <- od2line(flow, zones_sf)
# }
Run the code above in your browser using DataLab