3

I have several polygon layers that I would like to merge and dissolve within a virtual layer in QGIS. So that any changes made to the individual layers will automatically update in the merged/dissolved Virtual Layer. I want to dissolve the entire layers, not based on any fields.

I’ve used ST_Union to do this with a single layer, as below:

SELECT ST_Union(geometry)
FROM "Important Wetlands"

What is the correct syntax to add multiple layers to this Virtual Layer?

The layers are in a GeoPackage.

1

2 Answers 2

3

Here are two ways of doing it:

  1. fetch all geometries then union them
SELECT st_union(sub.geometry) 
FROM (
   SELECT geometry FROM layerA
   UNION ALL
   SELECT geometry FROM layerB
)sub
  1. Union each layer independently then union again
SELECT st_union(sub.geom) 
FROM (
   SELECT st_union(geometry) as geom FROM layerA
   UNION ALL
   SELECT st_union(geometry) as geom FROM layerB
)sub
2

Another solution is to use the st_union() function for several times

SELECT
    st_union(st_union(g1.geometry, g2.geometry))
FROM
    "grid_test" AS g1,
    "grid_test2" AS g2

Input:

input

Output:

output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.