ST_CollectionExtract

更新时间:
复制 MD 格式

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

ParameterTypeDescription
collectiongeometryThe input GeometryCollection from which to extract geometry objects.
typeintegerThe geometry type to extract.

Supported values for type:

ValueGeometry type
1Point
2LineString
3Polygon

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 type is 3 (Polygon), the function always returns a MultiPolygon, even when polygon edges in the collection are shared.

Warning

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.