Returns a list the same length as the input, where each element is an array
of that geometry's constituent parts. ga_explode() turns a multipolygon of
three polygons into one element holding a polygon array of length three.
Details
ga_explode() returns the singular form of the input geometry type:
| input | element type |
multipoint, point | point |
multilinestring, linestring | linestring |
multipolygon, polygon | polygon |
ga_explode() decides the element type from the array's declared type, not
from its contents, so it still returns polygons for an array of
multipolygons that all happen to hold one part. ga_explode() raises an
error for a mixed geometry array, which has no single singular type;
narrow it with ga_downcast_geometry() first.
ga_explode() returns an element of length one for a singular geometry, so
it handles any input. For a null geometry, it returns a null element, which
is distinct from an empty one.
Because ga_explode() preserves length, you can keep the result alongside
the other columns of a table, each element in line with its source row. Use
ga_flatten() to collapse it into a single array with one row per part.
Examples
fp <- system.file("shape/nc.shp", package = "sf")
nc <- as.data.frame(read_shapefile(fp))
# one list element per county, holding that county's polygon parts
parts <- ga_explode(nc$geometry)
parts$length
#> [1] 100
head(lengths(as.vector(parts)))
#> [1] 1 1 1 3 1 1