1

I have a QGIS plugin that tries to call the v.clean algorithm multiple times in a row and use the same output file each time the algorithm runs through. But even though I set the --overwrite parameter to true (as found in the grass manual), the output can't be overwritten and instead I get the error

 ERROR 1: A file system object called 'C:\Users\...\Desktop\output.gpkg' already exists.

in the QGIS log. I can still run the plugin, but only the snap algorithm of the following code will be applied to my line layer:

    output = self.dlg.directory.text()

    params_snap = {
        'input' : input_2,
        'type' : 1,
        'tool' : 1,
        'threshold' : 0.000095,
        'output': output,
        'error' : 'memory:',
        'GRASS_OUTPUT_TYPE_PARAMETER': 2
    }
    processing.run('grass7:v.clean', params_snap)

    params_break = {
        'input': input_2,
        'type': 1,
        'tool': 0,
        'threshold': 0.1,
        '-c': True,
        'output': output,
        'error': 'memory:',
        '--overwrite': True,
        'GRASS_OUTPUT_TYPE_PARAMETER': 2
    }
    processing.runAndLoadResults('grass7:v.clean', params_break)

The path and name of the ouput are from a QFileDialog. If the information is needed: The input file is a temporary file.

If I set a different output file for each part of the processing the code runs fine and without errors, but since I will use more than only two algorithms creating and deleting files over and over again will just slow the plugin down.

I use the current QGIS LTR 3.4.

  • the grass manual can diverge from the way QGis uses the tool. I cannot test right now, but i suspect that QGis uses another keyword for --overwrite. I suggest running this tool once using the QGis interface and then looking at the processing history to find out exactly how QGis runs this tool – xlDias Mar 25 at 14:48
  • The GUI version of the GRASS 7 tool in QGIS 2.18 doesn't have an option to specify overwriting. Does it in QGIS 3.4? – Paulo Raposo Mar 31 at 7:01
  • @PauloRaposo it does not, but you can choose consecutive processes with the same output file. In the log grass uses --ouverwrite and -o at the same time, but neither of them, nor both of them work. – gHupf Apr 2 at 18:21
3
+50

but since I will use more than only two algorithms creating and deleting files over and over again will just slow the plugin down.

Anyway files are created and deleted whether you want it or not (see your ERROR 1 message), so if --overwrite parameter do not work - as workaround you can manually check if file exist and delete it before starting grass7:v.clean.

Here is the code:

import os
if os.path.exists(output): 
    os.remove(output)

Another workaround is to use subprocess module to call cmd variant of the command. Here is example:

import subprocess
clipper = r'D:\source\nts_index_250k.shp'
tiles = '117D 117A 116O 116P'.split()
for tile in tiles:
    warp = '''gdalwarp -cutline {clipper} -cwhere "'TILE_NAME' = '{tile}'" {infile} {outfile}'''.format(clipper=clipper,
        tile=tile,
        infile=infile,
        outfile=tile + infile
        )
    subprocess.call(warp)

EDIT 1:

If you execute this code: print(processing.algorithmHelp('grass7:v.clean')) with QGIS Python console, you may notice that --overwrite is absent in parameters description.

But if you run the same tool using QGIS interface and check for Log tab you may notice that --overwrite parameter is used somehow. That is because you may run every grass tool using cmd/shell (avoiding pyQgis API).

Here is v.clean log results: picture

EDIT 2:

Also you may try to insert string --overwrite in these paramaters:

GRASS_VECTOR_DSCO: v.out.ogr output data source options (dsco)

    Parameter type: QgsProcessingParameterString

    Accepted data types:
        - str
        - QgsProperty

GRASS_VECTOR_LCO: v.out.ogr output layer options (lco)

    Parameter type: QgsProcessingParameterString

    Accepted data types:
        - str
        - QgsProperty
  • I do not really know how the second solution works. Could you explain the benefits of it compared to the first solution? If I try the first solution I get a WinError 32, the process cannot access the file because it is used by another process. I do not use RunAndLoadResults, so QGIS does not load the output of the first time v.clean runs through. Any idea how to solve this? – gHupf Apr 2 at 18:07
  • @gHupf See update – Mr. Che Apr 3 at 5:03

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.