ST_ConvexHull function

The ST_ConvexHull function takes a geometry as an input parameter and returns the convex hull of it.

The resulting geometry is represented in the spatial reference system of the given geometry.

If possible, the specific type of the returned geometry will be ST_Point, ST_LineString, or ST_Polygon. For example, the boundary of a polygon with no holes is a single linestring, represented as ST_LineString. The boundary of a polygon with one or more holes consists of multiple linestrings, represented as ST_MultiLineString.

If the given geometry is null or is empty, then null is returned.

This function can also be called as a method.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_ConvexHull(geometry)

Parameter

geometry
A value of type ST_Geometry or one of its subtypes that represents the geometry to compute the convex hull.

Return type

db2gse.ST_Geometry

Example

In the following example, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display.

The following code creates and populates the SAMPLE_GEOMETRIES table.

SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse 
 
CREATE TABLE sample_geometries(id INTEGER, spatial_type varchar(18), 
   geometry ST_GEOMETRY)

INSERT INTO sample_geometries(id, spatial_type, geometry)
VALUES
    (1, 'ST_LineString', ST_LineString
        ('linestring(20 20, 30 30, 20 40, 30 50)', 0)),
    (2, 'ST_Polygon', ST_Polygon('polygon
        ((110  120,  110 140, 120 130, 110 120))', 0) ),
    (3, 'ST_Polygon', ST_Polygon('polygon((30 30, 25 35, 15 50, 
        35 80, 40 85, 80 90,70 75, 65 70, 55 50, 75 40, 60 30, 
        30 30))', 0) ),
    (4, 'ST_MultiPoint', ST_MultiPoint('multipoint(20 20, 30 30, 
        20 40, 30 50)', 1))

The following SELECT statement calculates the convex hull for all the geometries constructed previously and displays the result.

SELECT id, spatial_type, cast(geometry..ST_ConvexHull..ST_AsText 
   AS varchar(300)) AS convexhull
FROM   sample_geometries

Results:

ID     SPATIAL_TYPE       CONVEXHULL
----- ------------------ ----------------------------------------------------
    1 ST_LineString      POLYGON (( 20.00000000 40.00000000, 
                           20.00000000 20.00000000, 30.00000000 
                           30.00000000, 30.00000000 50.00000000, 
                           20.00000000 40.00000000)) 

    2 ST_Polygon         POLYGON (( 110.00000000 140.00000000, 
                           110.00000000 120.00000000, 120.00000000
                           130.00000000,  110.00000000 140.00000000))

    3 ST_Polygon         POLYGON (( 15.00000000 50.00000000, 
                           25.00000000 35.00000000, 30.00000000 
                           30.00000000, 60.00000000 30.00000000,
                           75.00000000 40.00000000, 80.00000000 
                           90.00000000, 40.00000000 85.00000000,
                           35.00000000 80.00000000, 15.00000000 
                           50.00000000))

    4 ST_MultiPoint      POLYGON (( 20.00000000 40.00000000, 
                           20.00000000 20.00000000, 30.00000000 
                           30.00000000, 30.00000000 50.00000000, 
                           20.00000000 40.00000000))