Fork me on GitHub

ST_Simplify

Signature

GEOMETRY ST_Simplify(GEOMETRY geom, DOUBLE distance);

Description

Simplifies geom using the Douglas-Peuker algorithm with a distance tolerance of distance. Ensures that resulting polygonal geometries are valid.

The Douglas-Peucker algorithm does not preserve topology.

To preserve the topology, use ST_SimplifyPreserveTopology. But note that ST_Simplify is significantly faster.

Examples

SELECT ST_Simplify('POLYGON((2 1, 1 2, 2 2, 2 3, 3 3, 3 2,
                             4 2, 4 1, 3 0, 2 0, 2 1))',
                    0.5)
-- Answer: POLYGON((2 1, 1 2, 3 3, 4 1, 3 0, 2 0, 2 1))

SELECT ST_Simplify('POLYGON((2 1, 1 2, 2 2, 2 3, 3 3, 3 2,
                             4 2, 4 1, 3 0, 2 0, 2 1))',
                    1)
-- Answer:POLYGON((2 1, 1 2, 3 3, 4 1, 2 1))

-- If the distance tolerance is too large, the Geometry may be
-- oversimplified:
SELECT ST_Simplify('POLYGON((2 1, 1 2, 2 2, 2 3, 3 3, 3 2,
                             4 2, 4 1, 3 0, 2 0, 2 1))',
                    2)
-- Answer: POLYGON EMPTY

-- POINTs and MULTIPOINTs cannot be further simplified:
SELECT ST_Simplify('MULTIPOINT((190 300), (10 11))', 4);
-- Answer: MULTIPOINT((190 300), (10 11))

-- Simplify a LINESTRING:
SELECT ST_Simplify('LINESTRING(250 250, 280 290, 300 230, 340 300,
                               360 260, 440 310, 470 360, 604 286)',
                   40);
-- Answer: LINESTRING(250 250, 280 290, 300 230, 470 360, 604 286)
Comparison with ST_SimplifyPreserveTopology
CREATE TABLE INPUT_TABLE(geom GEOMETRY);
INSERT INTO INPUT_TABLE VALUES (
    'POLYGON((8 25, 28 22, 28 20, 15 11, 33 3, 56 30,
              46 33, 46 34, 47 44, 35 36, 45 33, 43 19,
              29 21, 29 22, 35 26, 24 39, 8 25))');

-- Notice that the result of ST_SimplifyPreserveTopology contains
-- more vertices than the result of ST_Simplify. In general, it will
-- contain enough to ensure validity.
SELECT ST_SimplifyPreserveTopology(geom, 20) SPT,
       ST_Simplify(geom, 20) S FROM INPUT_TABLE;

-- Answer:
-- |               SPT              |            S          |
-- |--------------------------------|-----------------------|
-- | POLYGON((8 25, 33 3, 56 30,    | POLYGON((8 25, 56 30, |
-- |          47 44, 43 19, 8 25))  |          33 3, 8 25)) |

CREATE TABLE INPUT_TABLE(geom GEOMETRY);
INSERT INTO INPUT_TABLE VALUES (
    'POLYGON((5 7, 2 5, 5 4, 13 4, 18 7, 16 11, 7 9, 11 7, 5 7),
             (13 8, 13 6, 14 6, 15 9, 13 8))');

-- Here ST_SimplifyPreserveTopology keeps the hole whereas
-- ST_Simplify removes it:
SELECT ST_SimplifyPreserveTopology(geom, 3) SPT,
       ST_Simplify(geom, 3) S FROM INPUT_TABLE;

-- Answer:
-- |              SPT            |              S             |
-- |-----------------------------|----------------------------|
-- | POLYGON((5 7, 2 5, 13 4,    | POLYGON((5 7, 16 11, 18 7, |
-- |          18 7, 16 11, 5 7), |          2 5, 5 7))        |
-- |         (13 8, 13 6, 14 6,  |                            |
-- |          15 9, 13 8))       |                            |

See also