PyQGIS – Saving Temporary Vector Layer After Adding into Group

pyqgispyqt5qgis-processingqgsvectorlayer

This code is not displaying any feature in QGIS , so I want to save the temporary layer so that the features can be visible.

extract1 = processing.run("native:extractbylocation", { 'INPUT': layer1, 'PREDICATE': [0], 
'INTERSECT': QgsProcessingFeatureSourceDefinition(ribbon.id(), selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid), 
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT})['OUTPUT'] 
group.addLayer(extract1) 
QgsProject.instance().mapLayersByName("extract1") extract1.setName('My Layer')

Best Answer

I'm not sure what you mean by "save the temporary layer", because if you want to save the result, don't use a temporary output! But I guess what you actually want is to add the resulting temporary layer to the project, rename it and add it to a group in the layer tree. To do that you can use the code below:

extract1 = processing.run("native:extractbylocation",
                        { 'INPUT': layer1,
                        'PREDICATE': [0],
                        'INTERSECT': QgsProcessingFeatureSourceDefinition(layer2.id(), selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),
                        'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT})['OUTPUT']

# Set the layer name
extract1.setName('My Layer')
# Add the layer to project with parameter addToLegend set to False
QgsProject.instance().addMapLayer(extract1, addToLegend=False)
# Now insert the layer node into an existing group at the desired position (given by integer in first argument)
group.insertLayer(0, extract1)

The code example in the question is incomplete so here is a complete, reproducible example which should be self explanatory to any future readers. You can see in the screenshot how my layer tree is set up. To run this example you would need to only change the layer names.

enter image description here

This example adds the temporary output layer to a group in the layer tree:

layer1 = QgsProject.instance().mapLayersByName('ne_10m_populated_places_simple')[0] # Change layer name
layer2 = QgsProject.instance().mapLayersByName('Aus_state_boundaries')[0] # Change layer name

extract1 = processing.run("native:extractbylocation",
                        { 'INPUT': layer1,
                        'PREDICATE': [0],
                        'INTERSECT': QgsProcessingFeatureSourceDefinition(layer2.id(), selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),
                        'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT})['OUTPUT']

extract1.setName('My Layer')
QgsProject.instance().addMapLayer(extract1, addToLegend=False)
group1 = QgsProject.instance().layerTreeRoot().findGroup('group1')
group1.insertLayer(0, extract1)

The result of this example can be seen below.

enter image description here

This example saves the result to a file location, then loads it into the project and adds it to a group in the layer tree. To run this example you must change both the layer names and the file path to the save location.

layer1 = QgsProject.instance().mapLayersByName('ne_10m_populated_places_simple')[0] # Change layer name
layer2 = QgsProject.instance().mapLayersByName('Aus_state_boundaries')[0] # Change layer name

save_path = '/home/ben/GIS Files/Vector Data/Aus_state_boundaries/selected.gpkg' # Change path

processing.run("native:extractbylocation",
                        { 'INPUT': layer1,
                        'PREDICATE': [0],
                        'INTERSECT': QgsProcessingFeatureSourceDefinition(layer2.id(), selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),
                        'OUTPUT': save_path})

extract1 = QgsVectorLayer(save_path, 'My Layer', 'ogr')
QgsProject.instance().addMapLayer(extract1, addToLegend=False)
group1 = QgsProject.instance().layerTreeRoot().findGroup('group1')
group1.insertLayer(0, extract1)

The result of this example is below.

enter image description here

Related Question