0

I need to convert this:

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
import processing


class Modelo(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('inpput', 'input_file.shp', defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('output', 'UF_extraido.shp', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}

        # Extrair por expressão
        alg_params = {
            'EXPRESSION': '\"S_majority\" = 1',
            'INPUT': parameters['input'],
            'OUTPUT': parameters['output']
        }
        outputs['ExtrairPorExpresso'] = processing.run('native:extractbyexpression', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['output'] = outputs['ExtrairPorExpresso']['OUTPUT']
        return results

    def name(self):
        return 'modelo'

    def displayName(self):
        return 'modelo'

    def group(self):
        return ''

    def groupId(self):
        return ''

    def createInstance(self):
        return Modelo()

This is a custom script used in QGIS.

... to something like this:

def extract_by_expression(input_shape_file_path, output_shape_file_path):

    parameters = { 
        'EXPRESSION': '\"S_majority\" = 1',
        'INPUT': QgsProcessingParameterVectorLayer('input', input_shape_file_path, defaultValue=None),
        'OUTPUT': QgsProcessingParameterFeatureSink('output', output_shape_file_path, type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None)
    }

    processing.run('native:extractbyexpression', parameters)

I made this for other algorithms and everything worked fine. But for this specific case, I'm getting this error:

Traceback (most recent call last):
  File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 467, in <module>
  File "<string>", line 460, in extract_by_expression
  File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/tools/general.py", line 108, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
  File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/core/Processing.py", line 168, in runAlgorithm
    raise QgsProcessingException(msg)
_core.QgsProcessingException: Unable to execute algorithm
Could not load source layer for INPUT: invalid value

The input file is OK, it's being used for other algorithms. So I guess this is not a problem with the file itself.

I'm using QGIS 3.18.1-Zürich and the idea is to be able to write my own scripts instead of doing that manually in QGIS interface.

Thanks!

  • Might be just a typo? In `self.addParameter(QgsProcessingParameterVectorLayer('inpput', 'input_file.shp', defaultValue=None))` You wrote in**pp**ut. – Germán Carrillo Jun 21 '21 at 15:54

0 Answers0