QGIS – How to Merge Layers of Certain Geometry Type Only

geometry-data-typekmzmergeqgisvector

I have nearly a thousand layers of geospatial data consisting only of Point layers and Line Layers and I just want to make it so that I only have one layer for each type.

Is there a way to filter to only run the Data Management Tools > Merge Vector Layers on just one type of layer at a time (e.g. Points only first and then Layer)?

Or at least on how to load only one of them at a time into QGIS? The data is from a (single) KMZ file.

enter image description here

Best Answer

You can use PyQGIS:

layerlist = []
for lyr in QgsProject().instance().mapLayers().values(): #For every layer added to the map
    if lyr.geometryType()==1: #1 is line, 2 polygon, 0 point
        layerlist.append(lyr) #If the geometrytype is 1, then append the layer to layerlist

processing.runAndLoadResults("native:mergevectorlayers", {'LAYERS':layerlist,'CRS':None,'OUTPUT':'TEMPORARY_OUTPUT'})

enter image description here

And if you want to you can automate the merging by grouping the geometry types using collections.defaultdict(list):

from collections import defaultdict as dd

list_them = dd(list)

numtoname = {0:'points', 1:'lines', 2:'polygons'}
for lyr in QgsProject().instance().mapLayers().values():
    list_them[numtoname[lyr.geometryType()]].append(lyr)
    
for geomtype, layerlist in list_them.items():
    print(geomtype)
    print(layerlist)
    processing.runAndLoadResults("native:mergevectorlayers", {'LAYERS':layerlist,'CRS':None,'OUTPUT':'TEMPORARY_OUTPUT'})

enter image description here

Related Question