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 specified 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 specified geometry is null or is empty, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_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

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.

 
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(ST_AsText(ST_ConvexHull(geometry)) 
   AS varchar(300)) AS convexhull
FROM   sample_geometries

Results:

ID    SPATIAL_TYPE       CONVEXHULL
----- ------------------ ----------------------------------------------------
    1 ST_LineString      POLYGON ((20 40, 20 20, 30 30, 30 50, 20 40))                                                                                                                                                                                                                                                            
    2 ST_Polygon         POLYGON ((110 140, 110 120, 120 130, 110 140))                                                                                                                                                                                                                                                           
    3 ST_Polygon         POLYGON ((15 50, 25 35, 30 30, 60 30, 75 40, 80 90, 
                                         40 85, 35 80, 15 50))                                                                                                                                                                                                                                
    4 ST_MultiPoint      POLYGON ((20 40, 20 20, 30 30, 30 50, 20 40))