Friday 24 November 2017

pyqgis - QGIS Model Builder Script - Possible to merge layers in a folder?



I would like the user to select a folder via the interface in which all the vector shapefiles inside that folder would be merged.


I have been using the Model Builder and looking at example scripts and tried to incorporate simple commands into my own script. The following code is incorrect but any guidance would be appreciative:


##Test=name
##input=folder
##merge=output vector

outputs_0=processing.runalg("qgis:mergevectorlayers", input, None)

I have looked at Merge scripts from the Processing and SAGA plugins, but I was wondering if it's possible to do something similar in just one or two lines of code?



Answer




Found a method which helped me and hopefully others:


##Test=name
##Select_Directory=folder
##Merged_Layer=output vector

import os
import glob

input = processing.getObject(Select_Directory)
os.chdir(Select_Directory)

output = glob.glob('./*.shp')
Layer_0 = output[0]

processing.runalg("saga:mergeshapeslayers", Layer_0, ";".join(output), Merged_Layer)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...