Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge layers #40

Open
otofoto opened this issue Oct 24, 2023 · 4 comments
Open

Merge layers #40

otofoto opened this issue Oct 24, 2023 · 4 comments
Labels
Feature request Validated The feature request has been accepted and, hopefully, it'll be included in a next release!

Comments

@otofoto
Copy link

otofoto commented Oct 24, 2023

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

@gacarrillor
Copy link
Owner

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 gacarrillor added Feature request Feedback required We are waiting for you... labels Oct 25, 2023
@otofoto
Copy link
Author

otofoto commented Oct 25, 2023

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")`

@gacarrillor
Copy link
Owner

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?

@otofoto
Copy link
Author

otofoto commented Oct 26, 2023

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

@gacarrillor gacarrillor added Validated The feature request has been accepted and, hopefully, it'll be included in a next release! and removed Feedback required We are waiting for you... labels Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature request Validated The feature request has been accepted and, hopefully, it'll be included in a next release!
Projects
None yet
Development

No branches or pull requests

2 participants