Skip to main content
4 of 5
Rollback to Revision 2
enmapping
  • 522
  • 1
  • 6
  • 17

How to Iterate through files in a folder QGIS, using python

I am using QGIS 3.16, and I am trying to run the “service area from layer” function on approximately 400 layers (200,000 starting points in total), I am also due to be repeating this at a range of distances so looking to use python. Also, I cannot merge together as there are 1,000s km of road and just not feasible to set it running for next month.

I have been trying to run the “service are from layer” function via python and I can get it to accept my road and node layer, which have to have the same name (but are in different folders), and then output this to a different folder. The issue is that the table contains a row for each service area, but there is no geometry written. Below is the code that I have been working on but reading about I can see that there is something relating to FeatureSinks but not sure how I go about including this.

I have tried, but I think my Python skills are just a little too rusty.

My code is below.

import os
import processing
import glob
import string

#Folder where my road networks are stored
Select_folderRoads='//Mac/Home/FINAL/March2021/r1/'
#Folder where the starting points/nodes are stored
Select_folderSDH='//Mac/Home/FINAL/March2021/d1/'
#Folder where the output is to be stored
Save_results='//Mac/Home/FINAL/March2021/path250m/'
os.chdir(Select_folderRoads) 

for lyr in glob.glob("*.sqlite"):
        
        #Individual road network layer
        inlayer = Select_folderRoads +lyr
        #Ouptut filename
        outname = Save_results+lyr
        #Layer containing start points/nodes
        startname = Select_folderSDH+lyr
        inlay = lyr[:-7]
        inlayc = inlay.upper()
        
        testinlayer = str(inlayer+'|'+"layername="+inlayc)

        processing.run("native:serviceareafromlayer", 
            {'LAYERS':  inlayer,
            'DEFAULT_DIRECTION': 2,
            'DEFAULT_SPEED': 50.0,
            'DIRECTION_FIELD': '',
            'INCLUDE_BOUNDS': False,
            'INPUT': testinlayer,
            'OUTPUT_LINES':outname,
            'START_POINTS':startname,
            'STRATEGY': 0,
            'TOLERANCE': 0.0,
            'TRAVEL_COST': 250.0,
            'VALUE_BACKWARD': '',
            'VALUE_BOTH': '',
            'VALUE_FORWARD': ''})
enmapping
  • 522
  • 1
  • 6
  • 17