[GIS] How to split a polygon by a polygon using PostGIS

polygonpostgissplitting

I've a large polygon (table_a) that contains a smaller polygon (table_b). Now I want to split the larger polygon by the smaller polygon.

Unfortunately st_split(table_a.geom, table_b.geom) isn't working (Error: Splitting a Polygon by a Polygon is unsupported).

Is there any way to split a polygon by a polygon using PostGIS?

enter image description here
enter image description here
enter image description here

Best Answer

You can't split a polygon with another polygon, but you can use ST_Boundary to extract the lines that make up a polygon, and then do the split. For example:

CREATE TEMPORARY TABLE testdata (id serial, geom geometry);
INSERT INTO testdata (geom) VALUES 
(ST_Buffer('POINT (0 0)', 1)),
(ST_Buffer('POINT (0.5 0)', 1));

SELECT ST_Split(a.geom, ST_Boundary(b.geom))
FROM testdata a, testdata b 
WHERE ST_Intersects(a.geom, b.geom) AND a.id != b.id;
Related Question