<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Exchange Server">
<!-- converted from rtf -->
<style><!-- .EmailQuote { margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; } --></style>
</head>
<body>
<font face="Times New Roman" size="3"><span style="font-size:12pt;"><a name="_MailEndCompose"></a>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Thanks a lot Nyall!</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">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 <a href="https://gis.stackexchange.com/a/312884/109610"><font color="#0563C1"><u>one
of your</u></font><font color="#0563C1"><u> answer on GIS SE</u></font></a> 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. It's
like if <font face="Courier New" size="2"><span style="font-size:10pt;">QgsVectorFileWriter</span></font> is now keeping a lock on the shapefile (if a remove this line, the shapefile gets deleted). However, if I use the <i>Package layers</i> tool, I can delete
the shapefile without any itch.</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;"># Convert Shapefile to Geopackage with error without threading</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">path_SHP = "C:/temp/vector.shp"</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">vlayer_SHP = QgsVectorLayer(path_SHP)</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">options_GPKG = QgsVectorFileWriter.SaveVectorOptions()</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">options_GPKG.driverName = "GPKG"</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">options_GPKG.layerName = "streams"</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">QgsVectorFileWriter.writeAsVectorFormatV2(vlayer_SHP, "C:/temp/vector_package.gpkg", QgsCoordinateTransformContext(), options_GPKG)</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;"> </span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">QgsProject.instance().addMapLayer(vlayer_SHP, False)</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">QgsProject.instance().removeMapLayer(vlayer_SHP.id())</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">QgsVectorFileWriter.deleteShapeFile(path_SHP)</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;"> </span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;"> </span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;"> </span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;"># Convert Shapefile to Geopackage without error and without threading</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">path_streams_SHP = "C:/temp/vector.shp"</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">processing.run("native:package", {'LAYERS':[path_streams_SHP],'OUTPUT':"C:/temp/vector_package.gpkg",'OVERWRITE':True,'SAVE_STYLES':False})</span></font></div>
<div><font face="Courier New" size="2"><span style="font-size:10pt;">QgsVectorFileWriter.deleteShapeFile(path_streams_SHP)</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Jean-François Bourdon, ing.f.</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Analyste en télédétection</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Direction des inventaires forestiers</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Ministère des Forêts, de la Faune et des Parcs</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">5700, 4e Avenue Ouest, local A-108</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Québec (Québec) G1H 6R1</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Téléphone : 418 627-8669, poste 4304</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">jean-francois.bourdon@mffp.gouv.qc.ca </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">mffp.gouv.qc.ca</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">-----Message d'origine-----<br>
De : Nyall Dawson [<a href="mailto:nyall.dawson@gmail.com">mailto:nyall.dawson@gmail.com</a>]
<br>
Envoyé : 10 août 2020 18:46<br>
À : Bourdon, Jean-François (DIF) <Jean-Francois.Bourdon@mffp.gouv.qc.ca><br>
Cc : qgis-developer@lists.osgeo.org<br>
Objet : Re: [QGIS-Developer] Unload QgsVectorLayer/QgsrasterLayer</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">On Tue, 11 Aug 2020 at 06:37, Bourdon, Jean-François (DIF) <<a href="mailto:Jean-Francois.Bourdon@mffp.gouv.qc.ca">Jean-Francois.Bourdon@mffp.gouv.qc.ca</a>> wrote:</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> Hi list,</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> 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:</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> path_raster = "C:/temp/raster.tif"</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> rlayer = QgsRasterLayer(path_raster, "flow")</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> QgsProject.instance().addMapLayer(rlayer, False)</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> QgsProject.instance().removeMapLayer(rlayer.id())</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> os.remove(path_raster)</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> 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).</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">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.</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Try returning the FlagNoThreading flag from your algorithm's flags() override.</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">Nyall</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> Many thanks</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> Jean-François Bourdon, ing.f.</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> Analyste en télédétection</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> Direction des inventaires forestiers</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> Ministère des Forêts, de la Faune et des Parcs 5700, 4e Avenue Ouest, </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> local A-108 Québec (Québec) G1H 6R1 Téléphone : 418 627-8669, poste </span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> 4304 <a href="mailto:jean-francois.bourdon@mffp.gouv.qc.ca">jean-francois.bourdon@mffp.gouv.qc.ca</a></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> mffp.gouv.qc.ca</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> _______________________________________________</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> QGIS-Developer mailing list</span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> <a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> List info: <a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=02%7C01%7C%7C72ed7712914543cfa40908d83d7f1f60%7C8705e97737814f4790e1c84c8b884da1%7C0%7C0%7C637326963481150210&sdata=6%2FDPX0W3P6BHnYAdJ9%2BX%2FTdD%2BhYQIzeuM5gbMayTEOo%3D&reserved=0">https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=02%7C01%7C%7C72ed7712914543cfa40908d83d7f1f60%7C8705e97737814f4790e1c84c8b884da1%7C0%7C0%7C637326963481150210&sdata=6%2FDPX0W3P6BHnYAdJ9%2BX%2FTdD%2BhYQIzeuM5gbMayTEOo%3D&reserved=0</a></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;">> Unsubscribe: <a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=02%7C01%7C%7C72ed7712914543cfa40908d83d7f1f60%7C8705e97737814f4790e1c84c8b884da1%7C0%7C0%7C637326963481150210&sdata=6%2FDPX0W3P6BHnYAdJ9%2BX%2FTdD%2BhYQIzeuM5gbMayTEOo%3D&reserved=0">https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-developer&data=02%7C01%7C%7C72ed7712914543cfa40908d83d7f1f60%7C8705e97737814f4790e1c84c8b884da1%7C0%7C0%7C637326963481150210&sdata=6%2FDPX0W3P6BHnYAdJ9%2BX%2FTdD%2BhYQIzeuM5gbMayTEOo%3D&reserved=0</a></span></font></div>
<div><font face="Calibri" size="2"><span style="font-size:11pt;"> </span></font></div>
</span></font>
</body>
</html>