diff --git a/R/aggregate.R b/R/aggregate.R index 33c1431..90f6678 100644 --- a/R/aggregate.R +++ b/R/aggregate.R @@ -61,7 +61,7 @@ #' subzones = od_data_zones_small #' try(od_disaggregate(od, zones, subzones)) #' od_disag = od_disaggregate(od, zones, subzones, max_per_od = 500) -#' ncol(od_disag) -3 == ncol(od) # same number of columns, the same... +#' ncol(od_disag) - 3 == ncol(od) # same number of columns, the same... #' # Except disag data gained geometry and new agg ids: #' sum(od_disag[[3]]) == sum(od[[3]]) #' sum(od_disag[[4]]) == sum(od[[4]]) @@ -81,12 +81,11 @@ od_disaggregate = function(od, population_column = 3, max_per_od = 5, keep_ids = TRUE, - integer_outputs = FALSE - ) { + integer_outputs = FALSE) { od$nrows = od_nrows(od, population_column, max_per_od) azn = paste0(names(z)[1], code_append) # is the input od data an sf object? tell the user and convert to df if so - if(methods::is(object = od, class2 = "sf")) { + if (methods::is(object = od, class2 = "sf")) { message("Input object is sf, attempting to convert to a data frame") od = sf::st_drop_geometry(od) } @@ -134,7 +133,7 @@ od_disaggregate = function(od, max_n_od = ceiling(od[[population_column]][i] / max_per_od) o_options = subpoints[[1]][subpoints[[azn]] == od[[1]][i]] d_options = subpoints[[1]][subpoints[[azn]] == od[[2]][i]] - if(max_n_od > length(o_options) || max_n_od > length(d_options)) { + if (max_n_od > length(o_options) || max_n_od > length(d_options)) { warning("Insufficient subzones/points to prevent duplicate desire lines") message("Sampling may fail. Try again with larger max_per_od") } @@ -145,17 +144,17 @@ od_disaggregate = function(od, odn_list = lapply(od[i, -c(1, 2)], function(x) x / nrow(od_new)) odns = as.data.frame(odn_list)[rep(1, nrow(od_new)), , drop = FALSE] names(odns) = numeric_col_names - if(integer_outputs) { + if (integer_outputs) { odns[] = apply(odns, 2, function(x) smart.round(x)) } od_new = cbind(od_new, odns) - if(keep_ids) { + if (keep_ids) { od_new$o_agg = od[[1]][i] od_new$d_agg = od[[2]][i] } od_new_sf = od::od_to_sf(od_new, subpoints, silent = TRUE) # Remove sampled points from 'universe' of available points - if(i < nrow(od)) { + if (i < nrow(od)) { subpoints <<- subpoints[!subpoints[[1]] %in% c(o, d), ] } od_new_sf @@ -225,7 +224,7 @@ od_split = od_disaggregate smart.round = function(x) { y = floor(x) - indices = utils::tail(order(x-y), round(sum(x)) - sum(y)) + indices = utils::tail(order(x - y), round(sum(x)) - sum(y)) y[indices] = y[indices] + 1 y } @@ -240,7 +239,7 @@ smart.round = function(x) { #' \dontrun{ #' u = "https://github.com/ITSLeeds/od/releases/download/v0.3.1/road_network_min.Rds" #' f = basename(u) -#' if(!file.exists(f)) download.file(u, f) +#' if (!file.exists(f)) download.file(u, f) #' road_network_min = readRDS(f) #' od_sample_vertices(road_network_min) #' } @@ -292,7 +291,7 @@ od_sample_points = function(subpoints, subdf, z, per_zone, azn = "azn") { subpoints_joined = sf::st_join(sf::st_sf(subpoints), z[1]) sel_list = lapply(1:nrow(per_zone), function(i) { which_points = which(subpoints_joined[[1]] == per_zone[[1]][i]) - if(length(which_points) == 0) { + if (length(which_points) == 0) { return(NULL) } sample(which_points, size = per_zone[[2]][i]) diff --git a/R/jitter.R b/R/jitter.R index 1fbddcb..8f10267 100644 --- a/R/jitter.R +++ b/R/jitter.R @@ -67,30 +67,30 @@ #' # plot(dlr3[od$all > 200, 1]) #' # mapview::mapview(od_sf$geometry[od$all > 200]) od_jitter = function( - od, - z, - subpoints = NULL, - code_append = "_ag", - population_column = 3, - max_per_od = 100000, - keep_ids = TRUE, - integer_outputs = FALSE, - # od_jitter-specific arguments (and zd) - zd = NULL, - subpoints_o = NULL, - subpoints_d = NULL, - disag = FALSE - ) { - + od, + z, + subpoints = NULL, + code_append = "_ag", + population_column = 3, + max_per_od = 100000, + keep_ids = TRUE, + integer_outputs = FALSE, + # od_jitter-specific arguments (and zd) + zd = NULL, + subpoints_o = NULL, + subpoints_d = NULL, + disag = FALSE) { if (!methods::is(od, "sf")) { # the data structure to reproduce for matching OD pairs od = od::od_to_sf(od, z = z, zd = zd) } disag = all(is.null(zd), is.null(subpoints_o), is.null(subpoints_d), disag) - if(disag) { + if (disag) { message("Using od_disaggregate") # todo remove once tested - return(od_disaggregate(od, z, subpoints, code_append, population_column, - max_per_od, keep_ids, integer_outputs)) + return(od_disaggregate( + od, z, subpoints, code_append, population_column, + max_per_od, keep_ids, integer_outputs + )) } odc_new = odc_original = od::od_coordinates(od) od = sf::st_drop_geometry(od) @@ -101,7 +101,7 @@ od_jitter = function( names(points_per_zone)[1] = names(z)[1] points_per_zone_joined = merge(sf::st_drop_geometry(z), points_per_zone) # unique_zone_codes = points_per_zone_joined[[1]] - zo = z[match(points_per_zone[[1]], z[[1]], nomatch = FALSE) ,] + zo = z[match(points_per_zone[[1]], z[[1]], nomatch = FALSE), ] # browser() if (is.null(subpoints_o)) { subpoints_o = sf::st_sample(zo, size = points_per_zone_joined$Freq) @@ -118,10 +118,11 @@ od_jitter = function( for (i in unique_zones) { # total number of origins and destinations n_origins = sum(od[[1]] == i) - if (n_origins == 0) + if (n_origins == 0) { next() + } sel_sj = which(sj_df$geo_code == i) - if(n_origins > length(sel_sj)) { + if (n_origins > length(sel_sj)) { sel_sj_o = sel_sj[sample(length(sel_sj), size = n_origins, replace = TRUE)] } else { sel_sj_o = sel_sj[sample(length(sel_sj), size = n_origins)] @@ -129,7 +130,7 @@ od_jitter = function( odc_new[od[[1]] == i, "ox"] = sj_df$x[sel_sj_o] odc_new[od[[1]] == i, "oy"] = sj_df$y[sel_sj_o] # remove those random points from the list of options - sj_df = sj_df[-sel_sj_o,] + sj_df = sj_df[-sel_sj_o, ] } if (is.null(zd)) { @@ -140,7 +141,7 @@ od_jitter = function( id_destinations = od[[2]] points_per_zone = data.frame(table(id_destinations)) - zd = zd[match(points_per_zone[[1]], zd[[1]], nomatch = FALSE) ,] + zd = zd[match(points_per_zone[[1]], zd[[1]], nomatch = FALSE), ] if (is.null(subpoints_d)) { names(points_per_zone)[1] = names(zd)[1] points_per_zone_joined_d = merge(sf::st_drop_geometry(zd), points_per_zone) @@ -159,11 +160,12 @@ od_jitter = function( i = unique_zones_d[1] for (i in unique_zones_d) { n_destinations = sum(od[[2]] == i) - if (n_destinations == 0) + if (n_destinations == 0) { next() + } # when there are subpoints sel_sj = which(sj_df_d$geo_code == i) - if(n_destinations > length(sel_sj)) { + if (n_destinations > length(sel_sj)) { sel_sj_d = sel_sj[sample(length(sel_sj), size = n_destinations, replace = TRUE)] } else { sel_sj_d = sel_sj[sample(length(sel_sj), size = n_destinations)] @@ -171,7 +173,7 @@ od_jitter = function( odc_new[od[[2]] == i, "dx"] = sj_df_d$x[sel_sj_d] odc_new[od[[2]] == i, "dy"] = sj_df_d$y[sel_sj_d] # remove those random points from the list of options - sj_df = sj_df[-sel_sj_d,] + sj_df = sj_df[-sel_sj_d, ] } sf::st_sf(od, geometry = odc_to_sfc_sf(odc_new, crs = sf::st_crs(z))) } diff --git a/R/network.R b/R/network.R index da74337..a17a58a 100644 --- a/R/network.R +++ b/R/network.R @@ -37,20 +37,24 @@ od_to_network = function(x, z, zd = NULL, silent = TRUE, package = "sf", crs = 4 # g[sample(nrow(g), size = uoid[i]), ] # }) # i = 1 - l_origin = lapply(seq(nrow(x)), - function(i) { - g = net_o[net_o[[z_nm]] == x[[1]][i], ] - g[sample(nrow(g), size = 1), ] - }) + l_origin = lapply( + seq(nrow(x)), + function(i) { + g = net_o[net_o[[z_nm]] == x[[1]][i], ] + g[sample(nrow(g), size = 1), ] + } + ) d_origin = do.call(rbind, l_origin) # d_origin$geo_code == x[[1]] TRUE odc_origin = sf::st_coordinates(d_origin) - l_destination = lapply(seq(nrow(x)), - function(i) { - g = net_d[net_d[[z_nm]] == x[[2]][i], ] - g[sample(nrow(g), size = 1), ] - }) + l_destination = lapply( + seq(nrow(x)), + function(i) { + g = net_d[net_d[[z_nm]] == x[[2]][i], ] + g[sample(nrow(g), size = 1), ] + } + ) d_destination = do.call(rbind, l_destination) odc_destination = sf::st_coordinates(d_destination) diff --git a/R/od-funs.R b/R/od-funs.R index 0da58f0..d521096 100644 --- a/R/od-funs.R +++ b/R/od-funs.R @@ -35,14 +35,13 @@ #' plot(desire_lines_d$geometry[n], lwd = 3, add = TRUE) od_to_sf = function(x, z, zd = NULL, odc = NULL, silent = FALSE, filter = TRUE, package = "sfheaders", crs = 4326) { - - if(!is.null(odc)) { + if (!is.null(odc)) { return(odc_to_sf(odc = odc, crs = crs)) } if (filter && is.null(zd)) { x = od_filter(x, codes = z[[1]], silent = silent) } - if(filter && !is.null(zd)) { + if (filter && !is.null(zd)) { x = od_filter(x, codes = c(z[[1]], zd[[1]])) } od_sfc = od_to_sfc(x, z, zd, silent, package, crs, filter) @@ -57,11 +56,11 @@ od_to_sfc = function(x, package = "sfheaders", crs = 4326, filter = TRUE) { - if(package == "sfheaders") { + if (package == "sfheaders") { odc = od_coordinates(x, z, zd, silent = silent) # todo: add support for p od_sfc = odc_to_sfc(odc) - if(requireNamespace("sf", quietly = TRUE)) { - if(!is.na(sf::st_crs(z))) { + if (requireNamespace("sf", quietly = TRUE)) { + if (!is.na(sf::st_crs(z))) { crs = sf::st_crs(z) } sf::st_crs(od_sfc) = sf::st_crs(crs) @@ -82,7 +81,7 @@ od_to_sfc = function(x, #' matches them with objects representing origins and destinations #' in wide range of input data types (spatial lines, points or text strings). #' It returns a data frame of coordinates representing movement between all origin (ox, oy) and destination (dx, dy) points. -#' +#' #' See [points_to_od()] for a function that creates #' an 'od data frame' from a set (or two sets) of points. #' @param p Points representing origins and destinations @@ -105,29 +104,29 @@ od_to_sfc = function(x, #' pd = od_data_destinations #' od_coordinates(x, p, pd) od_coordinates = function(x, p = NULL, pd = NULL, silent = TRUE, sfnames = FALSE) { - if(methods::is(x, "sf")) { + if (methods::is(x, "sf")) { return(od_coordinates_sf(x)) } o_code = x[[1]] d_code = x[[2]] - if(methods::is(o_code, "factor")) { + if (methods::is(o_code, "factor")) { message("Converting origin ID from factor to character") o_code = as.character(o_code) } - if(methods::is(d_code, "factor")) { + if (methods::is(d_code, "factor")) { message("Converting destination ID from factor to character") d_code = as.character(d_code) } p_code_original = p[[1]] - if(methods::is(p_code_original, "factor")) { + if (methods::is(p_code_original, "factor")) { message("Converting geometry ID from factor to character") p_code_original = as.character(p_code_original) } od_code = unique(c(o_code, d_code)) sel_p_in_x = p_code_original %in% od_code geometry_contains_polygons = geometry_contains_polygons(p) - if(geometry_contains_polygons) { - if(requireNamespace("sf", quietly = TRUE)) { + if (geometry_contains_polygons) { + if (requireNamespace("sf", quietly = TRUE)) { suppressWarnings({ p_in_x = sf::st_centroid(sf::st_geometry(p)[sel_p_in_x]) }) @@ -137,16 +136,16 @@ od_coordinates = function(x, p = NULL, pd = NULL, silent = TRUE, sfnames = FALSE } else { p_in_x = sf::st_geometry(p)[sel_p_in_x] } - if(!silent) message(nrow(p) - nrow(p_in_x), " points not in od data removed.") + if (!silent) message(nrow(p) - nrow(p_in_x), " points not in od data removed.") p_code = p_code_original[sel_p_in_x] stopifnot(all(o_code %in% p_code)) # todo: add error message - if(is.null(pd)) { + if (is.null(pd)) { stopifnot(all(d_code %in% p_code)) # todo: add error message } else { stopifnot(all(d_code %in% pd[[1]])) # todo: add error message } o_matching_p = match(o_code, p_code) - if(is.null(pd)) { + if (is.null(pd)) { d_matching_p = match(d_code, p_code) } else { pcode_d = pd[[1]] @@ -156,11 +155,13 @@ od_coordinates = function(x, p = NULL, pd = NULL, silent = TRUE, sfnames = FALSE } p_coordinates = sfheaders::sfc_to_df(p_in_x)[c("x", "y")] o_coords = p_coordinates[o_matching_p, ] - if(is.null(pd)) { + if (is.null(pd)) { d_coords = p_coordinates[d_matching_p, ] } odc = cbind(o_coords, d_coords) - if(sfnames) return(as.matrix(odc)) # return without updating column names + if (sfnames) { + return(as.matrix(odc)) + } # return without updating column names colnames(odc) = c("ox", "oy", "dx", "dy") odc } @@ -184,7 +185,7 @@ od_coordinates_sf = function(x) { odc_to_sf = function(odc, d = NULL, crs = 4326) { odc_id = od_coordinates_ids(odc) odc_sfc = sfheaders::sfc_linestring(obj = odc_id, x = "x", y = "y", linestring_id = "id") - if(is.null(d)) { + if (is.null(d)) { return(sf::st_sf(geometry = odc_sfc, crs = crs)) } sf::st_sf(d, geometry = odc_sfc, crs = crs) @@ -204,11 +205,11 @@ odc_to_sfc = function(odc) { odc_to_sfc_sf = function(odc, crs = 4326) { linestring_list = lapply(seq(nrow(odc)), function(i) { sf::st_linestring(rbind(odc[i, 1:2], odc[i, 3:4])) - }) + }) sf::st_sfc(linestring_list, crs = crs) } #' Interleave origin and destination coordinates -#' +#' #' This function takes a matrix with 4 columns representing origin and destination coordinates #' and returns a data frame with 3 columns with the ID of each linestring, plus #' the coordinates representing origin and destination coordinates. @@ -220,7 +221,7 @@ odc_to_sfc_sf = function(odc, crs = 4326) { #' od_coordinates_ids(od_coordinates(od_data_df, p = od_data_zones, sfnames = TRUE)) od_coordinates_ids = function(odc) { # If odc has 1 row, do the interleaving manually: - if(nrow(odc) == 1) { + if (nrow(odc) == 1) { # rbind without checking column names, to prevent 'names do not match previous names' error: colnames(odc) = c("x", "y", "x", "y") res = rbind(odc[, 1:2], odc[, 3:4]) @@ -228,7 +229,7 @@ od_coordinates_ids = function(odc) { return(res) } # Convert odc to matrix if not one already - if(!is.matrix(odc)) { + if (!is.matrix(odc)) { odc = as.matrix(odc) } res = vctrs::vec_interleave(odc[, 1:2], odc[, 3:4]) @@ -297,7 +298,7 @@ odmatrix_to_od = function(odmatrix) { geometry_contains_polygons = function(z) { # The sf way: - if(!requireNamespace("sf", quietly = TRUE)) { + if (!requireNamespace("sf", quietly = TRUE)) { # stop("sf package required, to install it see https://github.com/r-spatial/sf#installing") # without sf: res = grepl(pattern = "POLY", class(z$geometry)[1]) @@ -336,7 +337,7 @@ geometry_contains_polygons = function(z) { od_filter = function(x, codes, silent = FALSE) { sel_o_in_codes = x[[1]] %in% codes sel_d_in_codes = x[[2]] %in% codes - if(!silent) { + if (!silent) { message(sum(!sel_o_in_codes), " origins with no match in zone ids") message(sum(!sel_d_in_codes), " destinations with no match in zone ids") } diff --git a/R/oneway.R b/R/oneway.R index fad3664..85b341f 100644 --- a/R/oneway.R +++ b/R/oneway.R @@ -44,8 +44,7 @@ od_oneway = function(x, ..., id1 = names(x)[1], id2 = names(x)[2], - oneway_key = NULL - ) { + oneway_key = NULL) { # is_sf = is(x, "sf") # only make it work with dfs for now if (is.null(oneway_key)) { diff --git a/R/points_to_od.R b/R/points_to_od.R index 55b2499..db830ff 100644 --- a/R/points_to_od.R +++ b/R/points_to_od.R @@ -55,57 +55,60 @@ points_to_od = function(p, pd = NULL, interzone_only = FALSE, ids_only = FALSE, #' @export points_to_od.sf = function(p, pd = NULL, interzone_only = FALSE, ids_only = FALSE, max_dist = Inf, max_dest = Inf) { - single_geometry = is.null(pd) - if(any(duplicated(p[[1]]))) { + if (any(duplicated(p[[1]]))) { warning("Duplicated ids found in first column of origins") } - if(any(sf::st_geometry_type(p) != "POINT")){ + if (any(sf::st_geometry_type(p) != "POINT")) { message("Converting p to centroids") suppressWarnings(p <- sf::st_centroid(p)) } - if(!single_geometry){ - if(any(duplicated(pd[[1]]))) { + if (!single_geometry) { + if (any(duplicated(pd[[1]]))) { warning("Duplicated ids found in first column of destinations") } - if(any(sf::st_geometry_type(p) != "POINT")){ + if (any(sf::st_geometry_type(p) != "POINT")) { message("Converting pd to centroids") suppressWarnings(p <- sf::st_centroid(p)) } } - if(single_geometry) { + if (single_geometry) { pd = p } # Use nngeo implementation if max_dist or max_dest is provided - if(max_dist < Inf || max_dest < Inf) { + if (max_dist < Inf || max_dest < Inf) { # Fail gracefully if nngeo is not available: - if(!requireNamespace("nngeo", quietly = TRUE)) { + if (!requireNamespace("nngeo", quietly = TRUE)) { stop("nngeo must be installed for max_dist and max_dest arguments") } - if(max_dest > nrow(pd)){ + if (max_dest > nrow(pd)) { max_dest = nrow(pd) } - - nn <- nngeo::st_nn(p, pd, k = max_dest, maxdist = max_dist, returnDist = FALSE, - progress = FALSE) - odf = data.frame(O = rep(p[[1]], lengths(nn)), - D = pd[[1]][unlist(nn, use.names = FALSE)]) + + nn = nngeo::st_nn(p, pd, + k = max_dest, maxdist = max_dist, returnDist = FALSE, + progress = FALSE + ) + odf = data.frame( + O = rep(p[[1]], lengths(nn)), + D = pd[[1]][unlist(nn, use.names = FALSE)] + ) } else { odf = data.frame(expand.grid(p[[1]], pd[[1]], stringsAsFactors = FALSE)) } - if(interzone_only) { + if (interzone_only) { odf = od_interzone(odf) } - if(ids_only) { + if (ids_only) { return(odf) } - if(single_geometry) { + if (single_geometry) { odc = od_coordinates(odf, p) } else { odc = od_coordinates(odf, p, pd = pd) @@ -113,7 +116,7 @@ points_to_od.sf = function(p, pd = NULL, interzone_only = FALSE, ids_only = FALS cbind(odf, odc) } #' @export -points_to_od.matrix = function(p, pd = NULL, interzone_only = FALSE, ids_only = FALSE, max_dist = NULL, max_dest = NULL) { +points_to_od.matrix = function(p, pd = NULL, interzone_only = FALSE, ids_only = FALSE, max_dist = NULL, max_dest = NULL) { coords_to_od(p, interzone_only = interzone_only, ids_only = ids_only) } #' @rdname points_to_od @@ -146,11 +149,11 @@ points_to_odl = function(p, pd = NULL, crs = 4326, ...) { coords_to_od = function(p, interzone_only = FALSE, ids_only = FALSE) { id = seq(nrow(p)) odf = data.frame(expand.grid(id, id, stringsAsFactors = FALSE)[2:1]) - if(interzone_only) { + if (interzone_only) { odf = od_interzone(odf) } names(odf) = c("O", "D") - if(ids_only) { + if (ids_only) { return(odf) } coords_o = p[odf$O, ] diff --git a/data-raw/ad-hoc-tests.R b/data-raw/ad-hoc-tests.R index 0ec0385..effe3d0 100644 --- a/data-raw/ad-hoc-tests.R +++ b/data-raw/ad-hoc-tests.R @@ -6,7 +6,7 @@ x = od_data_df p = od_data_centroids -x[[1]][1] = "404" +x[[1]][1] = "404" # Next line will error: od_coordinates(x, p, silent = FALSE)[1:2, ] # From original stplanr function: @@ -27,10 +27,9 @@ network = od_data_network library(tmap) tmap_mode("view") tm_shape(lines_to_points_on_network) + tm_lines(lwd = 5) + - tm_shape(lines_to_points) + tm_lines(col = "grey", lwd = 5) + - tm_shape(od_data_zones_min) + tm_borders() + - qtm(od_data_network, lines.col = "yellow") + tm_shape(lines_to_points) + tm_lines(col = "grey", lwd = 5) + + tm_shape(od_data_zones_min) + tm_borders() + + qtm(od_data_network, lines.col = "yellow") plot(sf::st_geometry(lines_to_points_on_network)) plot(lines_to_points, col = "grey", add = TRUE) plot(sf::st_geometry(z), add = TRUE) - diff --git a/data-raw/integerise.R b/data-raw/integerise.R index e7a427d..5704b39 100644 --- a/data-raw/integerise.R +++ b/data-raw/integerise.R @@ -1,6 +1,6 @@ smart.round = function(x) { y = floor(x) - indices = utils::tail(order(x-y), round(sum(x)) - sum(y)) + indices = utils::tail(order(x - y), round(sum(x)) - sum(y)) y[indices] = y[indices] + 1 y } @@ -12,7 +12,6 @@ smart.round.df = function(d) { d = data.frame( all = 48.3, train = 0.7, bus = 7.65, taxi = 0.7, car_driver = 3.45, car_passenger = 0.9, bicycle = 0.65, foot = 33.95 - ) res = smart.round.df(d) @@ -29,7 +28,7 @@ od_disag = od_disaggregate(od, zones, subzones) #> although coordinates are longitude/latitude, st_intersects assumes that they are planar #> although coordinates are longitude/latitude, st_intersects assumes that they are planar #> although coordinates are longitude/latitude, st_intersects assumes that they are planar -ncol(od_disag) -1 == ncol(od) # same number of columns (except disag data gained geometry) +ncol(od_disag) - 1 == ncol(od) # same number of columns (except disag data gained geometry) #> [1] FALSE sum(od_disag[[3]]) == sum(od[[3]]) #> [1] TRUE diff --git a/data-raw/test-disag.R b/data-raw/test-disag.R index c0a5d0a..58df1be 100644 --- a/data-raw/test-disag.R +++ b/data-raw/test-disag.R @@ -10,7 +10,7 @@ od = od_data_df[1:2, ] zones = od_data_zones_min subzones = od_data_zones_small od_disag = od_disaggregate(od, zones, subzones) -ncol(od_disag) -1 == ncol(od) # same number of columns (except disag data gained geometry) +ncol(od_disag) - 1 == ncol(od) # same number of columns (except disag data gained geometry) sum(od_disag[[3]]) == sum(od[[3]]) sum(od_disag[[4]]) == sum(od[[4]]) od_sf = od_to_sf(od, zones) @@ -42,7 +42,9 @@ qtm(zones_many, borders.lwd = 3) + tm_shape(zones_lsoa_many) + tm_borders("red", lty = 3) + qtm(centroids_lsoa_many) -od_test = desire_lines_many %>% select(geo_code1, geo_code2, all_base) %>% sf::st_drop_geometry() +od_test = desire_lines_many %>% + select(geo_code1, geo_code2, all_base) %>% + sf::st_drop_geometry() # fails: desire_lines_disag = od_disaggregate(od = od_test, z = zones_many, subzones = zones_lsoa_many) @@ -55,7 +57,8 @@ summary(zones_many$geo_code %in% od_test$geo_code2) summary(zones_many$geo_code %in% od_test$geo_code1) # remove missing line -od_test = desire_lines_many %>% select(geo_code1, geo_code2, all_base) %>% +od_test = desire_lines_many %>% + select(geo_code1, geo_code2, all_base) %>% sf::st_drop_geometry() %>% slice(-nrow(od_test)) diff --git a/data-raw/test-od_disaggregate.R b/data-raw/test-od_disaggregate.R index 33bb0dd..729b99c 100644 --- a/data-raw/test-od_disaggregate.R +++ b/data-raw/test-od_disaggregate.R @@ -2,13 +2,13 @@ library(tidyverse) u = "https://github.com/ITSLeeds/od/releases/download/0.2.1/od_iz_ed.Rds" f = basename(u) -if(!file.exists(f)) download.file(u, f) +if (!file.exists(f)) download.file(u, f) od = readRDS("od_iz_ed.Rds") # head(od) u = "https://github.com/ITSLeeds/od/releases/download/0.2.1/iz_zones11_ed.Rds" f = basename(u) -if(!file.exists(f)) download.file(u, f) +if (!file.exists(f)) download.file(u, f) zones = readRDS("iz_zones11_ed.Rds") # head(zones) diff --git a/man/od_disaggregate.Rd b/man/od_disaggregate.Rd index ac2283a..3a3fb72 100644 --- a/man/od_disaggregate.Rd +++ b/man/od_disaggregate.Rd @@ -97,7 +97,7 @@ od = od_data_df[1:2, 1:4] subzones = od_data_zones_small try(od_disaggregate(od, zones, subzones)) od_disag = od_disaggregate(od, zones, subzones, max_per_od = 500) -ncol(od_disag) -3 == ncol(od) # same number of columns, the same... +ncol(od_disag) - 3 == ncol(od) # same number of columns, the same... # Except disag data gained geometry and new agg ids: sum(od_disag[[3]]) == sum(od[[3]]) sum(od_disag[[4]]) == sum(od[[4]]) diff --git a/man/od_sample_vertices.Rd b/man/od_sample_vertices.Rd index 3eb868b..9438a3f 100644 --- a/man/od_sample_vertices.Rd +++ b/man/od_sample_vertices.Rd @@ -18,7 +18,7 @@ Todo: export this at some point \dontrun{ u = "https://github.com/ITSLeeds/od/releases/download/v0.3.1/road_network_min.Rds" f = basename(u) -if(!file.exists(f)) download.file(u, f) +if (!file.exists(f)) download.file(u, f) road_network_min = readRDS(f) od_sample_vertices(road_network_min) } diff --git a/tests/tinytest.R b/tests/tinytest.R index d618f64..ea58569 100644 --- a/tests/tinytest.R +++ b/tests/tinytest.R @@ -1,5 +1,3 @@ - -if ( requireNamespace("tinytest", quietly=TRUE) ){ +if (requireNamespace("tinytest", quietly = TRUE)) { tinytest::test_package("od") } -