| spatial_explode | R Documentation |
Streams a lazy vectra query and splits every multipart geometry into its
component single-part geometries: a MULTIPOLYGON becomes one row per
polygon, a MULTILINESTRING one row per linestring, a MULTIPOINT one row
per point, and a GEOMETRYCOLLECTION one row per member (recursively). The
attributes of the source feature are copied onto each part. Already
single-part geometries pass through unchanged, as does an empty geometry
(kept as one row). This is the streaming counterpart of the QGIS
"multipart to singleparts" tool and of sf::st_cast() to a single-part type.
spatial_explode(
x,
geom = "geometry",
crs = NA,
out_geom = NULL,
part = NULL,
flush_rows = NULL
)
x |
A |
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
|
out_geom |
Name of the output geometry column. Defaults to |
part |
Optional name of an integer column numbering the parts within each
source feature, 1-based. Default |
flush_rows |
Transformed rows buffered before a spill flush. Larger
values mean fewer, bigger temporary files. |
One batch is decoded, exploded, and spilled at a time, so peak memory tracks one batch and its parts, not the whole layer.
A vectra_node of single-part features, backed by temporary .vtr
spills (removed when the node is garbage-collected) and carrying the input
CRS.
spatial_map() for per-feature transforms, spatial_dissolve() to
merge features the other way, collect_sf() to materialize as sf.
mp <- sf::st_multipolygon(list(
list(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0))),
list(rbind(c(2, 2), c(3, 2), c(3, 3), c(2, 3), c(2, 2)))))
f <- tempfile(fileext = ".vtr")
write_vtr(data.frame(
id = 1L,
geometry = sf::st_as_binary(sf::st_sfc(mp), hex = TRUE)
), f)
# One row per polygon, attributes copied, parts numbered.
tbl(f) |> spatial_explode(part = "part_id") |> collect_sf()
unlink(f)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.