Friday 23 November 2018

How to use "Merge vector layers" in QGIS using Geopackages as output with FIDs duplicated



I'm using QGIS 3.2.3 in Windows and I created a tessellation with polygons (Voronoi tiling / Thiessen polygons) with the built-in tool "Voronoi Diagrams", (Call this layer LAYER1). Afterwards, I used LAYER1 to subdivide another layer stored as a Geopackage with many polygons (call this layer LAYER2) using the built-in tool "Clip" and the option "Iterate over this layer" to create a separate layer for every feature in LAYER2 with the subdivision of LAYER1 and set as output for each one a new Geopackage (call this layers OUTPUTS).


Now I want to merge all this OUTPUTS in a new layer, so I get a tesellation of polygons in LAYER2, for that I tried to use the tool "Merge Vector layers" that I suppose replace and extends the old tool "Merge Shapefiles", but as I'm using now geopackage this process fails (If I set the output format as Shapefile it runs successfully, because that format doesn't use a unique ID).


I suppose that, this problem is caused because all features in all OUTPUTS must to have different FID to be merged, but that's no my case because the FID in the outputs are based on FIDs of LAYER1 so the same polygon can divide many others in LAYER2.


Is there a solution to this problem using QGIS built-in tools? The easy way could be just use as output format Shapefile. But I like to maintain the consistency of my project and avoid to rename some fields in my layers, or edit manually the fids in the outputs.



Answer



As long as the QGIS fix is not ready you can use ogr2ogr for merging the layers. Use it with parameter -unsetFid https://www.gdal.org/ogr2ogr.html. Run this command for all your input layers for appending data from the layers to a single table named "merged"


ogr2ogr -f gpkg -update -append -unsetFid output.gpkg input_layer1 -nln merged



Afterward of @ndawson's comment and @user30184's answer. I wrote a batch file for Windows that iterates in all OUTPUTS with the pattern iterate_*.gpkg and merges all in one file this way:



echo off set dst_datasource_name=voronoi_merge.gpkg del %dst_datasource_name% for %%f in (iterate_*.gpkg) do ( echo Merging %%~nf ogr2ogr -f gpkg -update -append -unsetFid %dst_datasource_name% %%~nf.gpkg -nln voronoi_merge )


This file must be run from the folder with all the OUTPUTS


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...