2

I am trying to run processing algorithms in Python code editor inside QGIS. Here is my code:

zones = QgsVectorLayer('C:/Users/pamidiashoka/Downloads/OP_Pen/boundaries.shp', 'zone boundaries', 'ogr')

points = QgsVectorLayer('C:/Users/pamidiashoka/Downloads/OP_Pen/666_KAMARLE_Adharne/666_KAMARLE_Adharne.shp', 'zone boundaries', 'ogr')

extr = processing.runAndLoadResults('qgis:extractbyexpression', {'INPUT': points,
                    'EXPRESSION':f"filename = '2A_2_Village_Internal_Road_1'",
                    'OUTPUT': 'TEMPORARY_OUTPUT'})
        
cnt = processing.runAndLoadResults('qgis:extractbylocation', {'INPUT':zones,
                    'PREDICATE': 0,
                    'INTERSECT' : extr['OUTPUT'],
                    'OUTPUT': 'TEMPORARY_OUTPUT'})

The first algorithm is giving proper output and adding a new layer to the panel but the second one is throwing an error. Here is the error I am getting

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 13, in <module>
  File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\tools\general.py", line 151, in runAndLoadResults
    return Processing.runAlgorithm(alg, parameters=parameters, onFinish=handleAlgorithmResults, feedback=feedback,
  File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\core\Processing.py", line 187, in runAlgorithm
    ret, results = execute(alg, parameters, context, feedback, catch_exceptions=False)
  File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\gui\AlgorithmExecutor.py", line 72, in execute
    results, ok = alg.run(parameters, context, feedback, {}, False)
_core.QgsProcessingException: Could not create memory layer

What is the cause of this error and how can I resolve this issue?

2 Answers 2

0

you should try this:

cnt = processing.runAndLoadResults('qgis:extractbylocation', {'INPUT':zones,
                    'PREDICATE': 0,
                    'INTERSECT' : extr,
                    'OUTPUT': 'TEMPORARY_OUTPUT'})
1
  • It has to be extr['OUTPUT'], otherwise the following exception will arise: Could not load source layer for INTERSECT: invalid value Commented Jun 25 at 9:07
0

I could get the same error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "Untitled-1", line 9, in <module>
  File "C:\PROGRA~1/QGIS34~1.2/apps/qgis/./python/plugins\processing\tools\general.py", line 156, in runAndLoadResults
    return Processing.runAlgorithm(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\PROGRA~1/QGIS34~1.2/apps/qgis/./python/plugins\processing\core\Processing.py", line 214, in runAlgorithm
    ret, results = execute(
                   ^^^^^^^^
  File "C:\PROGRA~1/QGIS34~1.2/apps/qgis/./python/plugins\processing\gui\AlgorithmExecutor.py", line 74, in execute
    results, ok = alg.run(parameters, context, feedback, {}, False)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_core.QgsProcessingException: Could not create memory layer

after executing this piece of code:

zones = QgsVectorLayer('C:/Users/pamidiashoka/Downloads/OP_Pen/boundaries.shp', 'zone boundaries', 'ogr')

params = {
    'INPUT' : zones, 
    'EXPRESSION' : f"filename = '2A_2_Village_Internal_Road_1'",
    'OUTPUT' : 'TEMPORARY_OUTPUT'
    }
    
result = processing.runAndLoadResults("qgis:extractbyexpression", params)['OUTPUT']

This is happening due to the invalid 'zone boundaries' layer, which I obviously do not have on my PC. However, in your case it could be a wrong source path. Therefore, it is suggested to check the layer's validity before applying any geoalgorithm with the .isValid() method of the QgsMapLayer class:

if zones.isValid() and points.isValid():
   ...

Also, one could improve their code with nesting the Python's try/except-block.


References:

Your Answer

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.