loadthemall icon indicating copy to clipboard operation
loadthemall copied to clipboard

Merge layers

Open otofoto opened this issue 1 year ago • 4 comments

Option to merge layers on import if they have same name. Example: Cadgroups.zip

otofoto avatar Oct 24 '23 20:10 otofoto

Nice suggestion. Several ways to do that, though.

For instance, we could create a temporary virtual layer (.vrt) file for all layers sharing a name. Would that make sense for you?

gacarrillor avatar Oct 25 '23 07:10 gacarrillor

No idea if vrt solves this..

But this works by merging in one file only not sure if it works for larger files.

`import os

seen_name = {} # Instanciate a dict, the key will be the name and the value each real path for dirpath, dirnames, filenames in os.walk('C:/Cadgroups/'): for filename in filenames: if '.shp' in filename: if filename in seen_name: seen_name[filename].append(os.path.join(dirpath, filename)) else: seen_name[filename] = [os.path.join(dirpath, filename)]

os.chdir('C:/test')

for unique_name, list_filepath in seen_name.items(): ref_lyr = QgsVectorLayer(list_filepath[0], unique_name, 'ogr') features = []

for nb, unique_file in enumerate(list_filepath[1:]):
    lyr = QgsVectorLayer(unique_file, unique_name+str(nb), 'ogr')
    features.extend([ft for ft in lyr.getFeatures()])

ref_lyr.dataProvider().addFeatures(features)
QgsVectorFileWriter.writeAsVectorFormat(
    ref_lyr, unique_name, "UTF8", ref_lyr.crs(), "ESRI Shapefile")`

otofoto avatar Oct 25 '23 20:10 otofoto

No idea if vrt solves this..

I think it does it in a way. See the result:

image

From there, you can perform analysis, do queries, etc. You could even export the virtual layer to another format.

Perhaps a real merge is too much for a plugin that only searches and loads layers. But the virtual layer fits into that scope, avoiding a number of layers being loaded and showing them instead as a single one in the Layers panel, which would be handy and convenient for some users.

If this feature request fits into the aforementioned description, I think it's doable and would be a nice addition. What do you think?

gacarrillor avatar Oct 26 '23 14:10 gacarrillor

This seems nice to have option. Let's try.

otofoto avatar Oct 26 '23 16:10 otofoto