Extracts geometry objects of a specified type from a GeometryCollection and returns them as a multi-geometry.
Syntax
geometry ST_CollectionExtract(geometry collection, integer type);Parameters
| Parameter | Type | Description |
|---|---|---|
collection | geometry | The input GeometryCollection from which to extract geometry objects. |
type | integer | The geometry type to extract. |
Supported values for type:
| Value | Geometry type |
|---|---|
| 1 | Point |
| 2 | LineString |
| 3 | Polygon |
Description
Returns a multi-geometry (MultiPoint, MultiLineString, or MultiPolygon) containing all geometries of the specified type found in the input collection.
If no geometries of the specified type exist in the input collection, returns an empty geometry object.
When
typeis 3 (Polygon), the function always returns a MultiPolygon, even when polygon edges in the collection are shared.
In most cases, the ST_CollectionExtract function returns an invalid MultiPolygon object. For example, when the input collection comes from ST_Split, the result is an invalid MultiPolygon object. Use ST_IsValid to check the result and ST_MakeValid to repair it if needed.
Examples
Extract LineString geometries (type = 2)
SELECT ST_AsText(
ST_CollectionExtract(
ST_GeomFromText('GEOMETRYCOLLECTION(LINESTRING(1 1,2 1),LINESTRING(2 1,2 2))'),
2
)
);Result:
st_astext
--------------------------------------
MULTILINESTRING((1 1,2 1),(2 1,2 2))
(1 row)What's next
ST_CollectionHomogenize: Returns the simplest representation of a geometry collection.
ST_IsValid: Checks whether a geometry is valid.
ST_MakeValid: Repairs an invalid geometry without losing vertices.
ST_Split: Splits a geometry by another geometry and returns the result as a GeometryCollection.