4

I created a script in which I run numerous algorithms from processing. Each resulting layer is stored in a shp file. I just want to keep the final layer and delete the others, but when I want to erase them while QGIS is still open, they are still in usewhile not shown in the layers list, I can't erase them. This is a problem when I run my script a 2nd time because it doesn't work.

I tried removing the layers with QgsMapLayerRegistry.instance().addMapLayer(layer) QgsMapLayerRegistry.instance().removeMapLayer(layer.id()) but I had no success so far... .prj .qpj and .shx are deleted, only remains .dbf and .shp.

Here is the code

import processing
from qgis.core import *

layer=processing.getObject("carte")
layer.invertSelection()

processing.runalg('qgis:fixeddistancebuffer', layer, "20","5",False, "c:/Users/LENOVO/Desktop/zones/tampon+.shp")
processing.runalg('qgis:dissolve', "c:/Users/LENOVO/Desktop/zones/tampon+.shp", True, None, "c:/Users/LENOVO/Desktop/zones/dissolve.shp")

tampon=QgsVectorLayer("c:/Users/LENOVO/Desktop/zones/tampon+.shp", 'finale','ogr')
QgsMapLayerRegistry.instance().addMapLayer(tampon)
QgsMapLayerRegistry.instance().removeMapLayer(tampon.id())

processing.runalg('qgis:fixeddistancebuffer', "c:/Users/LENOVO/Desktop/zones/dissolve.shp", "-20","5",False, "c:/Users/LENOVO/Desktop/zones/carte_finale.shp")
dissolve=QgsVectorLayer("c:/Users/LENOVO/Desktop/zones/dissolve.shp", 'finale','ogr')

QgsMapLayerRegistry.instance().addMapLayer(dissolve)
QgsMapLayerRegistry.instance().removeMapLayer(dissolve.id())
layer = QgsVectorLayer("c:/Users/LENOVO/Desktop/zones/carte_finale.shp", 'finale','ogr')
QgsMapLayerRegistry.instance().addMapLayer(layer)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Clement
  • 765
  • 1
  • 6
  • 23

1 Answers1

2

Instead of writing an output for each algorithm (and then having to load and remove them), you can instead save them as temporary layers using the None parameter and call these as inputs for any subsequent algorithms. Not tested but the following should hopefully work:

import processing
from qgis.core import *

layer=processing.getObject("carte")
layer.invertSelection()

tampon_plus = processing.runalg('qgis:fixeddistancebuffer', layer, "20","5",False, None)
dissolve = processing.runalg('qgis:dissolve', tampon_plus['OUTPUT'], True, None, None)
carte_finale = processing.runalg('qgis:fixeddistancebuffer', dissolve['OUTPUT'], "-20","5",False, "c:/Users/LENOVO/Desktop/zones/carte_finale.shp")

layer = QgsVectorLayer("c:/Users/LENOVO/Desktop/zones/carte_finale.shp", 'finale','ogr')
QgsMapLayerRegistry.instance().addMapLayer(layer)
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • Well it works perfectly well for my script, but it doesn't answers the issue of files that can't be deleted whereas they are no longer in layer list... I will use your way from now, Thanks ! – Clement Jul 10 '17 at 10:46
  • @Clement the best way for saying thanks here is accepting the answer (if it solved your issue or if it was the most useful for finding a solution). – mgri Jul 10 '17 at 10:49
  • @Clement - Most welcome! I have had trouble with similar problems in the past. Sometimes using del layer works after removing it from registry and then deleting the associated files like .dbf and .shp, other times it doesn't :) – Joseph Jul 10 '17 at 10:49
  • If you still have the problem, try my answer here: https://gis.stackexchange.com/questions/243520/deleting-raster-file-after-doing-some-processing-in-qgis/332442#332442 – pnz1337 Aug 19 '19 at 23:34