Skip to content

Commit

Permalink
Merge pull request #236 from Robinlovelace/master
Browse files Browse the repository at this point in the history
Beginnings of 0.2.2 release prep
  • Loading branch information
richardellison committed Dec 1, 2017
2 parents c4845c9 + c40791f commit 96d1052
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: stplanr
Type: Package
Title: Sustainable Transport Planning
Version: 0.2.1
Version: 0.2.1.1
Authors@R: c(
person("Robin", "Lovelace", email = "rob00x@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-5679-6536")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# stplanr 0.2.2

* In this release **sp** is demoted from a Depends to an Imports, meaning that all its functions will not be attached to your namespace (it will not be loaded) when you run `library(stplanr)`, making it less tied to **sp**. This is a continuation of the work to support **sf** and will make it easier for the package to work with alternative representations of geographic data.

# stplanr 0.2.0

## NEW FEATURES
Expand Down
57 changes: 28 additions & 29 deletions R/aggregate_funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,39 @@
#' only the first column is retained. These columns are renamed with a prefix
#' of "o_" and "d_".
#' @param FUN Function to use on aggregation. Default is sum.
#' @param prop_by_area Boolean value indicating if the values should be
#' proportionally adjusted based on area. Default is TRUE unless FUN = mean.
#' @param digits The number of digits to use when proportionally adjusting
#' values based on area. Default is the value of getOption("digits").
#'
#' @inheritParams sp_aggregate
#' @return data.frame containing the aggregated od flows.
#'
#' @export
#' @examples
#' data(flow)
#' data(zones)
#' zones@data$region <- NULL
#' zones$quadrant = c(1, 2, 1, 4, 5, 6, 7, 1)
#' library(sp)
#' 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)
#' colSums(od_agg[3:9]) == colSums(flow[3:9])
#' od_sf_agg <- od2line(od_agg, aggzones_sf)
#' plot(flowlines, lwd = flowlines$Bicycle)
#' plot(od_sf_agg$geometry, lwd = od_sf_agg$Bicycle, add = TRUE, col = "red")
od_aggregate <- function(flow, zones, aggzones,
aggzone_points = NULL, cols = FALSE, aggcols = FALSE,
aggzone_points = NULL,
cols = FALSE,
aggcols = FALSE,
FUN = sum,
prop_by_area = ifelse(identical(FUN, mean) == FALSE, TRUE, FALSE),
digits = getOption("digits")){
digits = getOption("digits")) {
UseMethod("od_aggregate", zones)
}
#' @export
od_aggregate.sf <- function(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")){
aggzone_points = NULL,
cols = FALSE,
aggcols = FALSE,
FUN = sum,
prop_by_area = ifelse(identical(FUN, mean) == FALSE, TRUE, FALSE),
digits = getOption("digits")) {

flow_first_col <- colnames(flow)[1]
flow_second_col <- colnames(flow)[2]
Expand All @@ -80,26 +79,24 @@ od_aggregate.sf <- function(flow, zones, aggzones,
aggzone_points <- sf::st_centroid(aggzones)
}

aggflow_lines <- points2line(aggzone_points)

zones_agg <- zone_points %>%
sf::st_join(y = aggzones[aggcols]) %>%
sf::st_set_geometry(NULL)

names(zones_agg)[1] <- flow_first_col
zones_agg$new_orig = zones_agg[, aggcols[1]]
zones_agg$new_dest = zones_agg[, aggcols[1]]

flow$flow_new_orig <- flow[1] %>%
dplyr::inner_join(y = zones_agg[c(flow_first_col, aggcols)]) %>%
dplyr::pull(aggcols)
flow_new_orig <- flow %>%
dplyr::inner_join(y = zones_agg[c(flow_first_col, "new_orig")])

names(zones_agg)[1] <- flow_second_col

flow$flow_new_dest <- flow[2] %>%
dplyr::inner_join(y = zones_agg[c(flow_second_col, aggcols)]) %>%
dplyr::pull(aggcols)
flow_new_dest <- flow_new_orig %>%
dplyr::inner_join(y = zones_agg[c(flow_second_col, "new_dest")])

flow_ag <- flow %>%
dplyr::group_by(.data$flow_new_orig, .data$flow_new_dest) %>%
flow_ag <- flow_new_dest %>%
dplyr::group_by(!!rlang::sym("new_orig"), !!rlang::sym("new_dest")) %>%
dplyr::summarise_at(.vars = cols, .funs = sum) %>%
dplyr::ungroup()

Expand All @@ -110,10 +107,12 @@ od_aggregate.sf <- function(flow, zones, aggzones,
}
#' @export
od_aggregate.Spatial <- function(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")){
aggzone_points = NULL,
cols = FALSE,
aggcols = FALSE,
FUN = sum,
prop_by_area = ifelse(identical(FUN, mean) == FALSE, TRUE, FALSE),
digits = getOption("digits")) {
zonesfirstcol <- colnames(zones@data)[1]
aggzonesfirstcol <- colnames(aggzones@data)[1]

Expand Down
7 changes: 3 additions & 4 deletions man/od_aggregate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 96d1052

Please sign in to comment.