Skip to main content
2 of 3
review code, iterate over many SQLite (not spatialite DB), set CRS
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65

I tried to create a working Python code and I considered that you have many SQLite DBs with just one layer (same name as the DB file in lowercase) in your folders r1 and d1.

Here is my working code under QGIS 3.16 (Windows, but I took your question OSX paths for the code 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/'
# Set working directory to roads folder
os.chdir(Select_folderRoads)

uk_crs = QgsCoordinateReferenceSystem(27700)

# iterate over roads DB network layers
for dbname in glob.glob("*.sqlite"):
    # get layer name from SQLite DB name
    lyr_name = dbname.split(".sqlite")[0].lower()
    # get roads and points layers, set their CRS
    roads_layer = QgsVectorLayer(f"{Select_folderRoads}{dbname}|layername={lyr_name}", "roads_lyr", "ogr")
    roads_layer.setCrs(uk_crs)
    points_layer = QgsVectorLayer(f"{Select_folderSDH}{dbname}|layername={lyr_name}", "points_lyr", "ogr")
    points_layer.setCrs(uk_crs)

    # processing algorithm that output result as GPKG
    result = processing.run(
        "native:serviceareafromlayer",
        {'DEFAULT_DIRECTION': 2,
        'DEFAULT_SPEED': 50.0,
        'DIRECTION_FIELD': '',
        'INCLUDE_BOUNDS': False,
        'INPUT': roads_layer,
        'OUTPUT_LINES': f"{Save_results}{lyr_name}",
        'START_POINTS': points_layer,
        'STRATEGY': 0,
        'TOLERANCE': 0.0,
        'TRAVEL_COST': 250.0,
        'VALUE_BACKWARD': '',
        'VALUE_BOTH': '',
        'VALUE_FORWARD': ''}
    )
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65