Title: | Extract Centerline from Closed Polygons |
---|---|
Description: | Generates skeletons of closed 2D polygons using Voronoi diagrams. It provides methods for 'sf', 'terra', and 'geos' objects to compute polygon centerlines based on the generated skeletons. Voronoi, G. (1908) <doi:10.1515/crll.1908.134.198>. |
Authors: | Anatoly Tsyplenkov [aut, cre, cph] |
Maintainer: | Anatoly Tsyplenkov <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.1 |
Built: | 2024-11-13 08:28:28 UTC |
Source: | https://github.com/atsyplenkov/centerline |
Find the shortest path between start and end points within a polygon
cnt_path(skeleton, start_point, end_point)
cnt_path(skeleton, start_point, end_point)
skeleton |
an output from |
start_point |
one or more starting points. It should be of the same
class as the |
end_point |
one ending point of the same class as |
The following function uses the sfnetworks::st_network_paths()
approach to
connect start_point
with end_point
by using the
skeleton
of a closed polygon as potential routes.
It is important to note that multiple starting points are permissible, but there can only be one ending point. Should there be two or more ending points, the algorithm will return an error.
Neither starting nor ending points are required to be located on the edges of a polygon (i.e., snapped to the boundary); they can be positioned wherever possible inside the polygon.
The algorithm identifies the closest nodes of the polygon's skeleton
to the starting and ending points and then connects them
using the shortest path possible along the skeleton.
Therefore, if more precise placement of start and end
points is necessary, consider executing the cnt_skeleton()
function with the keep = 1
option. In doing so, the resulting
skeleton may be more detailed, increasing the likelihood that the starting
and ending points are already situated on the skeleton paths.
a list of sf
, sfc
, SpatVector
or geos_geometry
class objects of a LINESTRING
geometry
library(sf) library(geos) # Load Polygon and points data polygon <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "polygon", quiet = TRUE ) |> geos::as_geos_geometry() points <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "polygon_points", quiet = TRUE ) |> geos::as_geos_geometry() # Find polygon's skeleton pol_skeleton <- cnt_skeleton(polygon) # Connect points pol_path <- cnt_path( skeleton = pol_skeleton, start_point = points[2], end_point = points[1] ) # Plot plot(polygon) plot(pol_skeleton, col = "blue", add = TRUE) plot(points[1:2], col = "red", add = TRUE) plot(pol_path, lwd = 3, add = TRUE)
library(sf) library(geos) # Load Polygon and points data polygon <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "polygon", quiet = TRUE ) |> geos::as_geos_geometry() points <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "polygon_points", quiet = TRUE ) |> geos::as_geos_geometry() # Find polygon's skeleton pol_skeleton <- cnt_skeleton(polygon) # Connect points pol_path <- cnt_path( skeleton = pol_skeleton, start_point = points[2], end_point = points[1] ) # Plot plot(polygon) plot(pol_skeleton, col = "blue", add = TRUE) plot(points[1:2], col = "red", add = TRUE) plot(pol_path, lwd = 3, add = TRUE)
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.
cnt_path_guess(input, skeleton = NULL, return_geos = FALSE, ...)
cnt_path_guess(input, skeleton = NULL, return_geos = FALSE, ...)
input |
|
skeleton |
|
return_geos |
|
... |
Arguments passed on to
|
An sf
, sfc
or SpatVector
class
object of a LINESTRING
geometry
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)
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)
This function generates skeletons of closed polygon objects.
cnt_skeleton(input, keep = 0.5, method = "voronoi")
cnt_skeleton(input, keep = 0.5, method = "voronoi")
input |
|
keep |
numeric, proportion of points to retain (0.05-5.0; default 0.5). See Details. |
method |
character, either |
If keep = 1
, no transformation will occur. The
function will use the original geometry to find the skeleton.
If the keep
parameter is below 1, then the geos::geos_simplify()
function will be used. So the original input
geometry would be simplified, and the resulting skeleton will be cleaner but
maybe more edgy.
The current realisation of simplification is similar (but not identical)
to rmapshaper::ms_simplify()
one with Douglas-Peuker algorithm. However,
due to geos
superpower, it performs several times faster.
If you find that the built-in simplification algorithm performs poorly,
try rmapshaper::ms_simplify()
first and then find the polygon skeleton
with keep = 1
, i.e.
cnt_skeleton(rmapshaper::ms_simplify(polygon_sf), keep = 1)
If the keep
is above 1, then the densification
algorithm is applied using the geos::geos_densify()
function. This may
produce a very large object if keep is set more than 2. However, the
resulting skeleton would potentially be more accurate.
If method = "voronoi"
(default), the skeleton will be generated
using the geos::geos_voronoi_edges()
function. This is application of the
Voronoi diagram algorithm (Voronoi, 1908).
A Voronoi diagram partitions space into regions based on the distance to
the polygon's vertices. The edges of these
cells form a network of lines (skeletons) that represent
the structure of the polygon while preserving its overall shape.
If method = "straight"
, the skeleton will be generated
using the raybevel::skeletonize()
function. See
https://www.tylermw.com/posts/rayverse/raybevel-introduction.html
a sf
, sfc
, SpatVector
or geos_geometry
class object of a MULTILINESTRING
geometry
Voronoi, G. (1908). Nouvelles applications des paramètres continus à la théorie des formes quadratiques. Journal für die reine und angewandte Mathematik, 134, 198-287. doi:10.1515/crll.1908.134.198
library(sf) polygon <- sf::st_read(system.file("extdata/example.gpkg", package = "centerline"), layer = "polygon", quiet = TRUE ) plot(polygon) pol_skeleton <- cnt_skeleton(polygon) plot(pol_skeleton)
library(sf) polygon <- sf::st_read(system.file("extdata/example.gpkg", package = "centerline"), layer = "polygon", quiet = TRUE ) plot(polygon) pol_skeleton <- cnt_skeleton(polygon) plot(pol_skeleton)
Binding for ggplot2::geom_sf()
, therefore it supports
only sf
objects.
geom_cnt( mapping = ggplot2::aes(), data = NULL, stat = "sf", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, keep = 0.5, method = c("voronoi", "straight"), simplify = TRUE, ... )
geom_cnt( mapping = ggplot2::aes(), data = NULL, stat = "sf", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, keep = 0.5, method = c("voronoi", "straight"), simplify = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
You can also set this to one of "polygon", "line", and "point" to override the default legend. |
inherit.aes |
If |
keep |
numeric, proportion of points to retain (0.05-5.0; default 0.5). See Details. |
method |
character, either |
simplify |
logical, if |
... |
Other arguments passed on to
|
A Layer
ggproto object that can be added to a plot.
coord_sf()
ensures that all layers use a common CRS. You can
either specify it using the crs
param, or coord_sf()
will
take it from the first layer that defines a CRS.
Most regular geoms, such as geom_point()
, geom_path()
,
geom_text()
, geom_polygon()
etc. will work fine with coord_sf()
. However
when using these geoms, two problems arise. First, what CRS should be used
for the x and y coordinates used by these non-sf geoms? The CRS applied to
non-sf geoms is set by the default_crs
parameter, and it defaults to
NULL
, which means positions for non-sf geoms are interpreted as projected
coordinates in the coordinate system set by the crs
parameter. This setting
allows you complete control over where exactly items are placed on the plot
canvas, but it may require some understanding of how projections work and how
to generate data in projected coordinates. As an alternative, you can set
default_crs = sf::st_crs(4326)
, the World Geodetic System 1984 (WGS84).
This means that x and y positions are interpreted as longitude and latitude,
respectively. You can also specify any other valid CRS as the default CRS for
non-sf geoms.
The second problem that arises for non-sf geoms is how straight lines
should be interpreted in projected space when default_crs
is not set to NULL
.
The approach coord_sf()
takes is to break straight lines into small pieces
(i.e., segmentize them) and then transform the pieces into projected coordinates.
For the default setting where x and y are interpreted as longitude and latitude,
this approach means that horizontal lines follow the parallels and vertical lines
follow the meridians. If you need a different approach to handling straight lines,
then you should manually segmentize and project coordinates and generate the plot
in projected coordinates.
geom_cnt_text()
, geom_cnt_label()
, ggplot2::geom_sf()
if (requireNamespace("geomtextpath", quietly = TRUE)) { library(sf) library(ggplot2) lake <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "lake", quiet = TRUE ) ggplot() + geom_sf(data = lake) + geom_cnt( data = lake, keep = 1, simplify = TRUE ) + theme_void() }
if (requireNamespace("geomtextpath", quietly = TRUE)) { library(sf) library(ggplot2) lake <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "lake", quiet = TRUE ) ggplot() + geom_sf(data = lake) + geom_cnt( data = lake, keep = 1, simplify = TRUE ) + theme_void() }
Binding for geomtextpath::geom_textsf()
and
geomtextpath::geom_labelsf()
geom_cnt_text( mapping = ggplot2::aes(), data = NULL, stat = "sf", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, keep = 0.5, method = c("voronoi", "straight"), simplify = TRUE, ... ) geom_cnt_label( mapping = ggplot2::aes(), data = NULL, stat = "sf", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, keep = 0.5, method = c("voronoi", "straight"), simplify = TRUE, ... )
geom_cnt_text( mapping = ggplot2::aes(), data = NULL, stat = "sf", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, keep = 0.5, method = c("voronoi", "straight"), simplify = TRUE, ... ) geom_cnt_label( mapping = ggplot2::aes(), data = NULL, stat = "sf", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, keep = 0.5, method = c("voronoi", "straight"), simplify = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
You can also set this to one of "polygon", "line", and "point" to override the default legend. |
inherit.aes |
If |
keep |
numeric, proportion of points to retain (0.05-5.0; default 0.5). See Details. |
method |
character, either |
simplify |
logical, if |
... |
Arguments passed on to
|
geom_cnt_text()
understands the following aesthetics:
x
y
label
alpha
angle
colour
family
fontface
group
hjust
linecolour
lineheight
linetype
linewidth
size
spacing
textcolour
vjust
In addition to aforementioned aesthetics, geom_cnt_label()
also
understands:
boxcolour
boxlinetype
boxlinewidth
fill
geom_cnt()
, geomtextpath::geom_textsf()
,
geomtextpath::geom_labelsf()
, ggplot2::geom_sf()
library(sf) library(ggplot2) lake <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "lake", quiet = TRUE ) # Plot centerline and lake name as text ggplot() + geom_sf(data = lake) + geom_cnt_text( data = lake, aes(label = "Lake Ohau"), size = 8, simplify = TRUE ) + theme_void() # Plot lake name as label ggplot() + geom_sf(data = lake) + geom_cnt_label( data = lake, aes(label = "Lake Ohau"), linecolor = NA, # disable line drawing size = 10, method = "s", simplify = TRUE ) + theme_void()
library(sf) library(ggplot2) lake <- sf::st_read( system.file("extdata/example.gpkg", package = "centerline"), layer = "lake", quiet = TRUE ) # Plot centerline and lake name as text ggplot() + geom_sf(data = lake) + geom_cnt_text( data = lake, aes(label = "Lake Ohau"), size = 8, simplify = TRUE ) + theme_void() # Plot lake name as label ggplot() + geom_sf(data = lake) + geom_cnt_label( data = lake, aes(label = "Lake Ohau"), linecolor = NA, # disable line drawing size = 10, method = "s", simplify = TRUE ) + theme_void()