1

I am new to qgis python scripting. QGIS always randomly crashes when I am trying to run the script as a processing algorithm.

Here is my code:

class OverlayCheckProcessingAlgorithm(QgsProcessingAlgorithm):

OUTPUT = 'OUTPUT'INPUTS = 'INPUTS'


def tr(self, string):
    """
    Returns a translatable string with the self.tr() function.
    """
    return QCoreApplication.translate('Processing', string)

def createInstance(self):
    return OverlayCheckProcessingAlgorithm()

def name(self):
    """
    Returns the algorithm name, used for identifying the algorithm. This
    string should be fixed for the algorithm, and must not be localised.
    The name should be unique within each provider. Names should contain
    lowercase alphanumeric characters only and no spaces or other
    formatting characters.
    """
    return 'overlaycheck'

def displayName(self):
    """
    Returns the translated algorithm name, which should be used for any
    user-visible display of the algorithm name.
    """
    return self.tr('Overlay Check')

def group(self):
    """
    Returns the name of the group this algorithm belongs to. This string
    should be localised.
    """
    return self.tr('scripts')

def groupId(self):
    """
    Returns the unique ID of the group this algorithm belongs to. This
    string should be fixed for the algorithm, and must not be localised.
    The group id should be unique within each provider. Group id should
    contain lowercase alphanumeric characters only and no spaces or other
    formatting characters.
    """
    return 'scripts'

def shortHelpString(self):
    """
    Returns a localised short helper string for the algorithm. This string
    should provide a basic description about what the algorithm does and the
    parameters and outputs associated with it..
    """
    return self.tr("Example algorithm short description")

def initAlgorithm(self, config=None):
    """
    Here we define the inputs and output of the algorithm, along
    with some other properties.
    """
    # We add the input vector features source. It can have any kind of
    # geometry.
    self.addParameter(
        QgsProcessingParameterMultipleLayers(
            self.INPUTS,
            self.tr('Input layers'),
           QgsProcessing.TypeVectorAnyGeometry
        )
    )

    # We add a feature sink in which to store our processed features (this
    # usually takes the form of a newly created vector layer when the
    # algorithm is run in QGIS).
    self.addParameter(
        QgsProcessingParameterFeatureSink(
            self.OUTPUT,
            self.tr('Output layer'),

        )
    )

def processAlgorithm(self, parameters, context, feedback):
    """
    Here is where the processing itself takes place.
    """
    # Retrieve the feature source and sink. The 'dest_id' variable is used
    # to uniquely identify the feature sink, and must be included in the
    # dictionary returned by the processAlgorithm function.


    algresult = processing.run("qgis:mergevectorlayers",
        {'LAYERS': parameters[self.INPUTS],
         'CRS':'EPSG:3175',
         'OUTPUT': parameters[self.OUTPUT]},
         context = context, feedback = feedback)

    l =  QgsProcessingUtils.mapLayerFromString(algresult['OUTPUT'],context,True)

    l_prov = l.dataProvider()
    l_prov.addAttributes([QgsField("Overlay_Count", QVariant.Int)] )
    l.updateFields()

# get the number of existing attributes -> ac
    fields = l_prov .fields()
    ac = fields.count()


    feats = l.getFeatures(QgsFeatureRequest())

    count = []



    for f in feats:
        l.selectByIds([f.id()])
        j = processing.run('native:saveselectedfeatures',{'INPUT':l,'OUTPUT':'memory:target_line'})
        processing.run('native:selectbylocation', {'INPUT':l,'PREDICATE':[5,6],'INTERSECT':j['OUTPUT'],'METHOD':0})
        s = processing.run('native:saveselectedfeatures',{'INPUT':l,'OUTPUT':'memory:selection'})
        t = len(list(s['OUTPUT'].getFeatures()))

#update values to column 'ac - 1'
        r = {f.id():{ac-1:t-1}}
        count.append(r)


    for i in count:
        l_prov.changeAttributeValues(i)



    return {self.OUTPUT: l}

I guess the problem is not properly calling (sink, dest_id) = self.parameterAsSink(), but I have no idea where should I put it in.

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.