Name

ST_CollectionHomogenize — Returns the simplest representation of a geometry collection.

Synopsis

geometry ST_CollectionHomogenize(geometry collection);

Description

Given a geometry collection, returns the "simplest" representation of the contents.

  • Homogeneous (uniform) collections are returned as the appropriate multi-geometry.

  • Heterogeneous (mixed) collections are flattened into a single GeometryCollection.

  • Collections containing a single atomic element are returned as that element.

  • Atomic geometries are returned unchanged. If required, these can be converted to a multi-geometry using ST_Multi.

[Warning]

This function does not ensure that the result is valid. In particular, a collection containing adjacent or overlapping Polygons will create an invalid MultiPolygon. This situation can be checked with ST_IsValid and repaired with ST_MakeValid.

Availability: 2.0.0

Examples

Single-element collection converted to an atomic geometry

Code
SELECT ST_CollectionHomogenize('GEOMETRYCOLLECTION(POINT(0 0))');
Output
POINT(0 0)
Figure
Geometry figure for visual-st-collectionhomogenize-01

Nested single-element collection converted to an atomic geometry:

Code
SELECT ST_CollectionHomogenize('GEOMETRYCOLLECTION(MULTIPOINT((0 0)))');
Output
POINT(0 0)
Figure
Geometry figure for visual-st-collectionhomogenize-02

Collection converted to a multi-geometry:

Code
SELECT ST_CollectionHomogenize('GEOMETRYCOLLECTION(POINT(0 0), POINT(1 1))');
Output
MULTIPOINT((0 0),(1 1))
Figure
Geometry figure for visual-st-collectionhomogenize-03

Nested heterogeneous collection flattened to a GeometryCollection:

Code
SELECT ST_CollectionHomogenize('GEOMETRYCOLLECTION(POINT(0 0), GEOMETRYCOLLECTION( LINESTRING(1 1,2 2)))');
Output
GEOMETRYCOLLECTION(POINT(0 0), LINESTRING(1 1,2 2))
Figure
Geometry figure for visual-st-collectionhomogenize-04

Collection of Polygons converted to an (invalid) MultiPolygon:

Code
SELECT ST_CollectionHomogenize('GEOMETRYCOLLECTION (POLYGON ((10 50,50 50,50 10,10 10,10 50)),POLYGON ((90 50,90 10,50 10,50 50,90 50)))');
Output
MULTIPOLYGON(((10 50,50 50,50 10,10 10,10 50)),((90 50,90 10,50 10,50 50,90 50)))
Figure
Geometry figure for visual-st-collectionhomogenize-05