ST_Generalize function

The ST_Generalize function takes a geometry and a threshold as input parameters and represents the given 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 given threshold. Z and M coordinates are not considered for the simplification. The resulting geometry is in the spatial reference system of the given geometry.

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

This function can also be called as a method.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_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

db2gse.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.

SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse
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(115))  
  Generalize_3
FROM sample_lines

Results:

GENERALIZE 3
----------------------------------------------------------------------
LINESTRING ( 10.00000000 10.00000000, 34.00000000 26.00000000, 
  59.00000000  63.00000000, 80.00000000 80.00000000)

  
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(65)) 
  Generalize_6
FROM sample_lines

Results:

GENERALIZE 6
----------------------------------------------------------------
LINESTRING ( 10.00000000 10.00000000, 80.00000000 80.00000000)