<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#006600">
<div class="moz-cite-prefix">Hi Stefan and many thanks for your
answer! but it seems not working :(<br>
Here the new code. I replaced "tempOutput" with "output". Layer is
correctly loaded and removed but "tempOutput" cannot be deleted.<br>
<br>
import processing<br>
import os<br>
<br>
tempOutput = 'D:/test_alg/map_to_clip.tif'<br>
maskFN = 'D:/test_alg/mask.shp'<br>
output = 'D:/test_alg/clipped_map.tif'<br>
<br>
algRes = processing.runalg("gdalogr:cliprasterbymasklayer",<br>
tempOutput,<br>
maskFN,<br>
"-9999",False,False,False,5,4,75,6,1,False,0,False,"",<br>
output)<br>
<br>
try:<br>
# add to the map<br>
tempLay = QgsRasterLayer(output, "tempLay")<br>
if not tempLay.isValid(): raise Exception("Failed to load
tempLay")<br>
QgsMapLayerRegistry.instance().addMapLayer(tempLay) #!!!!<br>
<br>
layermap = QgsMapLayerRegistry.instance().mapLayers()<br>
for i, layer in layermap.iteritems():<br>
if layer.source() == output:<br>
print 'layername',layer.source() <br>
# Remove layer references<br>
QgsMapLayerRegistry.instance().removeMapLayer(layer.id())
#!!!!<br>
<br>
# Delete the file<br>
os.remove(tempOutput)<br>
<br>
except Exception as e:<br>
print e<br>
<br>
<br>
<br>
<br>
Il 27/04/2017 16:07, Stefan Kiefer ha scritto:<br>
</div>
<blockquote
cite="mid:1869599937.78457.1493302052542@webmail.strato.com"
type="cite">
<pre wrap="">Hi enrico,
as far as I see from the reference you added the solution would be to add the result (resp. output) to a Layer instead "tempOutput". Then you remove that Layer, I suppose while removing the layer a reference on "tempOutput" ist released.
cheers
Stefan
</pre>
<blockquote type="cite">
<pre wrap="">enrico chiaradia <a class="moz-txt-link-rfc2396E" href="mailto:enrico.chiaradia@yahoo.it"><enrico.chiaradia@yahoo.it></a> hat am 27. April 2017 um 15:13 geschrieben:
Dear List,
I'd like to remove an existing file (see [tempOutput]) after it was used
by a geoalgorithm. The geoalgorithm works perfectly but after its
execution, the file [tempOutput] is locked.
Here a simple test script that returns "[Error 32] The process cannot
access the file because it is being used by another process:
'D:/test_alg/map_to_clip.tif'":
import processing
import os
tempOutput = 'D:/test_alg/map_to_clip.tif'
maskFN = 'D:/test_alg/mask.shp'
output = 'D:/test_alg/clipped_map.tif'
algRes = processing.runalg("gdalogr:cliprasterbymasklayer",
tempOutput,
maskFN,
"-9999",False,False,False,5,4,75,6,1,False,0,False,"",
output)
try:
os.remove(tempOutput)
except Exception as e:
print e
I also found a solution on the web [1] but it doesn't work for me :( If
I understand correctly, the trick is to load the file as layer and then
release it. Here its implementation:
import processing
import os
tempOutput = 'D:/test_alg/map_to_clip.tif'
maskFN = 'D:/test_alg/mask.shp'
output = 'D:/test_alg/clipped_map.tif'
algRes = processing.runalg("gdalogr:cliprasterbymasklayer",
tempOutput,
maskFN,
"-9999",False,False,False,5,4,75,6,1,False,0,False,"",
output)
try:
# add to the map
tempLay = QgsRasterLayer(tempOutput, "tempLay")
if not tempLay.isValid(): raise Exception("Failed to load tempLay")
QgsMapLayerRegistry.instance().addMapLayer(tempLay) #!!!!
layermap = QgsMapLayerRegistry.instance().mapLayers()
for i, layer in layermap.iteritems():
print 'layername',layer.source()
if layer.source() == tempOutput:
# Remove layer references
QgsMapLayerRegistry.instance().removeMapLayer(layer.id()) #!!!!
# Delete the file
os.remove(tempOutput)
except Exception as e:
print e
I didn't find any other solution for the moment. Any suggestion is
really very appreciate!
Thanks
enrico
[1]
<a class="moz-txt-link-freetext" href="https://gis.stackexchange.com/questions/93143/can-not-delete-file-after-using-runalg-function">https://gis.stackexchange.com/questions/93143/can-not-delete-file-after-using-runalg-function</a>
_______________________________________________
Qgis-developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
</pre>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<p><br>
</p>
</body>
</html>