View source: R/spatial_topology.R
| spatial_polygonize | R Documentation |
Forms the polygons enclosed by a set of lines (the QGIS "Polygonize", GEOS
Polygonize): the inverse of taking polygon boundaries. The lines of each
group are unioned and noded so every crossing becomes a shared vertex, then
the faces of that planar arrangement are returned, one per row. A pile of
lines that does not close any area yields no faces. Like spatial_dissolve()
and spatial_construct() it rides the partition tier: x is spilled once
and routed into one disjoint shard per by group in a single bounded pass,
then each group's lines are polygonized together. Peak memory is the routing
budget during the pass, then one group's geometry while its faces are built –
partition on a key whose groups fit in memory. With no by, the whole layer
yields one set of faces.
spatial_polygonize(
x,
by = NULL,
geom = "geometry",
crs = NA,
flush_rows = NULL
)
x |
A |
by |
Character vector of attribute columns to polygonize within: one set
of faces per distinct combination of their values. |
geom |
Name of the input geometry column holding hex-WKB or WKT strings.
Default |
crs |
Coordinate reference system of the input geometry, in any form
|
flush_rows |
Transformed rows buffered before a spill flush. Larger
values mean fewer, bigger temporary files. |
Each face is new geometry built from the whole group, so it carries the by
columns only, not the attributes of any single source line. Geometry travels
through the engine as hex-encoded WKB in a string column and the CRS is
carried on the returned node; the noding is sf/GEOS and expects
projected or unprojected planar data. The sf package is an optional
dependency (Suggests).
A vectra_node of one row per face, carrying the by columns and the
input CRS, backed by temporary .vtr spills removed when the node is
garbage-collected.
spatial_split() to cut existing polygons by a blade,
spatial_construct() for hulls and tessellations, spatial_dissolve() to
merge geometries by group, collect_sf() to materialize as sf.
grid <- sf::st_sfc(
sf::st_linestring(rbind(c(0, 0), c(2, 0))),
sf::st_linestring(rbind(c(0, 1), c(2, 1))),
sf::st_linestring(rbind(c(0, 2), c(2, 2))),
sf::st_linestring(rbind(c(0, 0), c(0, 2))),
sf::st_linestring(rbind(c(1, 0), c(1, 2))),
sf::st_linestring(rbind(c(2, 0), c(2, 2))))
f <- tempfile(fileext = ".vtr")
write_vtr(data.frame(
geometry = sf::st_as_binary(grid, hex = TRUE)
), f)
# The four unit cells enclosed by the grid of lines.
tbl(f) |> spatial_polygonize() |> collect_sf()
unlink(f)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.