Skip to content

Commit

Permalink
filter valid route by total weight
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhao0217 committed Feb 19, 2025
1 parent b527338 commit 706ae81
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions R/corenet.R
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,14 @@ prepare_network = function(network, key_attribute = "all_fastest_bicycle_go_dutc
#' @param minDistPts The minimum distance (in meters) to consider for path calculations.
#' @param centroids An sf object containing centroids to which paths are calculated.
#' @param path_type A character string indicating the type of path calculation: 'shortest', 'all_shortest', or 'all_simple'.
#' @param max_path_weight The maximum weight threshold for paths to be considered.
#' - 'shortest': Calculates the shortest path considering weights.
#' - 'all_shortest': Calculates all paths that tie for the shortest distance, considering weights.
#' - 'all_simple': Calculates all simple paths, ignoring weights.
#' @return An sf object containing the paths that meet the criteria or NULL if no paths meet the criteria.
#' @export

calculate_paths_from_point_dist = function(network, point, minDistPts = 2, maxDistPts = 1500, centroids, path_type = "shortest") {
calculate_paths_from_point_dist = function(network, point, minDistPts = 2, maxDistPts = 1500, centroids, path_type = "shortest", max_path_weight = 10) {
path_cache = list()

# Ensure the network's CRS is correctly set for distance measurement in meters
Expand Down Expand Up @@ -526,10 +527,24 @@ calculate_paths_from_point_dist = function(network, point, minDistPts = 2, maxDi
type = path_type
)

edges_in_paths = paths_from_point |>
# edges_in_paths = paths_from_point |>
# dplyr::pull(edge_paths) |>
# base::unlist() |> base::unique()

# result = network |> dplyr::slice(unique(edges_in_paths)) |> sf::st_as_sf()
paths_from_point$total_weight = purrr::map_dbl(
paths_from_point$edge_paths,
~ sum(network |> activate(edges) |> slice(.x) |> pull(weight))
)

# Filter out paths exceeding weight threshold
valid_paths = paths_from_point[paths_from_point$total_weight <= max_path_weight, ]

edges_in_paths = valid_paths |> # CHANGED: Use filtered paths
dplyr::pull(edge_paths) |>
base::unlist() |> base::unique()

base::unlist() |>
base::unique()

result = network |> dplyr::slice(unique(edges_in_paths)) |> sf::st_as_sf()
} else {
result = NULL
Expand All @@ -541,6 +556,7 @@ calculate_paths_from_point_dist = function(network, point, minDistPts = 2, maxDi
}



#' Calculate the largest connected component of a network
#'
#' This function takes a spatial network represented as an `sf` object, converts it into a graph format,
Expand Down

0 comments on commit 706ae81

Please sign in to comment.