Skip to content

rnet_filter() #577

@Robinlovelace

Description

@Robinlovelace
#' Filter route network by connected component size
#'
#' Groups a route network using stplanr::rnet_group() and removes
#' components (islands) smaller than a specified size.
#'
#' @param rl An sf object representing a route network (LINESTRING/MULTILINESTRING).
#' @param component_size Integer threshold; components with strictly more than this
#' number of features are retained. Default 10 (matching previous inline logic).
#' @return An sf object filtered to retain only components exceeding the threshold.
#' @examples
#' # rnet_filtered <- rnet_filter(rnet_sf, component_size = 20)
#' @export
rnet_filter <- function(rl, component_size = 10) {
  stopifnot(inherits(rl, "sf"))
  if (!"geometry" %in% names(rl)) {
    stop("Input 'rl' must be an sf object with a geometry column.")
  }
  rl$group <- stplanr::rnet_group(rl)
  rl <- dplyr::group_by(rl, group)
  rl <- dplyr::mutate(rl, n = dplyr::n())
  rl <- dplyr::filter(rl, n > component_size)
  rl <- dplyr::select(rl, -n, -group)
  rl <- dplyr::ungroup(rl)
  rl
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions