Skip to contents

This function, as follows from the title, tries to guess the polygon centerline by connecting the most distant points from each other. First, it finds the point most distant from the polygon's centroid, then it searches for a second point, which is most distant from the first. The line connecting these two points will be the desired centerline.

Usage

cnt_path_guess(input, skeleton = NULL, return_geos = FALSE, ...)

Arguments

input

sf, sfc or SpatVector polygons object

skeleton

NULL (default) or cnt_skeleton() output. If NULL then polygon's skeleton would be estimated in the background using specified parameters (see inherit params below). Forwarded anchors from ... apply only when a skeleton is built or rebuilt; they are ignored when a valid skeleton is supplied.

return_geos

FALSE (default). A logical flag that controls whether the geos_geometry should be returned.

...

Arguments passed on to cnt_skeleton

keep

numeric, proportion of points to retain (0.05-5.0; default 0.5). See Details.

method

character, either "voronoi" (default) or "straight", or just the first letter "v" or "s". See Details.

anchors

NULL (default) or boundary POINT geometries of the same spatial class and CRS as input. When supplied, each point must lie on exactly one polygon-part boundary (exterior or hole ring). With method = "voronoi" and keep < 1, accepted anchors are injected as exact exterior/hole ring vertices before protected simplification so they participate in geos_unique_points() as Voronoi sites (the achieved vertex count may exceed the nominal keep density). Connector validation and clipping then use that prepared polygon. With keep >= 1, anchors are not injected and the ordinary no-op or densify site set is retained. In all cases anchors are also attached by a direct interior connector so they remain explicit degree-one graph terminals. Attribute columns on anchors are ignored; the returned skeleton still inherits polygon attributes exactly as without anchors.

Value

An sf, sfc or SpatVector class object of a LINESTRING geometry

Examples

library(sf)
library(geos)
lake <-
  sf::st_read(
    system.file("extdata/example.gpkg", package = "centerline"),
    layer = "lake",
    quiet = TRUE
  ) |>
  geos::as_geos_geometry()
# Find lake's centerline
lake_centerline <- cnt_path_guess(input = lake, keep = 1)
# Plot
plot(lake)
plot(lake_centerline, col = "firebrick", lwd = 2, add = TRUE)