6

I run this script in my python console on QGIS:

import os
import processing
from qgis.core import QgsVectorLayer

polygon_file = "E:/polygon.shp"
point_input = "E:/station.shp"
raster_output = 'E:/output_idw.tif'

layer = QgsVectorLayer(point_input, 'layer', 'ogr')
processing.runalg('grass7:v.surf.idw', layer, 8.0, 5.0, 'value', False, "%f,%f,%f,%f" % (110.5, 117, -9, -4.5), 0.001, -1.0, 0.0001, raster_output)
processing.runalg("gdalogr:cliprasterbymasklayer", raster_output, polygon_file, -1, False, False, False, 6, 0, 75, 1, 1, False, 0, False, "", 'E:/output_crop.tif')
os.remove(raster_output)

I need to delete the unclip raster file (raster_output) directly by doing "os.remove(raster_output)" after processing but it return "[Error 32] The process cannot access the file because it is being used by another process" What should i do so i can delete that file without exit QGIS?

  • Processing may load the result of processing algorithm. Is your raster in the result in the QGIS Layer Panel? Try to remove it with QgsMapLayerRegistry.instance().removeMapLayer(layer_id) – Zoltan Jun 11 '17 at 4:26
  • No it's not, there're no active layer on QGIS Layer panel @Zoltan – Faizalprabowo Jun 11 '17 at 4:38
  • It is still loaded in the object raster_output and therefore locked. Delete raster_output first (del raster_output). – Matte Jun 11 '17 at 5:49
  • del raster_output os.remove('E:/output_idw.tif') Traceback (most recent call last): File "<input>", line 1, in <module> WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'E:/output_idw.tif' – Faizalprabowo Jun 11 '17 at 7:24
  • @Matte it still stuck and the file cannot be remove – Faizalprabowo Jun 11 '17 at 7:24
5

You could just make raster_output a temporary layer instead of writing it to disk. That way, you can avoid having to manually remove it. You can do this by assigning the output of grass7:v.surf.idw to None and then call the result as the input to the following algorithm.

So you could try something like this:

import os
import processing
from qgis.core import QgsVectorLayer

polygon_file = "E:/polygon.shp"
point_input = "E:/station.shp"

layer = QgsVectorLayer(point_input, 'layer', 'ogr')
raster_output = processing.runalg('grass7:v.surf.idw', layer, 8.0, 5.0, 'value', False, "%f,%f,%f,%f" % (110.5, 117, -9, -4.5), 0.001, -1.0, 0.0001, None)
processing.runalg("gdalogr:cliprasterbymasklayer", raster_output['OUTPUT'], polygon_file, -1, False, False, False, 6, 0, 75, 1, 1, False, 0, False, "", 'E:/output_crop.tif')
  • 'QgsRasterLayer' object has no attribute 'selectedfeaturecount' see log for more details.. I've got that error.. – Faizalprabowo Jun 13 '17 at 4:19
  • @Faizalprabowo - Strange, I cannot reproduce your error as it works fine for me. Try adding layer2 = QgsVectorLayer(polygon_file, 'layer2', 'ogr') and then use processing.runalg("gdalogr:cliprasterbymasklayer", raster_output['OUTPUT'], layer2, -1, False, False, False, 6, 0, 75, 1, 1, False, 0, False, "", 'E:/output_crop.tif') – Joseph Jun 13 '17 at 9:19
  • why after surf.idw process, the output.tif doesn't exist on temp directory so all script return "Wrong parameter value: " in raster_output['OUTPUT'] – Faizalprabowo Jun 19 '17 at 6:50
  • @Faizalprabowo - Have you tried saving it in another directory and not temp? Maybe even on the desktop (perhaps a permission issue)? – Joseph Jun 19 '17 at 8:18
  • if i do that, what's the different with my question example above, actually i need to remove it – Faizalprabowo Jun 20 '17 at 7:00
1

I had the same error with QGIS 3, by writing multiple times to a vector layer with "qgis:zonalstatistics" processing function.

The layer wasn't in the registry:

registryLayers = QgsProject.instance().mapLayers().keys()
legendLayers = [ layer.layerId() for layer in QgsProject.instance().layerTreeRoot().findLayers() ]
layersToRemove = set( registryLayers ) - set( legendLayers )
QgsProject.instance().removeMapLayers( list( layersToRemove ) )

I removed the corresponding variable:

del(vectorlayer)

After these, I still couldn't delete the shapefile in explorer.
Finally, I solved it by removing globals:

for name in dir():
    if not name.startswith('_'):
        del globals()[name]

Then you need to reload some packages:

from qgis.core import *
import processing

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.