Aggregate OD data between polygon geometries
od_aggregate(flow, zones, aggzones, aggzone_points = NULL, cols = FALSE, aggcols = FALSE, FUN = sum, prop_by_area = ifelse(identical(FUN, mean) == FALSE, TRUE, FALSE), digits = getOption("digits"))
| 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 |
|---|---|
| zones | A SpatialPolygonsDataFrame or SpatialPointsDataFrame representing the original centroids or boundaries of the travel flow data. Note that in the case of a SpatialPointsDataFrame, the entirety of the flow will be allocated to the polygon in which the point is located rather than being distributed by area. |
| aggzones | A SpatialPolygonsDataFrame containing the new boundaries to aggregate to. |
| aggzone_points | Points representing origins of OD flows (typically population-weighted centroids) |
| cols | A character vector containing the names of columns on which to apply FUN. By default, all numeric columns are aggregated. |
| aggcols | A character vector containing the names of columns in aggzones to retain in the aggregated data.frame. By default, only the first column is retained. These columns are renamed with a prefix of "o_" and "d_". |
| FUN | Function to use on aggregation. Default is sum. |
| prop_by_area | Boolean value indicating if the values should be proportionally adjusted based on area. Default is TRUE unless FUN = mean. |
| digits | The number of digits to use when proportionally adjusting values based on area. Default is the value of getOption("digits"). |
data.frame containing the aggregated od flows.
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 function aggregates OD flows between polygon geometries allocating the original flows to larger zones based on area.
zones$quadrant <- c(1, 2, 1, 4, 5, 6, 7, 1) aggzones <- rgeos::gUnaryUnion(zones, id = zones@data$quadrant) aggzones <- sp::SpatialPolygonsDataFrame(aggzones, data.frame(region = c(1:6)), match.ID = FALSE) sp::proj4string(aggzones) <- sp::proj4string(zones) aggzones_sf <- sf::st_as_sf(aggzones) aggzones_sf <- sf::st_set_crs(aggzones_sf, sf::st_crs(zones_sf)) od_agg <- od_aggregate(flow, zones_sf, aggzones_sf)#> Warning: st_centroid assumes attributes are constant over geometries of x#> Warning: st_centroid does not give correct centroids for longitude/latitude data#> Warning: st_centroid assumes attributes are constant over geometries of x#> Warning: st_centroid does not give correct centroids for longitude/latitude data#>#>#> Warning: Column `Area.of.residence` joining character vector and factor, coercing into character vector#>#> Warning: Column `Area.of.workplace` joining character vector and factor, coercing into character vectorcolSums(od_agg[3:9]) == colSums(flow[3:9])#> All Work.mainly.at.or.from.home #> TRUE TRUE #> Underground..metro..light.rail..tram Train #> TRUE TRUE #> Bus..minibus.or.coach Taxi #> TRUE TRUE #> Motorcycle..scooter.or.moped #> TRUE#> Warning: st_centroid assumes attributes are constant over geometries of x#> Warning: st_centroid does not give correct centroids for longitude/latitude dataplot(flowlines, lwd = flowlines$Bicycle)plot(od_sf_agg$geometry, lwd = od_sf_agg$Bicycle, add = TRUE, col = "red")