Name

ST_RemoveIrrelevantPointsForView — Removes points that are irrelevant for rendering a specific rectangular view of a geometry.

Synopsis

geometry ST_RemoveIrrelevantPointsForView(geometry geom, box2d bounds, boolean cartesian_hint = false);

Description

Returns a geometry without points being irrelevant for rendering the geometry within a given rectangular view.

This function can be used to quickly preprocess geometries that should be rendered only within certain bounds.

Only geometries of type (MULTI)POLYGON and (MULTI)LINESTRING are evaluated. Other geometries keep unchanged.

In contrast to ST_ClipByBox2D() this function

  • sorts out points without computing new intersection points which avoids rounding errors and usually increases performance,

  • returns a geometry with equal or similar point number,

  • leads to the same rendering result within the specified view, and

  • may introduce self-intersections which would make the resulting geometry invalid (see example below).

If cartesian_hint is set to true, the algorithm applies additional optimizations involving cartesian math to further reduce the resulting point number. Please note that using this option might introduce rendering artifacts if the resulting coordinates are projected into another (non-cartesian) coordinate system before rendering.

[Warning]

For polygons, this function does currently not ensure that the result is valid. This situation can be checked with ST_IsValid and repaired with ST_MakeValid.

Availability: 3.5.0

Examples

The input polygon has 55 points. The result retains 15 points, while rendering the same shape inside the view envelope.

Code
WITH data(geom, view) AS (VALUES (
  'POLYGON((3.2 14.2,6.8 14.2,7.6 11.9,7.2 9.8,7.8 7.2,10.3 7.2,11.5 7.5,13.8 7.2,14.6 5.9,14.2 4.8,14.8 3.2,14.6 0.9,13.2 -1.2,12.8 -3.8,12 -4.1,11 -4,9.8 -4.2,8.8 -4.8,8.3 -2.5,6.5 0.2,5.8 2.2,5.3 0.2,3.5 -1.5,2.8 -3.8,1.6 -4.5,-0.3 -5.2,-2.2 -5.8,-0.9 -4.8,-1.5 -4.4,-1.8 -2.8,-1.2 -1.8,0.5 -1.8,1.8 -1.8,2.3 -0.1,1.5 1.8,1.8 3.2,0 3.5,-3.2 4.2,-5.2 4.2,-4.4 4.9,-4.9 5.5,-4.3 5.9,-3.8 6.4,-3 6.9,-3.2 7.8,-2.2 8.2,-1.7 8.9,-2.7 9.4,-2.2 9.9,-1.4 10.9,-1.8 11.8,-1.2 12.2,-2.3 12.8,-2.9 13.2,0.6 13.5,3.2 14.2))'::geometry,
  ST_MakeEnvelope(0.1, 0, 10, 10)
))
SELECT geom AS input_geom,
       view AS input_view,
       ST_RemoveIrrelevantPointsForView(geom, view, true) AS result,
       view AS view
FROM data;
Output
-[ RECORD 1 ]----------
input_geom | POLYGON((3.2 14.2,6.8 14.2,7.6 11.9,7.2 9.8,7.8 7.2,10.3 7.2,11.5 7.5,13.8 7.2,14.6 5.9,14.2 4.8,14.8 3.2,14.6 0.9,13.2 -1.2,12.8 -3.8,12 -4.1,11 -4,9.8 -4.2,8.8 -4.8,8.3 -2.5,6.5 0.2,5.8 2.2,5.3 0.2,3.5 -1.5,2.8 -3.8,1.6 -4.5,-0.3 -5.2,-2.2 -5.8,-0.9 -4.8,-1.5 -4.4,-1.8 -2.8,-1.2 -1.8,0.5 -1.8,1.8 -1.8,2.3 -0.1,1.5 1.8,1.8 3.2,0 3.5,-3.2 4.2,-5.2 4.2,-4.4 4.9,-4.9 5.5,-4.3 5.9,-3.8 6.4,-3 6.9,-3.2 7.8,-2.2 8.2,-1.7 8.9,-2.7 9.4,-2.2 9.9,-1.4 10.9,-1.8 11.8,-1.2 12.2,-2.3 12.8,-2.9 13.2,0.6 13.5,3.2 14.2))
input_view | POLYGON((0.1 0,0.1 10,10 10,10 0,0.1 0))
result     | POLYGON((7.6 11.9,7.2 9.8,7.8 7.2,10.3 7.2,11 -4,8.3 -2.5,6.5 0.2,5.8 2.2,5.3 0.2,3.5 -1.5,2.3 -0.1,1.5 1.8,1.8 3.2,0 3.5,-2.9 13.2,7.6 11.9))
view       | POLYGON((0.1 0,0.1 10,10 10,10 0,0.1 0))
Figure
Geometry figure for visual-st-removeirrelevantpointsforview-01

Only linework affecting the view is retained; existing vertices are reused.

Code
WITH data(geom, view) AS (VALUES (
  'MULTILINESTRING((0 0,10 0,20 0,30 0),(0 15,5 15,10 15,15 15,20 15,25 15,30 15,40 15),(13 13,15 15,17 17))'::geometry,
  ST_MakeEnvelope(12, 12, 18, 18)
))
SELECT geom AS input_geom,
       view AS input_view,
       ST_RemoveIrrelevantPointsForView(geom, view, true) AS result,
       view AS view
FROM data;
Output
-[ RECORD 1 ]----------
input_geom | MULTILINESTRING((0 0,10 0,20 0,30 0),(0 15,5 15,10 15,15 15,20 15,25 15,30 15,40 15),(13 13,15 15,17 17))
input_view | POLYGON((12 12,12 18,18 18,18 12,12 12))
result     | MULTILINESTRING((10 15,15 15,20 15),(13 13,15 15,17 17))
view       | POLYGON((12 12,12 18,18 18,18 12,12 12))
Figure
Geometry figure for visual-st-removeirrelevantpointsforview-02

A line with no segment relevant to the view becomes empty.

Code
SELECT ST_RemoveIrrelevantPointsForView('LINESTRING(0 0,10 0,20 0,30 0)',
ST_MakeEnvelope(12, 12, 18, 18), true);
Output
LINESTRING EMPTY

Removing vertices without computing intersections can introduce self-intersections in the result.

Code
WITH data(geom, view) AS (VALUES (
  'POLYGON((0 10,0 0,10 -10,30 0,25 20,10 -5,0 10))'::geometry,
  ST_MakeEnvelope(-5, 1, 20, 12)
))
SELECT geom AS input_geom,
       view AS input_view,
       ST_RemoveIrrelevantPointsForView(geom, view, true) AS result,
       view AS view
FROM data;
Output
-[ RECORD 1 ]----------
input_geom | POLYGON((0 10,0 0,10 -10,30 0,25 20,10 -5,0 10))
input_view | POLYGON((-5 1,-5 12,20 12,20 1,-5 1))
result     | POLYGON((0 10,0 0,30 0,25 20,10 -5,0 10))
view       | POLYGON((-5 1,-5 12,20 12,20 1,-5 1))
Figure
Geometry figure for visual-st-removeirrelevantpointsforview-04

With the default cartesian_hint, the function removes only points known to be irrelevant to the view.

Code
WITH data(geom, view) AS (VALUES (
  'POLYGON((0 30,15 30,30 30,30 0,0 0,0 30))'::geometry,
  ST_MakeEnvelope(12, 12, 18, 18)
))
SELECT geom AS input_geom,
       view AS input_view,
       ST_RemoveIrrelevantPointsForView(geom, view) AS result,
       view AS view
FROM data;
Output
-[ RECORD 1 ]----------
input_geom | POLYGON((0 30,15 30,30 30,30 0,0 0,0 30))
input_view | POLYGON((12 12,12 18,18 18,18 12,12 12))
result     | POLYGON((0 30,30 30,30 0,0 0,0 30))
view       | POLYGON((12 12,12 18,18 18,18 12,12 12))
Figure
Geometry figure for visual-st-removeirrelevantpointsforview-05