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 *


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')

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')

layer = QgsVectorLayer("c:/Users/LENOVO/Desktop/zones/carte_finale.shp", 'finale','ogr')
  • 1
    Yes, better add the full code ^^ – Clement Jul 10 '17 at 10:26
  • Please use the {} formatting button on your code. The backlick is on intended for individual words. – Vince Jul 10 '17 at 10:30

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 *


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')
| improve this answer | |
  • 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: gis.stackexchange.com/questions/243520/… – pnz Aug 19 '19 at 23:34

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.