4

I have a SpatiaLite database with some multipolygon layers loaded.

What I would like to do is to merge (not dissolve) 2 layers into 1. Both layers have the same SRID and same field name (they are a result of the same algorithm).

I'm looking for a SpatiaLite funcion that does the same thing of QGIS -> Union.

Is there a simple comand to do that?


My solution, using @Alexandre's suggestions:

CREATE TABLE merged AS
SELECT * FROM layer1
UNION ALL
SELECT * FROM layer2;

SELECT RecoverGeometryColumn('merged', 'Geometry',  32632, 'MULTIPOLYGON', 'XY');

1 Answer 1

4

If you use DB Manager, you can use plain SQL to union two tables. Something like this:

SELECT field1, field2 FROM my_table_1
UNION ALL
SELECT field1, field2 FROM my_table_2

Notice that the fields must match in both tables in terms of data type, but they do not need to have the same name.

3
  • Hi Alexandre, thanks for your help! It works! But a small problem related to the question: I'm trying to create a new table (CREATE TABLE merged AS.. and your code). But, doing so, I don't have a 'spatial layer', but only a 'normal' table.. Should I re-load the geometry in some way?
    – matteo
    Sep 22, 2015 at 12:36
  • I would create the layer before, with all the necessary steps to add a geometry column (I'm not familiar with how Spatialite handles geometry tables now) and then insert the result of the query into the new table. Sep 22, 2015 at 12:48
  • Thanks Alexandre, I'll edit my question with the code you suggested.
    – matteo
    Sep 22, 2015 at 13:11

Your Answer

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

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