4

I am trying to learn the usage of Virtual Layers in QGIS but I can't find a solution for a problem that seems very simple.

I have two polygon layers and I want to have the difference.

Polygon layer 1:
Polygon layer 1

A "cut" layer:
"cut" layer

And this is what I want to get:
enter image description here

How should my query look like to achieve the desired output?

3 Answers 3

4

Another solution is to use the ST_Union() inside of the query (it was also mentioned in
@geozelot's comment), because features from a "cut" layer need to become one geometry (of type MULTIPOLYGON). This idea was taken from this thread : More on cutting polygons with polygons, in PostGIS.

Let's assume there are two polygon layers called 'grid_test' (green) with nine features and 'grid_test2' (orange) with five features in it, see image below.

input

So, your query may look like this:

WITH union_gd2 AS ( SELECT ST_Union(gt2.geometry) as geometry FROM "grid_test2" AS gt2 ) SELECT gd1.id, st_difference(gd1.geometry, ugd2.geometry) AS geometry FROM "grid_test" AS gd1, "union_gd2" AS ugd2 WHERE st_isvalid(st_difference(gd1.geometry, ugd2.geometry)) 

or like this:

SELECT gd1.id, st_difference(gd1.geometry, union_gd2.geometry) AS geometry FROM "grid_test" AS gd1, (SELECT ST_Union(gt2.geometry) as geometry FROM "grid_test2" AS gt2 ) AS union_gd2 WHERE st_isvalid(st_difference(gd1.geometry, union_gd2.geometry)) 

and then get the final output

result


References:

0
4

For a virtual layer, use this query:

select st_difference ( p1.geometry, st_intersection ( p1.geometry, p2.geometry )) as geom from polygon1 as p1, polygon2 as p2 

Red outline: Polygon layer 1; blue outline: polygon layer2, orange: output of the virtual layer = red polygons - blue polygons: enter image description here

4
  • This gives not the desired result. It only works correctly with just 1 polygon in the "cut" layer. WIth two polygons as in my example it makes something different. As far as I understand it, this is because your SQL expression calculates the difference for every possible combination of elements from the two layers. Commented Dec 15, 2021 at 22:45
  • You're right, I corrected this, see updated answer Commented Dec 15, 2021 at 22:55
  • Your updated solution works in some cases but not in mine. Took me a while to find out why. In my case there exist two polygons in the layer "polygon1" that intersect with both polygons of layer "polygon2" . In this case your solution does not work correctly. I would prefer a foolproof solution that I can easily port to any similar problem in the future. Commented Dec 16, 2021 at 0:03
  • 2
    Difference is tricky - you want a union of all intersecting 'cut' polygons (polygon2) for each 'input' polygon (polygon1) to create a complete difference. Note that this is a very costly operation, no matter the software. Commented Dec 16, 2021 at 8:35
0

Try using the "Difference" Tool in the Procesing Toolbox. Use the Polygon Layer 1 as Input Layer or "base" and the Polygon Layer 2 as the Overlay Layer or "cutter".

You can leave the rest of the inputs as default (unless you want a physical copy of the layer - you can select an output folder and file type.).

This is the same as ESRI ArcMap's "Erase Tool" and is a reverse clip.

Link to QGIS Documentation on the Difference Tool

1
  • 2
    Ignore this, did not realise they were both Virtual Layers. @Babel 's answer is better. Commented Dec 15, 2021 at 22:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.