3

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?

4

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)
  • 1
    Looks good, I will try it as soon as I could. Thanks a lot so far! – Miron Mar 15 '16 at 15:38
  • @Miron - Most welcome, hope it will help :) – Joseph Mar 15 '16 at 15:39
  • do you know (or does someone know) how to save the output as separated files? Let’s say my folder contains four shape files I want to convert to raster, how do I save each single one? – Miron Apr 1 '16 at 10:37
  • @Miron - I think I gave an answer which did something similar here. The key parts are assigning the paths of the shapefiles and results, seach for the shapefiles using glob.glob(), then add the path of the result in the output algorithm. – Joseph Apr 1 '16 at 11:00
  • @Miron - See my edited post :) – Joseph Apr 1 '16 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.