6

I wrote a progressing algorithm using Python. The tool uses multiple other processing tools in the toolbox and creates temporary outputs. Those outputs seem somehow saved while I was running the script while developing. Now, when I call my tool from the toolbox and want to select layer as an input, they appear in the selection even though they are created a while ago and they are not even loaded with QGIS Project that I am working.

enter image description here

This is how the input parameter in my script

self.addParameter(
    QgsProcessingParameterVectorLayer(
        self.GEOLOGIE_LAYER,
        self.tr('Pfad zur geologischen Karte'),
        defaultValue=0,
        optional=False
    )
)

Any idea how I can get rid of them?

I think this part of the script is producing those temporary layers

res2 = processing.run("native:clip", {
'INPUT': vlyr_ger,
'OVERLAY': vlyr_ezgStats,
'OUTPUT': 'TEMPORARY_OUTPUT'})
vlyr_gerClp = res2['OUTPUT']
vlyr_gerClp.setName('vlyr_gerClp')
removeBinaryFields(vlyr_gerClp)
lyrList.append(vlyr_gerClp)
7
  • 1
    Are these layers still in the QGIS document's table of contents? If so then you will have to remove them from there and then they should not be selectable options anymore.
    – wfgeo
    Commented Jun 8, 2020 at 16:11
  • 1
    No, they are not in the TOC. That's why it surprises me to see them appear as selectable options.
    – Nil
    Commented Jun 8, 2020 at 16:14
  • 1
    Probably your script or other algorithms are adding layers to QGIS layer registry (and not to the Layers panel). We could fix it in the script if you're willing to share it (or simply, if you call QgsProject.instance().addMapLayer(my_layer, False), just remove that False) or if you're fine, we can also remove them from the project, after the script is executed, with a couple of lines. Which solution fits best for your use case? Commented Jun 8, 2020 at 16:37
  • QGIS Version is 3.10.4! The script is a bit long. In the script, I am calling many processing tools in my script and I saved them as intermediate results. I think the code block, that I added into the question, is causing the problem. Because the only the temporary output of this part keep staying in the QGIS Project
    – Nil
    Commented Jun 9, 2020 at 7:07
  • Thx. I tried but the layers were still selectable. Eventually, I created a new qgis project. I shall see if those temporary layers will be generated again.
    – Nil
    Commented Jun 9, 2020 at 8:37

1 Answer 1

6

If you change .QGZ file's extension as .ZIP and extract .QGS file in it and open that .QGS file in a text editor, you will see XML-like content. You will see names of layers which exist in QGIS layer tree, and their ids. I guess, in the following lines, you will see extra ids which don't exist in QGIS layer tree but exist somehow in .QGS file. When you want to choose an input layer in a processing tool, you see those layer. (I guess, not sure 100%).

After I deleted some layers in .QGS file and opened it in QGIS and run a processing tool, I encountered the same problem. Although layers returned by other tools is not added to the layer tree, somehow their ids must be remaining in the project file.

For a temporary solution to get rid of those names, use the following script. It removes layers which don't exist in layer tree.

p = QgsProject.instance()

tree_layers = [l.layer() for l in p.layerTreeRoot().children()]
map_layers = p.mapLayers().values()

for l in map_layers:
    if not l in tree_layers:
        p.removeMapLayer(l.id())
3
  • 1
    del b ( del clipped, del vlyr_gerClp in my case) allows me only to delete the ones that my script newly created. However, my qgis project still shows the ones from previous sessions. I have deleted my temporary files in the temp folder but still cannot get rid of :/ Maybe I should just create a new QGIS Project -_-
    – Nil
    Commented Jun 9, 2020 at 7:14
  • that's correct. Unfortunately, it did not solve the problem either.
    – Nil
    Commented Jun 15, 2020 at 14:37
  • The above script doesn't take into account if your layer tree contains layer groups and will produce a traceback if your tree contains layer groups. But it does do what I needed, iill see if i can write a check on the layer group object type and ill post here.
    – nr_aus
    Commented Sep 29, 2020 at 3:25

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.