loadthemall
loadthemall copied to clipboard
Merge layers
Option to merge layers on import if they have same name. Example: Cadgroups.zip
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?
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")`
No idea if vrt solves this..
I think it does it in a way. See the result:
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?
This seems nice to have option. Let's try.