7

In QGIS Modeler I want to iterate over vector feature in two layers so that each iteration the inputs are:

  1. Layer 1 feature 1 - Layer 2 feature 1
  2. Layer 1 feature 2 - Layer 2 feature 2
  3. Layer 1 feature 3 - Layer 2 feature 3 and so on

My attempt: I have a simple model which has two Vector Features Inputs and activate "Iterate over this Layer..:" on both inputs.

The result is:

  1. Layer 1 feature 1 - Layer 2 all features
  2. Layer 1 feature 2 - Layer 2 all features ...

Is this possible? enter image description here

3
  • Two layers have same number of features? What if they have different number of features? Commented Oct 29, 2021 at 20:17
  • What is 1 in feature 1, id or an attribute value? Commented Oct 29, 2021 at 20:23
  • It is the feature id. They will have the same number of features
    – Bera
    Commented Oct 30, 2021 at 7:54

1 Answer 1

6

I don't know if it is possible using actual options in Modeler or Processing window, but you can make a script doing that.

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                       QgsFeatureSink,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink)


class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
    INPUT1 = 'INPUT1'
    INPUT2 = 'INPUT2'
    OUTPUT = 'OUTPUT'

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return ExampleProcessingAlgorithm()

    def name(self):
        return 'myscript'

    def displayName(self):
        return self.tr('Custom Difference')

    def group(self):
        return self.tr('Example scripts')

    def groupId(self):
        return 'examplescripts'

    def shortHelpString(self):
        return self.tr("Example algorithm short description")

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT1, self.tr('Layer1')))
        self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT2, self.tr('Layer2')))
        self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Output')))

    def processAlgorithm(self, parameters, context, feedback):
        layer1 = self.parameterAsVectorLayer(parameters, self.INPUT1, context)
        layer2 = self.parameterAsVectorLayer(parameters, self.INPUT2, context)
        
        (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
                                               layer1.fields(), layer1.wkbType(), layer1.sourceCrs())
  
        for i in range(layer1.featureCount()):
            feat1 = layer1.getFeature(i)
            feat2 = layer2.getFeature(i)  
            
            ### main processsing section ###
            geom = feat1.geometry().difference(feat2.geometry())
            feat1.setGeometry(geom)
            ################################

            sink.addFeature(feat1, QgsFeatureSink.FastInsert)

        return {self.OUTPUT: dest_id}

Then use it in QGIS Modeler:

enter image description here

Inputs: (Upper polygons in two layers have the same FID value. Same as lower polygons)

enter image description here

Output:

enter image description here

Your Answer

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

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