Returns a list the same length as the input, where each element is an array of that geometry's constituent parts. A multipolygon of three polygons becomes one element holding a polygon array of length three.
Details
The output geometry type is the singular form of the input:
| input | element type |
multipoint, point | point |
multilinestring, linestring | linestring |
multipolygon, polygon | polygon |
The element type is decided by the array's declared type, not by its
contents, so an array of multipolygons that all happen to hold one part
still explodes to polygons. A mixed geometry array has no single singular
type and errors; narrow it with ga_downcast_geometry() first.
A singular geometry yields an element of length one, so the operation is well defined for any input. A null geometry yields a null element, which is distinct from an empty one.
Because the length is preserved, the result lines up with the row it came
from and can sit alongside the other columns of a table. Use ga_flatten() to
collapse it into a single array with one row per part.
See also
Other cast:
ga_as_point(),
ga_cast_geometry(),
ga_downcast_geometry(),
ga_flatten(),
ga_from_wkb()
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