5

Very simple piece of code. I'm trying to run this as a stand alone Python script, not in the console. Keeps giving the error "Could not load source layer for INPUT", shape not found. But the os.path.isfile commands shows that the shapefile does exist. Driving me crazy this one problem. Been looking for solution for 2 days.

import sys, os
sys.path.extend([r'E:\sw_nt\QGIS_3.4\apps\qgis\python',r'E:\sw_nt\QGIS_3.4\apps\Python37\Lib\site-packages'])
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'E:\sw_nt\QGIS_3.4\apps\Qt5\plugins'
os.environ['PATH'] += r';E:\sw_nt\QGIS_3.4\apps\qgis\bin;E:\sw_nt\QGIS_3.4\apps\Qt5\bin'
sys.path.append(r'E:\sw_nt\QGIS_3.4\apps\qgis\python')
sys.path.append(r'E:\sw_nt\QGIS_3.4\apps\qgis\python\plugins')
sys.path.append(r'E:\sw_nt\QGIS_3.4\apps\qgis\python\plugins\processing')
from qgis.core import (
 QgsApplication, 
 QgsProcessingFeedback, 
 QgsVectorLayer
)
from qgis.analysis import QgsNativeAlgorithms
QgsApplication.setPrefixPath('/usr', True)
qgs = QgsApplication([], False)
qgs.initQgis()
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
feedback = QgsProcessingFeedback()
rivers = r'W:\srm\wml\Workarea\mamcgirr\QGIS_learning_projects\ne_10m_rivers_lake_centerlines.shp'
output = r'W:\srm\wml\Workarea\mamcgirr\QGIS_learning_projects\bcgw_test_data_as_shapes\clean_polys\danube3.shp'
expression = "name LIKE '%Danube%'"
print (os.path.isfile(rivers))
danube = processing.run(
 'native:extractbyexpression',
 {'INPUT': rivers, 'EXPRESSION': expression, 'OUTPUT': output},
 feedback=feedback
 )['OUTPUT']
print(danube)
MrXsquared
36.2k22 gold badges76 silver badges127 bronze badges
asked Apr 11, 2019 at 23:03
3
  • Have you tried using forward slashes instead of back slash in the path? Commented Apr 12, 2019 at 6:47
  • I tired this. Exact same result. Commented Apr 15, 2019 at 14:33
  • Perhaps there is some library not being loaded. I tried loading the 'modified script' another person submitted, but it looked exactly the same as the script I loaded. Commented Apr 15, 2019 at 17:36

2 Answers 2

2

I met the same problem as You. I'm new to Python, but I found it worked to use a QgsVecotrLayer instance instead of the layer path. Change the defination of rivers to

rivers = QgsVectorLayer("r'W:\srm\wml\Workarea\mamcgirr\QGIS_learning_projects\ne_10m_rivers_lake_centerlines.shp'","rivers","ogr")

By the way, if you get the direct solution please share with me.

answered May 1, 2019 at 8:47
0

I found the solution. My mistake was that after adding the raster or vector layer when I was planning to use it for further process, I selected it from the drop-down list, while you should change the state to model input and then select the layer. When this message appears, if you look at the model flowchart, the source layer will not be connected to the next component. While if you do as I aforementioned, the problem would be solved. I was engaged by this problem and error for many days and now I found my mistake. Look at the attached pictures.

enter image description here

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Apr 7, 2023 at 21:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.