[Qgis-developer] release file after processing.runalg
enrico chiaradia
enrico.chiaradia at yahoo.it
Fri Apr 28 07:10:59 PDT 2017
Hi Stefan, thanks again!
the file "map_to_clip.tif" is saved on the HD and it is not submitted to
any process before executing the script (I know that because I can
delete it before running the script).
At the same time, I noticed that the file "map_to_clip.tif.aux.xml" was
created after running the script and closing qgis (absolutely, I did no
other things!).
Can that suggest the process that locks the file?
Il 28/04/2017 11:00, Stefan Kiefer ha scritto:
> Hi Enrico,
> how is that file map_to_clip.tif created? There still could be a reference on that file, maybe because of the generation process (I assume that because you call it tempOutput, so there could be still a reference from another process). For example, if that file is visualized somewhere there definitly is a referene on that file.
>
> cheers
>
> Stefan
>
>> enrico chiaradia <enrico.chiaradia at yahoo.it> hat am 27. April 2017 um 16:29 geschrieben:
>>
>>
>> Hi Stefan and many thanks for your answer! but it seems not working :(
>> Here the new code. I replaced "tempOutput" with "output". Layer is
>> correctly loaded and removed but "tempOutput" cannot be deleted.
>>
>> 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(output, "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():
>> if layer.source() == output:
>> print 'layername',layer.source()
>> # Remove layer references
>> QgsMapLayerRegistry.instance().removeMapLayer(layer.id()) #!!!!
>>
>> # Delete the file
>> os.remove(tempOutput)
>>
>> except Exception as e:
>> print e
>>
>>
>>
>>
>> Il 27/04/2017 16:07, Stefan Kiefer ha scritto:
>>> 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
>>>
>>>> enrico chiaradia <enrico.chiaradia at yahoo.it> 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]
>>>> https://gis.stackexchange.com/questions/93143/can-not-delete-file-after-using-runalg-function
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Qgis-developer mailing list
>>>> Qgis-developer at lists.osgeo.org
>>>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20170428/7fe9b3c0/attachment.html>
More information about the Qgis-developer
mailing list