ST_Subdivide

Name

ST_Subdivide — Returns a set of geometry where no geometry in the set has more than the specified number of vertices.

Synopsis

setof geometry ST_Subdivide ( geometry geom , integer max_vertices=256 ) ;

Description

Turns a single geometry into a set in which each element has fewer than the maximum allowed number of vertices. Useful for converting excessively large polygons and other objects into small portions that fit within the database page size. Uses the same envelope clipping as ST_ClipByBox2D does, recursively subdividing the input geometry until all portions have less than the maximum vertex count. Minimum vertice count allowed is 8 and if you try to specify lower than 8, it will throw an error.

Clipping performed by the GEOS module.

[Note]

Requires GEOS 3.5.0+

Availability: 2.2.0 requires GEOS >= 3.5.0.

Examples

-- Create a new subdivided table suitable for joining to the original
CREATE TABLE subdivided_geoms AS
SELECT pkey, ST_Subdivide(geom) AS geom
FROM original_geoms;
 

Subdivide max 10 vertices

SELECT row_number() OVER() As rn, ST_AsText(geom) As wkt
FROM ( SELECT ST_SubDivide('POLYGON((132 10,119 23,85 35,68 29,66 28,49 42,32 56,22 64,32 110,40 119,36 150,
57 158,75 171,92 182,114 184,132 186,146 178,176 184,179 162,184 141,190 122,
190 100,185 79,186 56,186 52,178 34,168 18,147 13,132 10))'::geometry,10))  As f(geom);

rn |                          wkt
---+---------------------------------------------------------------------------
 1 | POLYGON((22 64,29.3913043478263 98.000000000001,106.000000000001 98.00000000001,
        106.000000000001 27.5882352941173,85 35,68 29,66 28,49 42,32 56,22 64))
 2 | POLYGON((29.3913043478263 98.000000000001,32 110,40 119,36 150,57 158,
        75 11,92 182,106.000000000001 183.272727272727,106.000000000001 98.000000000001,
        29.913043478263 98.000000000001))
 3 | POLYGON((106.000000000001 27.5882352941173,106.000000000001 98.00000000000,
 189.52380952381 98.000000000001,185 79,186 56,186 52,178 34,168 18,147 13,
 132 0,119 23,106.000000000001 27.5882352941173))
 4 | POLYGON((106.000000000001 98.000000000001,106.000000000001 183.27272727272,
    114 184,132 186,146 178,176 184,179 162,184 141,190 122,190 100,189.5238095238
 98.000000000001,106.000000000001 98.000000000001))

Useful in conjunction with ST_Segmentize to create additional vertices that can then be used for splitting

SELECT ST_AsText(ST_SubDivide(ST_Segmentize('LINESTRING(0 0, 100 100, 150 150)'::geometry,10),8));

LINESTRING(0 0,7.07106781186547 7.07106781186547,14.1421356237309 14.1421356237309,21.2132034355964 21.2132034355964,28.2842712474619 28.2842712474619,35.3553390593274 35.3553390593274,37.499999999998 37.499999999998)
LINESTRING(37.499999999998 37.499999999998,42.4264068711929 42.4264068711929,49.4974746830583 49.4974746830583,56.5685424949238 56.5685424949238,63.6396103067893 63.6396103067893,70.7106781186548 70.7106781186548,74.999999999998 74.999999999998)
LINESTRING(74.999999999998 74.999999999998,77.7817459305202 77.7817459305202,84.8528137423857 84.8528137423857,91.9238815542512 91.9238815542512,98.9949493661167 98.9949493661167,100 100,107.071067811865 107.071067811865,112.499999999998 112.499999999998)
LINESTRING(112.499999999998 112.499999999998,114.142135623731 114.142135623731,121.213203435596 121.213203435596,128.284271247462 128.284271247462,135.355339059327 135.355339059327,142.426406871193 142.426406871193,149.497474683058 149.497474683058,149.999999999998 149.999999999998)