0

I am working on a script which takes shapefiles from one folder called "Original files", process them and then output the results into another folder named "Processed files. I asked a question on how this can be done on stackoverflow (which includes a diagram) and received a detailed answer. I'm now trying to incorporate that answer into the script but unfortunately no shapefiles are being saved into the Processed files folders.

from os.path import expanduser
home = expanduser("~")

root_dir = home + "\Test\\"
original_dir = 'Original files'
processed_dir = 'Processed files'

outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405', None)

for path, subdirs, files in os.walk(root_dir):
    if original_dir in path:
        for file in files:
            if file.endswith('.shp'):
                outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], file, os.path.sep.join(path.split(os.path.sep)[:-1]) + os.path.sep + processed_dir + os.path.sep + file)

Am I missing an obvious step?

  • 2
    To start, I would recommend splitting up all of the path creation that you do within the runalg() function into smaller pieces. Do it all with new variables so you can print them before passing them to the function, that might reveal some issues... – mr.adam Mar 30 '15 at 13:58
  • @mr.adam - Thanks buddy, I will try that and see if it helps. – Joseph Mar 30 '15 at 14:00
3

Try changing this line:

outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], file, os.path.sep.join(path.split(os.path.sep)[:-1]) + os.path.sep + processed_dir + os.path.sep + file)

to:

outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], file, os.path.join(os.path.dirname(path), processed_dir, file))
  • Many thanks for this answer. I think it might have solved that problem. Unfortunately, I'm getting a Wrong parameter value...don't see it yet but playing around with the script to see where this error is. Don't suppose you have any ideas about this? – Joseph Mar 30 '15 at 14:13
  • No problem. Unfortunately, I cannot help you there. I am not familiar with qgis. I just noticed the issue at the end with how the output path was being constructed. Hopefully someone who is familiar with this plug in can chime in. – crmackey Mar 30 '15 at 14:16
  • Don't worry about it, I do think this error is on my side so I will accept your answer soon if I can't find the source of the problem. – Joseph Mar 30 '15 at 14:19
  • 1
    Your answer does indeed solve the problem as I tested it on another script :). Still haven't figured out the error yet though, will see how it goes before I post another question. Anyway, many thanks again! – Joseph Mar 30 '15 at 14:48
  • 1
    Good to know, glad I was able to help! – crmackey Mar 30 '15 at 15:16

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.