From 941fc7ce393929a01b4e13cef2766a5ff07b5535 Mon Sep 17 00:00:00 2001 From: wangzhao0217 <74598734+wangzhao0217@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:58:41 +0100 Subject: [PATCH] Update penalty cals penalty = dplyr::case_when( value < 1 ~ penalty_value[1], # Penalty of penalty_value[1] when value < 1 value >= 1 & value <= 50 ~ penalty_value[2], value > 50 & value <= 200 ~ penalty_value[3], TRUE ~ 1 # Default case when none of the above conditions are met ) --- R/corenet.R | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/R/corenet.R b/R/corenet.R index caeedb5..4c6d878 100644 --- a/R/corenet.R +++ b/R/corenet.R @@ -399,7 +399,7 @@ coherent_network_group = function(coherent_network, key_attribute = "all_fastest #' @export prepare_network = function(network, key_attribute = "all_fastest_bicycle_go_dutch", - road_scores = list("A Road" = 1, "B Road" = 2, "Minor Road" = 100000),penalty_value = 1, + road_scores = list("A Road" = 1, "B Road" = 2, "Minor Road" = 100000),penalty_value = [10000000, 10000, 1000], transform_crs = 27700) { # Cast the network to LINESTRING and transform to the specified CRS network = network |> @@ -409,9 +409,9 @@ prepare_network = function(network, key_attribute = "all_fastest_bicycle_go_dutc sfnetworks::activate("edges") |> dplyr::mutate( # Handle NA values and normalize using key_attribute - value = dplyr::if_else(is.na(!!rlang::sym(key_attribute)), 0, !!rlang::sym(key_attribute)), + value = dplyr::if_else(is.na(!!rlang::sym(key_attribute)), 0.01, !!rlang::sym(key_attribute)), max_value = max(value, na.rm = TRUE), - value = ifelse(value <= 50, 0, value), + value = ifelse(value <= 5, 0, value), value = ifelse(value == 0, 0.01, value), # Calculate logarithmic arterialness and round it arterialness = ((max_value / value) ^ 3) / max(((max_value / value) ^ 3), na.rm = TRUE), @@ -425,7 +425,12 @@ prepare_network = function(network, key_attribute = "all_fastest_bicycle_go_dutc }), # Calculate weight considering the road type influence weight = round(0.95 * arterialness + 0.05 * road_score, 6), - penalty = ifelse(value <= 200, penalty_value, 1), + penalty = dplyr::case_when( + value < 1 ~ penalty_value[1], # Penalty of penalty_value[1] when value < 1 + value >= 1 & value <= 50 ~ penalty_value[2], + value > 50 & value <= 200 ~ penalty_value[3], + TRUE ~ 1 # Default case when none of the above conditions are met + ), weight = weight * penalty ) # network = sfnetworks::activate(network, "nodes")