1

I'm writing a standalone script containing a series of processing tools. I appended system paths to make it consisten with QGIS consolo.

# -*- coding: utf-8 -*-
import sys
import os
sys.path.append('D:/QGIS 3.4/apps/qgis/python') 
sys.path.append('D:/QGIS 3.4/apps/qgis/python/plugins') 
#sys.path.append('D:QGIS 3.4/apps/qgis/python/qgis')
sys.path.append('D:/QGIS 3.4/bin')
sys.path.append('C:/Users/Administrator/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python')
sys.path.append('C:/Users/Administrator/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python/plugins')

os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'D:/QGIS 3.4/apps/qt5/plugins/'   
os.environ['QT_PLUGIN_PATH'] = r';D:/QGIS 3.4/apps/Qt5/plugins;D:/QGIS 3.4/apps/qgis/qtplugins;D:/QGIS 3.4/apps/qgis/plugins'
os.environ['PATH'] += ';D:/QGIS 3.4/apps/qgis/bin/;D:/QGIS 3.4/apps/qt5/bin/;D:/QGIS 3.4/bin/'

from qgis.core import *

import processing
from processing.core.Processing import Processing

from qgis.analysis import QgsNativeAlgorithms

prefixPath = "D:/QGIS 3.4/apps/qgis/"
QgsApplication.setPrefixPath(prefixPath, True)
qgs = QgsApplication([], False)

# load providers
qgs.initQgis()

Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

areaLayer = QgsVectorLayer(r"E:\data\area_x.shp","area","ogr")
intensityLayer = QgsVectorLayer(r"E:\data\intensity.shp","intensity","ogr")

result1 = processing.run("qgis:clip", 
        {'INPUT':intensityLayer,
        'OVERLAY':areaLayer,
        'OUTPUT':r'E:\data\clip1.shp'})

result2 = processing.run("qgis:clip",
        {'INPUT':r"E:\data\intensity.shp",
        'OVERLAY':r"E:\data\area_x.shp",
        'OUTPUT':r'E:\data\clip2.shp'})

qgs.exitQgis()

It works when I use a QgsVectorLayer instance as input parameter ( result1 ). However, When I change it to the layer path ( result2 ), I always get a exception:

Could not load source layer for INPUT: E:\data\intensity.shp not found. 

enter image description here

It has troubled me for a week. I need to call a number of processing algorithm so I prefer to write commands like result['OUTPUT'] rather than QgsVectorLayer(result['OUTPUT'], 'lyrName','ogr').

Your Answer

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

Browse other questions tagged or ask your own question.