1

In QGIS can I create a model in the Graphical Modeler that does the equivalent of the following manual steps if I were just using menus/toolbars? If I can't via the Modeler is there another way in QGIS or even a different tool like GDAL tools to automate this?

  1. [Assume I've opened a shapefile/layer]
  2. Toggle Editing on
  3. Edit->Select->Select All Features
  4. Edit->Edit Geometry->Merge Selected Features
  5. Take attributes from first feature

I have a large number of shapefiles. Each one contains up to thousands of features, each a vertical slice of the whole as shown in the first image below. I'd like to automate the above steps so I can convert each shapefile such that it contains a single feature combining all the original features as shown in the second image. All the features within a shapefile have the same attributes. I do not want to combine shapefiles, simply combine the features within a shapefile.

sample vertical features merged features

2
  • 2
    Try using Dissolve from the processing toolbox and run as batch process.
    – Ben W
    Jul 13, 2022 at 23:21
  • Or merge all shapefiles then aggregate
    – BERA
    Jul 14, 2022 at 8:51

1 Answer 1

1

Since you are merging all the features of your shapefiles, you can use Dissolve instead + a for loop with PyQGIS to dissolve all your shapefiles.

import glob, os, processing
    
#Put here the paths of your input and output folders   
inputFolder = r"C:\\Users\\_M92\\Desktop\\Jeff_S\\OriginalShapefiles"
outputFolder = r"C:\\Users\\_M92\\Desktop\\Jeff_S\\DissolvedShapefiles\\"
    
os.chdir(inputFolder)
    
for lyr in glob.glob("*.shp"):
    processing.run("native:dissolve", {'INPUT':lyr, 'OUTPUT': outputFolder + 'Dissolved_' + lyr})
    iface.addVectorLayer(outputFolder + 'Dissolved_' + lyr, 'Dissolved_' + lyr, 'ogr')

Note : The Dissolve algorithm pick the attributes of the first feature (of the original shapefile) and assigns it to the feature of the dissolved shapefile, automatically.

1
  • That worked beautifully! Thanks!
    – Jeff S.
    Jul 14, 2022 at 14:27

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.