ST_Generalize function

The ST_Generalize function takes a geometry and a threshold as input parameters and represents the specified geometry with a reduced number of points, while preserving the general characteristics of the geometry.

The Douglas-Peucker line-simplification algorithm is used, by which the sequence of points that define the geometry is recursively subdivided until a run of the points can be replaced by a straight line segment. In this line segment, none of the defining points deviates from the straight line segment by more than the specified threshold. Z and M coordinates are not considered for the simplification. The resulting geometry is in the spatial reference system of the specified geometry.

If the specified geometry is empty, an empty geometry of type ST_Point is returned. If the specified geometry or the threshold is null, null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_Generalize(geometry,threshold )

Parameter

geometry
A value of type ST_Geometry or one of its subtypes that represents the geometry for which the line-simplification is applied.
threshold
A value of type DOUBLE that identifies the threshold to be used for the line-simplification algorithm. The threshold must be greater than or equal to 0 (zero). The larger the threshold, the smaller the number of points that will be used to represent the generalized geometry.

Return type

ST_Geometry

Examples

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

Example 1
A linestring is created with eight points that go from (10, 10) to (80, 80). The path is almost a straight line, but some of the points are slightly off of the line. The ST_Generalize function can be used to reduce the number of points in the line.

CREATE TABLE sample_lines (id INTEGER, geometry ST_LineString)

INSERT INTO sample_lines VALUES
       (1, ST_Linestring('linestring(10 10, 21 20, 34 26, 40 40,
                                     52 50, 59 63, 70 71, 80 80)' ,0))

Example 2
When a generalization factor of 3 is used, the linestring is reduced to four coordinates, and is still very close to the original representation of the linestring.

SELECT CAST(ST_AsText(ST_Generalize(geometry, 3)) as VARCHAR(70))  
  Generalize_3
FROM sample_lines

Results:

GENERALIZE 3
----------------------------------------------------------------------
LINESTRING (10 10, 34 26, 59 63, 80 80)
  
Example 3
When a generalization factor of 6 is used, the linestring is reduced to only two coordinates. This produces a simpler linestring than the previous example, however it deviates more from the original representation.

SELECT CAST(ST_AsText(ST_Generalize(geometry, 6)) as VARCHAR(70)) 
  Generalize_6
FROM sample_lines

Results:

GENERALIZE 6
---------------------------------------------------------------------
LINESTRING (10 10, 80 80)