R/utils.R

Defines functions contains_or_overlaps check_geometry to_sfc

#' @noRd
to_sfc <- function(x) {
  sfc <- NULL
  if (inherits(x,  "sfc")) {
    sfc <- x
  } else if (inherits(x,  "sf")) {
    sfc <- sf::st_geometry(x)
  } else if (inherits(x,  "sfnetwork")) {
    sf <- sf::st_as_sf(x, "edges")
    sfc <- to_sfc(sf)
  } else {
    sfc <- sf::st_as_sfc(x)
  }
  sfc
}

#' @noRd
check_geometry <- function(geometry) {
  # verify that we only have linestrings
  geometry_type <- sf::st_geometry_type(geometry)
  is_not_linestring <- geometry_type != "LINESTRING"
  if (any(is_not_linestring)) {
    template <- "Edges should be of type LINESTRING (a %s is provided)."
    stop(sprintf(template, geometry_type[is_not_linestring]))
  }
}

#' @noRd
contains_or_overlaps <- function(x, y) {
  contains <- sf::st_contains(x, y)
  overlaps <- sf::st_overlaps(x, y)
  mapply(c, contains, overlaps)
}

Try the rcoins package in your browser

Any scripts or data that you put into this service are public.

rcoins documentation built on Aug. 21, 2025, 5:53 p.m.