I'm trying to cut holes in forest polygons, where there are lakes.
My PostGIS/PostgreSQL code look like this:
CREATE TABLE samp.forest_with_holes AS
SELECT
fo.objectid,
(CASE ST_Intersects(fo.geom, la.geom)
WHEN TRUE THEN ST_Difference(fo.geom, so.geom)
ELSE fo.geom
END CASE;) as geom
from samp.forest as fo, samp.lake as la;
But I get the error message:
ERROR: syntax error at or near "CASE"
LINE xx: END CASE;) as geom
Can anybody help me to see why this is not working – and please suggest a better way 🙂
Best Answer
Correct way of using CASE:
Better query:
EDIT:
Coalesce gives you the first non-null input and since the difference between a geom and null results in null it will return the next value: 'fo.geom'. The ST_Collect aggregate is needed to make sure you cut multiple holes when there is more than 1 lake in the same forest polygon.