Examples
The input polygon has 55 points. The result retains 15 points, while rendering the same shape inside the view envelope.
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;
-[ 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))
010300000001000000380000009A999999999909406666666666662C403333333333331B406666666666662C406666666666661E40CDCCCCCCCCCC2740CDCCCCCCCCCC1C409A999999999923403333333333331F40CDCCCCCCCCCC1C409A99999999992440CDCCCCCCCCCC1C4000000000000027400000000000001E409A99999999992B40CDCCCCCCCCCC1C403333333333332D409A999999999917406666666666662C4033333333333313409A99999999992D409A999999999909403333333333332D40CDCCCCCCCCCCEC3F6666666666662A40333333333333F3BF9A999999999929406666666666660EC0000000000000284066666666666610C0000000000000264000000000000010C09A99999999992340CDCCCCCCCCCC10C09A9999999999214033333333333313C09A9999999999204000000000000004C00000000000001A409A9999999999C93F33333333333317409A9999999999014033333333333315409A9999999999C93F0000000000000C40000000000000F8BF66666666666606406666666666660EC09A9999999999F93F00000000000012C0333333333333D3BFCDCCCCCCCCCC14C09A999999999901C033333333333317C0CDCCCCCCCCCCECBF33333333333313C0000000000000F8BF9A999999999911C0CDCCCCCCCCCCFCBF66666666666606C0333333333333F3BFCDCCCCCCCCCCFCBF000000000000E03FCDCCCCCCCCCCFCBFCDCCCCCCCCCCFC3FCDCCCCCCCCCCFCBF66666666666602409A9999999999B9BF000000000000F83FCDCCCCCCCCCCFC3FCDCCCCCCCCCCFC3F9A9999999999094000000000000000000000000000000C409A999999999909C0CDCCCCCCCCCC1040CDCCCCCCCCCC14C0CDCCCCCCCCCC10409A999999999911C09A999999999913409A999999999913C0000000000000164033333333333311C09A999999999917406666666666660EC09A9999999999194000000000000008C09A99999999991B409A999999999909C03333333333331F409A999999999901C06666666666662040333333333333FBBFCDCCCCCCCCCC21409A999999999905C0CDCCCCCCCCCC22409A999999999901C0CDCCCCCCCCCC2340666666666666F6BFCDCCCCCCCCCC2540CDCCCCCCCCCCFCBF9A99999999992740333333333333F3BF666666666666284066666666666602C09A9999999999294033333333333307C06666666666662A40333333333333E33F0000000000002B409A999999999909406666666666662C40 | 010300000001000000050000009A9999999999B93F00000000000000009A9999999999B93F000000000000244000000000000024400000000000002440000000000000244000000000000000009A9999999999B93F0000000000000000 | 010300000001000000100000006666666666661E40CDCCCCCCCCCC2740CDCCCCCCCCCC1C409A999999999923403333333333331F40CDCCCCCCCCCC1C409A99999999992440CDCCCCCCCCCC1C40000000000000264000000000000010C09A9999999999204000000000000004C00000000000001A409A9999999999C93F33333333333317409A9999999999014033333333333315409A9999999999C93F0000000000000C40000000000000F8BF66666666666602409A9999999999B9BF000000000000F83FCDCCCCCCCCCCFC3FCDCCCCCCCCCCFC3F9A9999999999094000000000000000000000000000000C4033333333333307C06666666666662A406666666666661E40CDCCCCCCCCCC2740 | 010300000001000000050000009A9999999999B93F00000000000000009A9999999999B93F000000000000244000000000000024400000000000002440000000000000244000000000000000009A9999999999B93F0000000000000000
Only linework affecting the view is retained; existing vertices are reused.
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;
-[ 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))
0105000000030000000102000000040000000000000000000000000000000000000000000000000024400000000000000000000000000000344000000000000000000000000000003E40000000000000000001020000000800000000000000000000000000000000002E4000000000000014400000000000002E4000000000000024400000000000002E400000000000002E400000000000002E4000000000000034400000000000002E4000000000000039400000000000002E400000000000003E400000000000002E4000000000000044400000000000002E400102000000030000000000000000002A400000000000002A400000000000002E400000000000002E4000000000000031400000000000003140 | 010300000001000000050000000000000000002840000000000000284000000000000028400000000000003240000000000000324000000000000032400000000000003240000000000000284000000000000028400000000000002840 | 01050000000200000001020000000300000000000000000024400000000000002E400000000000002E400000000000002E4000000000000034400000000000002E400102000000030000000000000000002A400000000000002A400000000000002E400000000000002E4000000000000031400000000000003140 | 010300000001000000050000000000000000002840000000000000284000000000000028400000000000003240000000000000324000000000000032400000000000003240000000000000284000000000000028400000000000002840
A line with no segment relevant to the view becomes empty.
SELECT ST_RemoveIrrelevantPointsForView('LINESTRING(0 0,10 0,20 0,30 0)',
ST_MakeEnvelope(12, 12, 18, 18), true);
Removing vertices without computing intersections can introduce self-intersections in the result.
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;
-[ 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))
010300000001000000070000000000000000000000000000000000244000000000000000000000000000000000000000000000244000000000000024C00000000000003E40000000000000000000000000000039400000000000003440000000000000244000000000000014C000000000000000000000000000002440 | 0103000000010000000500000000000000000014C0000000000000F03F00000000000014C00000000000002840000000000000344000000000000028400000000000003440000000000000F03F00000000000014C0000000000000F03F | 0103000000010000000600000000000000000000000000000000002440000000000000000000000000000000000000000000003E40000000000000000000000000000039400000000000003440000000000000244000000000000014C000000000000000000000000000002440 | 0103000000010000000500000000000000000014C0000000000000F03F00000000000014C00000000000002840000000000000344000000000000028400000000000003440000000000000F03F00000000000014C0000000000000F03F
With the default cartesian_hint , the function removes only points known to be irrelevant to the view.
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;
-[ 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))
0103000000010000000600000000000000000000000000000000003E400000000000002E400000000000003E400000000000003E400000000000003E400000000000003E4000000000000000000000000000000000000000000000000000000000000000000000000000003E40 | 010300000001000000050000000000000000002840000000000000284000000000000028400000000000003240000000000000324000000000000032400000000000003240000000000000284000000000000028400000000000002840 | 0103000000010000000500000000000000000000000000000000003E400000000000003E400000000000003E400000000000003E4000000000000000000000000000000000000000000000000000000000000000000000000000003E40 | 010300000001000000050000000000000000002840000000000000284000000000000028400000000000003240000000000000324000000000000032400000000000003240000000000000284000000000000028400000000000002840