centerline
The centerline R package simplifies the extraction of linear features
from complex polygons, such as roads or rivers, by computing their
centerlines (or median-axis) based on skeletons. It uses the super-fast
geos library in the
background and have bindings for your favorite spatial data library
(sf,
terra and
geos).
Installation
# The easiest way to get centerline is to install it from CRAN:
install.packages("centerline")
# Or the development version from GitHub:
# install.packages("pak")
pak::pak("atsyplenkov/centerline")Examples for closed geometries
At the heart of this package is the cnt_skeleton function, which
efficiently computes the skeleton of closed 2D polygonal geometries. The
function uses
geos::geos_simplify
by default to keep the most important nodes and reduce noise from the
beginning. However, it has option to densify the amount of points using
geos::geos_densify,
which can produce more smooth results. Otherwise, you can set the
parameter keep = 1 to work with the initial geometry.
library(sf)
library(centerline)
lake <-
sf::st_read(
system.file("extdata/example.gpkg", package = "centerline"),
layer = "lake",
quiet = TRUE
)
# Original
lake_skeleton <-
cnt_skeleton(lake, keep = 1)
# Simplified
lake_skeleton_s <-
cnt_skeleton(lake, keep = 0.1)
# Densified
lake_skeleton_d <-
cnt_skeleton(lake, keep = 2)cnt_skeleton() code