From 380b4f57ac961d0edfdebc727a2c283bfa3e482c Mon Sep 17 00:00:00 2001 From: wangzhao0217 Date: Thu, 20 Feb 2025 11:35:17 +0000 Subject: [PATCH] update corenet for anime_join --- DESCRIPTION | 2 +- NAMESPACE | 16 ++++++- R/anime_join.R | 8 +++- README.Rmd | 2 +- man/anime_join.Rd | 63 ++++++++++++++++++++++++++ man/calculate_paths_from_point_dist.Rd | 12 +++-- man/cohesive_network_prep.Rd | 5 +- man/corenet.Rd | 6 ++- 8 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 man/anime_join.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 3d42fa5..16fc06f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,4 +35,4 @@ Suggests: Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/NAMESPACE b/NAMESPACE index ae2566c..c729a50 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(anime_join) export(calculate_largest_component) export(calculate_paths_from_point_dist) export(coherent_network_group) @@ -8,4 +9,17 @@ export(corenet) export(create_coherent_network_PMtiles) export(prepare_network) export(removeDangles) -export(anime_join) +importFrom(anime,anime) +importFrom(anime,get_matches) +importFrom(dplyr,any_of) +importFrom(dplyr,group_by) +importFrom(dplyr,left_join) +importFrom(dplyr,mutate) +importFrom(dplyr,row_number) +importFrom(dplyr,select) +importFrom(dplyr,summarise) +importFrom(purrr,reduce) +importFrom(rlang,expr) +importFrom(rlang,sym) +importFrom(rlang,syms) +importFrom(sf,st_drop_geometry) diff --git a/R/anime_join.R b/R/anime_join.R index 82842e8..0819651 100644 --- a/R/anime_join.R +++ b/R/anime_join.R @@ -54,7 +54,13 @@ anime_join = function( ) { # Helper to display the name of the aggregation function get_fun_name = function(fn) { - deparse(substitute(fn)) + # If the function is one of the standard ones, just return its name. + # Otherwise, you can fallback to something generic, e.g. "user-defined function". + if (identical(fn, sum)) return("sum") + if (identical(fn, mean)) return("mean") + if (identical(fn, max)) return("max") + # fallback + return("user-defined function") } # Let user know what's happening diff --git a/README.Rmd b/README.Rmd index 78e722a..41fbcfe 100644 --- a/README.Rmd +++ b/README.Rmd @@ -98,7 +98,7 @@ OS_NPT_demo = cohesive_network_prep(base_network = os_edinburgh_demo_6km, crs = "EPSG:27700", key_attribute = "road_function", attribute_values = c("A Road", "B Road", "Minor Road"), use_stplanr = FALSE) -# mapview::mapview(OS_NPT_demo, color = "blue") + mapview::mapview(NPT_demo_6km, color = "red") +mapview::mapview(OS_NPT_demo, zcol = "all_fastest_bicycle_go_dutch") ``` ```{r} #rename NPT_MM_OSM$geom as NPT_MM_OSM$geometry diff --git a/man/anime_join.Rd b/man/anime_join.Rd new file mode 100644 index 0000000..23472d6 --- /dev/null +++ b/man/anime_join.Rd @@ -0,0 +1,63 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/anime_join.R +\name{anime_join} +\alias{anime_join} +\title{Aggregate a Source Attribute onto Target Features Using 'anime'} +\usage{ +anime_join( + source_data, + target_data, + attribute, + new_name, + agg_fun = sum, + weights, + angle_tolerance = 35, + distance_tolerance = 15 +) +} +\arguments{ +\item{source_data}{An \code{sf} object representing the source data.} + +\item{target_data}{An \code{sf} object representing the target data.} + +\item{attribute}{A character string, the name of the source attribute to aggregate.} + +\item{new_name}{A character string for the output column name in the returned data frame.} + +\item{agg_fun}{A function used to aggregate the matched values (e.g., \code{sum}, \code{mean}, \code{max}). +Defaults to \code{sum}.} + +\item{weights}{A character vector of column names in the source data to be used as weights. +(For example, \code{"target_weighted"}).} + +\item{angle_tolerance}{A numeric value for angular tolerance used by \code{anime}. Defaults to 35.} + +\item{distance_tolerance}{A numeric value for distance tolerance used by \code{anime}. Defaults to 15.} +} +\value{ +A \code{data.frame} (or \code{tibble}) containing: +\itemize{ +\item \code{id}: The original \code{id} from \code{target_data}. +\item \code{new_name}: The aggregated attribute (named according to \code{new_name}). +} +} +\description{ +This function runs \code{\link[anime:anime]{anime::anime()}} to match source features to target features +based on angle and distance tolerances. The matched source attribute(s) are then +aggregated (sum, max, mean, etc.) and joined back to the target data. +} +\examples{ +\dontrun{ + result = anime_join( + source_data = my_source_sf, + target_data = my_target_sf, + attribute = "population", + new_name = "pop_sum", + agg_fun = sum, + weights = "target_weighted", + angle_tolerance = 35, + distance_tolerance = 15 + ) +} + +} diff --git a/man/calculate_paths_from_point_dist.Rd b/man/calculate_paths_from_point_dist.Rd index 421ed73..34d228f 100644 --- a/man/calculate_paths_from_point_dist.Rd +++ b/man/calculate_paths_from_point_dist.Rd @@ -10,7 +10,8 @@ calculate_paths_from_point_dist( minDistPts = 2, maxDistPts = 1500, centroids, - path_type = "shortest" + path_type = "shortest", + max_path_weight = 10 ) } \arguments{ @@ -24,10 +25,13 @@ calculate_paths_from_point_dist( \item{centroids}{An sf object containing centroids to which paths are calculated.} -\item{path_type}{A character string indicating the type of path calculation: 'shortest', 'all_shortest', or 'all_simple'. -- 'shortest': Calculates the shortest path considering weights. +\item{path_type}{A character string indicating the type of path calculation: 'shortest', 'all_shortest', or 'all_simple'.} + +\item{max_path_weight}{\itemize{ +\item '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.} +- 'all_simple': Calculates all simple paths, ignoring weights. +}} } \value{ An sf object containing the paths that meet the criteria or NULL if no paths meet the criteria. diff --git a/man/cohesive_network_prep.Rd b/man/cohesive_network_prep.Rd index 53381c2..3e82a33 100644 --- a/man/cohesive_network_prep.Rd +++ b/man/cohesive_network_prep.Rd @@ -11,7 +11,7 @@ cohesive_network_prep( crs = "EPSG:27700", key_attribute = "road_function", attribute_values = c("A Road", "B Road", "Minor Road"), - use_stplanr = TRUE + use_stplanr = FALSE ) } \arguments{ @@ -50,8 +50,7 @@ os_edinburgh_demo_3km = sf::st_set_crs(os_edinburgh_demo_3km, 27700) NPT_demo_3km = sf::st_set_crs(NPT_demo_3km, 27700) base_network = sf::st_transform(os_edinburgh_demo_3km, crs = 27700) influence_network = sf::st_transform(NPT_demo_3km, crs = 27700) -target_zone = zonebuilder::zb_zone("Edinburgh", n_circles = 2) |> - sf::st_transform(crs = "EPSG:27700") +target_zone = zonebuilder::zb_zone("Edinburgh", n_circles = 3) |> sf::st_transform(crs = "EPSG:27700") # Prepare the cohesive network OS_NPT_demo = cohesive_network_prep( base_network = base_network, diff --git a/man/corenet.Rd b/man/corenet.Rd index 31c7121..207f360 100644 --- a/man/corenet.Rd +++ b/man/corenet.Rd @@ -15,7 +15,9 @@ corenet( minDistPts = 2, road_scores = list(`A Road` = 1, `B Road` = 1, `Minor Road` = 1000), n_removeDangles = 6, - penalty_value = 1 + penalty_value = 1, + group_column = "name_1", + max_path_weight = 10 ) } \arguments{ @@ -40,6 +42,8 @@ corenet( \item{n_removeDangles}{Number of iterations to remove dangles from the network, default is 6.} \item{penalty_value}{The penalty value for roads with low values, default is 1.} + +\item{group_column}{The column name to group the network by edge betweenness, default is "name_1".} } \value{ A spatial object representing the largest cohesive component of the network, free of dangles.