3

I would like to use the tool "Union" to union 3 different layers. This tool unfortunately can union only 2 layers so I have to run it twice.

First I run it to union the layers "Layer1" and "Layer2"

param_union = {
    'INPUT' : QgsProcessingFeatureSourceDefinition('X:/HoWGK.gpkg|layername=Layer1', selectedFeaturesOnly=False, featureLimit=-1, flags=QgsProcessingFeatureSourceDefinition.FlagOverrideDefaultGeometryCheck, geometryCheck=QgsFeatureRequest.GeometrySkipInvalid),
    'OUTPUT' : "memory:myUnionLayer1",
    'OVERLAY' : QgsProcessingFeatureSourceDefinition('X:/HoWGK.gpkg|layername=Layer2', selectedFeaturesOnly=False, featureLimit=-1, flags=QgsProcessingFeatureSourceDefinition.FlagOverrideDefaultGeometryCheck, geometryCheck=QgsFeatureRequest.GeometrySkipInvalid),
    'OVERLAY_FIELDS_PREFIX' : '' }
 
union_1 = processing.run("qgis:union", param_union)

Then, I'd like to take the output layer created as temporary/memory layer to union it with a 3rd layer "Layer3"

param_union = {
    'INPUT' : QgsProcessingFeatureSourceDefinition("memory:myUnionLayer1", selectedFeaturesOnly=False, featureLimit=-1, flags=QgsProcessingFeatureSourceDefinition.FlagOverrideDefaultGeometryCheck, geometryCheck=QgsFeatureRequest.GeometrySkipInvalid),
    'OUTPUT' : "memory:myUnionLayer2",
    'OVERLAY' : QgsProcessingFeatureSourceDefinition('X:/HoWGK.gpkg|layername=Layer3', selectedFeaturesOnly=False, featureLimit=-1, flags=QgsProcessingFeatureSourceDefinition.FlagOverrideDefaultGeometryCheck, geometryCheck=QgsFeatureRequest.GeometrySkipInvalid),
    'OVERLAY_FIELDS_PREFIX' : '' }
 
union_2 = processing.run("qgis:union", param_union)

But I got the error message: "Could not load source layer for INPUT: memory:myUnionLayer1 not found"

How can I access my temporary layer to use it in the algorithm?

1
union_1 = processing.run("qgis:union", param_union)

Should be

union_1 = processing.run("qgis:union", param_union)['OUTPUT']

Or union1 is a dictionary, not a layer

1
  • Hi! Thanks, I changed that but it didn't solve the problem...
    – SaskiaG
    Apr 27 '21 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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