[GIS] Load multiple files from folder for further processing

pyqgisqgis-processing

I'm working on a processing model and I'm looking for a possibility to load multiple shape-files from an Input-folder for further processing (first clip them all to a certain extend, then assign them to variables for further processes etc.) with a python script.

The problem is that the number of files in the Input-folder can vary from model-run to model-run. So far, the user has to define a certain number of shapefiles as Input.

Any Ideas about that?

Best Answer

You could use something like the following in your script which finds all shapefiles in a selected folder and for each shapefile, applies a processing function:

##Example=name
##Select_folder=Folder

import glob

for layers in glob.glob( Select_folder + "/" + "*.shp" ):
    # Do some processing

EDIT:

In response to the comments, you can process and output individual shapefiles by using similar code below (I used the buffer algorithm as an example):

##Example=name
##Select_directory=Folder
##Save_results=Folder

import glob, os, processing

os.chdir(Select_directory)
for layers in glob.glob( "*.shp" ):
    processing.runalg("qgis:fixeddistancebuffer", layers, 10, 10, False, Save_results + "/" + layers)
Related Question