What is the right way to calculate the difference between two layers? I tried to use the following approach:
SELECT ST_Difference(river.geom, lakes.geom)
FROM river LEFT JOIN lakes ON ST_Intersects(river.geom, lakes.geom)
But in the output, I lose the geometries of the river
layer which do not intersect any geometries in lakes
. It looks like left join does not work as expected.
Currently, I'm using another approach, but I'm not sure this is correct:
SELECT ST_Difference(river.geom, lakes.geom)
FROM river JOIN lakes ON ST_Intersects(river.geom, lakes.geom)
UNION
SELECT river.geom
FROM river JOIN lakes ON NOT ST_Intersects(river.geom, lakes.geom)