[QGIS-Developer] Unload QgsVectorLayer/QgsrasterLayer

Bourdon, Jean-François (DIF) Jean-Francois.Bourdon at mffp.gouv.qc.ca
Wed Aug 12 09:52:19 PDT 2020


Sorry to come back at this, but it doesn't to be fixed in 3.14 either, at least maybe not completely. I upgraded and got the same problem of sticky lock with algorithms native:fixgeometries() and native:buffer() but it doesn't do it with native:polygonfromlayerextent() or qgis:definecurrentprojection() for exemple. Each time, my input is a path to a Shapefile or Geopackage (not previously loaded) with the output being a path to a new file.


Jean-François Bourdon, ing.f.
Analyste en télédétection
Direction des inventaires forestiers
Ministère des Forêts, de la Faune et des Parcs
5700, 4e Avenue Ouest, local A-108
Québec (Québec) G1H 6R1
Téléphone : 418 627-8669, poste 4304
jean-francois.bourdon at mffp.gouv.qc.ca 
mffp.gouv.qc.ca

-----Message d'origine-----
De : Nyall Dawson [mailto:nyall.dawson at gmail.com] 
Envoyé : 11 août 2020 19:11
À : Bourdon, Jean-François (DIF) <Jean-Francois.Bourdon at mffp.gouv.qc.ca>
Cc : qgis-developer at lists.osgeo.org
Objet : Re: [QGIS-Developer] Unload QgsVectorLayer/QgsrasterLayer

On Wed, 12 Aug 2020 at 02:28, Bourdon, Jean-François (DIF) <Jean-Francois.Bourdon at mffp.gouv.qc.ca> wrote:
>
> Thanks a lot Nyall!
> I must say that it's not obvious for a newcomer to see where the problem is and how to fix it. With you pointer, I ended up to one of your answer on GIS SE to learn how to return FlagNoThreading flag. So it closes my issue with the raster file deletion, but it now causes the same problem elsewhere with the deletion of a shapefile.

This one was fixed in recent versions -- either upgrade to 3.14, or downgrade to the most recent LTR. 3.12 is EOLed and didn't get this fix.

Nyall


>
> # Convert Shapefile to Geopackage with error without threading 
> path_SHP = "C:/temp/vector.shp"
> vlayer_SHP = QgsVectorLayer(path_SHP)
> options_GPKG = QgsVectorFileWriter.SaveVectorOptions()
> options_GPKG.driverName = "GPKG"
> options_GPKG.layerName = "streams"
> QgsVectorFileWriter.writeAsVectorFormatV2(vlayer_SHP, 
> "C:/temp/vector_package.gpkg", QgsCoordinateTransformContext(), 
> options_GPKG)
>
> QgsProject.instance().addMapLayer(vlayer_SHP, False)
> QgsProject.instance().removeMapLayer(vlayer_SHP.id())
> QgsVectorFileWriter.deleteShapeFile(path_SHP)
>
>
>
> # Convert Shapefile to Geopackage without error and without threading 
> path_streams_SHP = "C:/temp/vector.shp"
> processing.run("native:package", 
> {'LAYERS':[path_streams_SHP],'OUTPUT':"C:/temp/vector_package.gpkg",'O
> VERWRITE':True,'SAVE_STYLES':False})
> QgsVectorFileWriter.deleteShapeFile(path_streams_SHP)
>
>
>
>
> Jean-François Bourdon, ing.f.
> Analyste en télédétection
> Direction des inventaires forestiers
> Ministère des Forêts, de la Faune et des Parcs 5700, 4e Avenue Ouest, 
> local A-108 Québec (Québec) G1H 6R1 Téléphone : 418 627-8669, poste 
> 4304 jean-francois.bourdon at mffp.gouv.qc.ca
> mffp.gouv.qc.ca
>
> -----Message d'origine-----
> De : Nyall Dawson [mailto:nyall.dawson at gmail.com] Envoyé : 10 août 
> 2020 18:46 À : Bourdon, Jean-François (DIF) 
> <Jean-Francois.Bourdon at mffp.gouv.qc.ca>
> Cc : qgis-developer at lists.osgeo.org
> Objet : Re: [QGIS-Developer] Unload QgsVectorLayer/QgsrasterLayer
>
> On Tue, 11 Aug 2020 at 06:37, Bourdon, Jean-François (DIF) <Jean-Francois.Bourdon at mffp.gouv.qc.ca> wrote:
> >
> > Hi list,
> >
> >
> >
> > I have a very simple problem: I can’t delete a file after loading it with QgsVectorLayer() or QgsRasterLayer() within a script (custom Processing Provider) as I get the error saying the file is still in use. My code to replicate:
> >
> >
> >
> > path_raster = "C:/temp/raster.tif"
> >
> > rlayer = QgsRasterLayer(path_raster, "flow")
> >
> > QgsProject.instance().addMapLayer(rlayer, False)
> >
> > QgsProject.instance().removeMapLayer(rlayer.id())
> >
> > os.remove(path_raster)
> >
> >
> >
> > However, the same code directly put inside the Python Console within QGIS works, but it won’t if used inside a Processing Provider plugin. I also tried using del rlayer without success. There is several questions about releasing QGIS’ lock on files on GIS StackExchange (e.g. here and here) but it doesn’t seem to work for everyone. Maybe it was working in version 2.X, but it fails on 3.12 (Windows 10).
>
> I suspect your algorithm is incorrectly running in a thread. You CAN'T do this if your algorithm directly access the project instance -- it's not thread safe and the layers are likely not getting deleted at all.
> Try returning the FlagNoThreading flag from your algorithm's flags() override.
>
> Nyall
>
>
> >
> >
> >
> > Many thanks
> >
> >
> >
> > Jean-François Bourdon, ing.f.
> >
> > Analyste en télédétection
> > Direction des inventaires forestiers Ministère des Forêts, de la 
> > Faune et des Parcs 5700, 4e Avenue Ouest, local A-108 Québec 
> > (Québec) G1H 6R1 Téléphone : 418 627-8669, poste
> > 4304 jean-francois.bourdon at mffp.gouv.qc.ca
> > mffp.gouv.qc.ca
> >
> >
> >
> > _______________________________________________
> > QGIS-Developer mailing list
> > QGIS-Developer at lists.osgeo.org
> > List info: 
> > https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fli
> > sts.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=02%7C01
> > %7C%7C33604bf340634ac62fc908d83e4bd7d5%7C8705e97737814f4790e1c84c8b8
> > 84da1%7C0%7C0%7C637327842748771566&sdata=1aRkbmsxY9V3jp47oTeLKCP
> > PnURK0PLDs8axqhhraKQ%3D&reserved=0
> > Unsubscribe: 
> > https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fli
> > sts.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=02%7C01
> > %7C%7C33604bf340634ac62fc908d83e4bd7d5%7C8705e97737814f4790e1c84c8b8
> > 84da1%7C0%7C0%7C637327842748771566&sdata=1aRkbmsxY9V3jp47oTeLKCP
> > PnURK0PLDs8axqhhraKQ%3D&reserved=0
>


More information about the QGIS-Developer mailing list