3

At 3.34.1, I need to merge all layers within a single File Geodatabase. All layers are polygonal and are in the same CRS.

Specifically, I would like to conduct the merge without having to first load all of the layers into the Layers panel.

The Merge Vector Layers... tool has the option to Add Directory.. (below), but it will not accept a .gdb as the directory.

enter image description here

How do I merge all layers found within a File Geodatabase, without having to first load them into the Layers panel?

4
  • 1
    Esri file geodatabases are read only in QGIS. Are you willing to write to another database type or does it have to be an Esri file geodatabase?
    – GBG
    Dec 11, 2023 at 21:17
  • 3
    File geodatabases can now be written by QGIS.
    – Stu Smith
    Dec 11, 2023 at 21:25
  • But I suppose that whether or not file geodatabases can be written misses the point, because the merge can be output to a temporary layer.
    – Stu Smith
    Dec 11, 2023 at 21:40
  • 2
    Ahh, nice: north-road.com/2022/11/24/…
    – GBG
    Dec 12, 2023 at 0:39

1 Answer 1

4

It seems like QGIS has some missing features regarding reading and writing from ESRI FileGeodatabases.

So using the UI to perform what you want seems currently impossible, you can however use the Python console to run a short script that does exactly that because QGIS still recognizes the layers.

This code should work, just open the Python Console, change the path to point to your .gdb folder and run the code.

import processing
from qgis.core import QgsVectorLayer, QgsProject

layers = []

gdb = 'C:/path/to/your/maybe_use_gpkg.gdb'
layer = QgsVectorLayer(gdb,"test","ogr")
subLayers = layer.dataProvider().subLayers()
for subLayer in subLayers:
    name = subLayer.split('!!::!!')[1]
    uri = "%s|layername=%s" % (gdb, name)
    layers.append(uri)

output = processing.run("native:mergevectorlayers", {
     'LAYERS': layers,
     'CRS':None,
     'OUTPUT':'TEMPORARY_OUTPUT'
     })

QgsProject.instance().addMapLayer(output['OUTPUT'])

You can still create the command for this in the UI for the other parameters and then use the Copy as Python Command advanced option, it will help if you don't know how the other parameters are expected.

The script only fills the LAYERS parameter and keeps all the rest as the default, so you can replace the options (the second parameter, wrapped in {}) of processing.run with whatever you filled and copied from the UI.

enter image description here

2
  • 1
    Hey, this worked just fine! But I don't understand your closing paragraph, probably because I don't understand Python. Would you please reword for dummies like me?
    – Stu Smith
    Dec 12, 2023 at 23:20
  • Sure thing, I added an explanation with a screenshot to make myself clearer
    – Dror Bogin
    Dec 13, 2023 at 8:19

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.