2

[QGIS 3.10] I have two processes I have created using the graphic modeler. I'm trying to create a script that would run one process or another depending on the inputs. But I'm getting an error in line 30 regarding the process.

1) Is this because it is not recognising the models I've created? any idea how can I solve this?

2) Also, any idea how can I restrict the Vector type of the Inputs?

NameError: name 'processing' is not defined

EDIT: As @Kadir suggested, I added import processing at the beginning and it seems to be working. I'm still not sure how to restrict the Vector_Layer to points or lines when using @alg decorator, in case someone can help. Else, I'm closing this question.

from qgis import processing
from qgis.processing import alg
from qgis.core import QgsFeature, QgsWkbTypes, QgsField, QgsFields, QgsPointXY, QgsGeometry, \
QgsCoordinateReferenceSystem, QgsProcessingMultiStepFeedback, QgsVectorFileWriter, QgsVectorLayer, \
QgsProcessing,QgsProcessingAlgorithm,QgsProcessingParameterFeatureSink
from PyQt5.QtCore import QVariant

@alg(name="Test if", label=alg.tr("Test if"), group="scripts", group_label=alg.tr("Example Scripts"))
@alg.input(type=alg.VECTOR_LAYER , name="LINES", label="Interpretation Lines", optional=False)
@alg.input(type=alg.VECTOR_LAYER , name="POINTS", label="Interpretation Points", optional=False)
@alg.input(type=alg.VECTOR_LAYER , name="KP", label="KP", optional=False)
@alg.input(type=alg.VECTOR_LAYER , name="DOB", label="DOB", optional=True)
@alg.input(type=alg.VECTOR_LAYER_DEST, name="OUTPUT", label="Output layer")

def testalg(instance, parameters, context, feedback, inputs):
    """
    Description goes here. (Don't delete this! Removing this comment will cause errors.)
    """        
    feedback = QgsProcessingMultiStepFeedback(1, feedback)
    results = {}
    outputs = {}

    if parameters['DOB'] is None:
       # PINS_no_DOB
        alg_params = {
            'interpretationlines': parameters['LINES'],
            'interpretationpoints': parameters['POINTS'],
            'kppoints': parameters['KP'],
            'qgis:refactorfields_1:test': parameters['OUTPUT']
        }
        outputs['OUTPUT'] = processing.run('model:FGBML_PINS_no_DOB', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['OUTPUT'] = outputs['OUTPUT']['qgis:refactorfields_1:test']
    else:
        # PINS_&_DOB
        alg_params = {
            'dob': parameters['DOB'],
            'interpretationlines': parameters['LINES'],
            'interpretationpoints': parameters['POINTS'],
            'kppoints': parameters['KP'],
            'qgis:refactorfields_1:test': parameters['OUTPUT']
        }
        outputs['OUTPUT'] = processing.run('model:FGBML_PINS_&_DOB', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['OUTPUT'] = outputs['OUTPUT']['qgis:refactorfields_1:test']


    return results
1
  • 2
    Just add import processing to import section. Jan 17, 2020 at 20:21

1 Answer 1

1

As @KadirSahbaz suggested I added import processing at the beginning. The code below works.

from qgis import processing
from qgis.processing import alg
from qgis.core import QgsFeature, QgsWkbTypes, QgsField, QgsFields, QgsPointXY, QgsGeometry, \
QgsCoordinateReferenceSystem, QgsProcessingMultiStepFeedback, QgsVectorFileWriter, QgsVectorLayer, \
QgsProcessing,QgsProcessingAlgorithm,QgsProcessingParameterFeatureSink
from PyQt5.QtCore import QVariant

@alg(name="Test if", label=alg.tr("Test if"), group="scripts", group_label=alg.tr("Example Scripts"))
@alg.input(type=alg.VECTOR_LAYER , name="LINES", label="Interpretation Lines", optional=False)
@alg.input(type=alg.VECTOR_LAYER , name="POINTS", label="Interpretation Points", optional=False)
@alg.input(type=alg.VECTOR_LAYER , name="KP", label="KP", optional=False)
@alg.input(type=alg.VECTOR_LAYER , name="DOB", label="DOB", optional=True)
@alg.input(type=alg.VECTOR_LAYER_DEST, name="OUTPUT", label="Output layer")

def testalg(instance, parameters, context, feedback, inputs):
    """
    Description goes here. (Don't delete this! Removing this comment will cause errors.)
    """        
    feedback = QgsProcessingMultiStepFeedback(1, feedback)
    results = {}
    outputs = {}

    if parameters['DOB'] is None:
       # PINS_no_DOB
        alg_params = {
            'interpretationlines': parameters['LINES'],
            'interpretationpoints': parameters['POINTS'],
            'kppoints': parameters['KP'],
            'qgis:refactorfields_1:test': parameters['OUTPUT']
        }
        outputs['OUTPUT'] = processing.run('model:FGBML_PINS_no_DOB', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['OUTPUT'] = outputs['OUTPUT']['qgis:refactorfields_1:test']
    else:
        # PINS_&_DOB
        alg_params = {
            'dob': parameters['DOB'],
            'interpretationlines': parameters['LINES'],
            'interpretationpoints': parameters['POINTS'],
            'kppoints': parameters['KP'],
            'qgis:refactorfields_1:test': parameters['OUTPUT']
        }
        outputs['OUTPUT'] = processing.run('model:FGBML_PINS_&_DOB', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['OUTPUT'] = outputs['OUTPUT']['qgis:refactorfields_1:test']


    return results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.