The ST_ConvexHull() function

The ST_ConvexHull() function returns the convex hull of any geometry object that has at least three vertices forming a convex.

Syntax

ST_ConvexHull (g1 ST_ Geometry)

Usage

Creating a convex hull is often the first step when tessellating a set of points to create a triangulated irregular network (TIN). If vertices of the geometry do not form a convex, ST_ConvexHull() returns a null.

ST_ConvexHull() generates an ST_Polygon value from the convex hull of three of the geometries that are pictured in the following figure. ST_ConvexHull() returns a null for the two-point ST_LineString because it does not form a convex hull.
Figure 1: The ST_ConvexHull() function.

This graphic is described in the surrounding text.

Return type

ST_Geometry

Example

The example creates the convexhull_test table that has two columns: geotype and g1. The geotype column is of VARCHAR(20) type and holds the name of the geometry subclass that is stored in g1, an ST_Geometry column:
CREATE TABLE convexhull_test (geotype varchar(20),
                              g1      ST_Geometry);
The following INSERT statements insert several geometry subclasses into the convexhull_test table:
INSERT INTO convexhull_test VALUES( 
   'Point',
   ST_PointFromText('point (10.02 20.01)',1000) 
);

INSERT INTO convexhull_test VALUES( 
   'Linestring', 
   ST_LineFromText('linestring (10.02 20.01,10.32 23.98,11.92 25.64)',1000) 
);

INSERT INTO convexhull_test VALUES(
   'Polygon',
   ST_PolyFromText('polygon ((10.02 20.01,11.92 35.64,25.02 34.15,
19.15 33.94,10.02 20.01))',1000) 
);

INSERT INTO convexhull_test VALUES( 
   'MultiPoint',
   ST_MPointFromText('multipoint (10.02 20.01,10.32 23.98,11.92 25.64)',1000)
);

INSERT INTO convexhull_test VALUES( 
   'MultiLineString',
   ST_MLineFromText('multilinestring ((10.02 20.01,10.32 23.98,11.92
25.64),(9.55 23.75,15.36 30.11))',1000)
);

INSERT INTO convexhull_test VALUES( 
   'Multipolygon', 
   ST_MPolyFromText('multipolygon (((10.02 20.01,11.92 35.64,25.02
34.15,19.15 33.94,10.02 20.01)),((51.71 21.73,73.36 27.04,71.52
32.87,52.43 31.90,51.71 21.73)))',1000)
);
The SELECT statement lists the subclass name that is stored in the geotype column and the convex hull:
SELECT geotype, ST_ConvexHull(g1) convexhull 
   FROM convexhull_test;