1

I am creating a Python script in QGIS 3 and I want to use the processing module. I want to transform my raster file to a vector file with the GRASS algorithm r.to.vect. My problem here is that the output I am getting is always empty. The way I am running the algorithm looks like this:

    param = {"input": path_to_some_raster_file,
             "type": 2,
             "output": some_tmp_file,
             'GRASS_OUTPUT_TYPE_PARAMETER': 0
             }

    vector = processing.run('grass7:r.to.vect', param)

But if I use the same parameters in the processing toolbox then the desired output is generated. What am I doing wrong with my Python attempt?

  • From the menubar, go to Processing > History. This will show you how the parameters for the tool should be used when running it from the console or script. – Joseph Feb 19 at 15:59
  • That still doesn't help me with my problem. I even copied the whole command from the history and run it in the qgis console and the result remains the same unfortunately.... – Blinxen Feb 19 at 16:10
  • I use version 3.4.4 – Blinxen Feb 22 at 11:55
  • 1
    Empty as in you receive an output but it contains no data or you do not receive any output (in which case maybe use processing.runAndLoadResults() instead of processing.run())? – Joseph Feb 22 at 12:05
  • Empty as in an output which contains no data. If I use processing.runAndLoadResults() then I get a QgsProcessingOutputLayerDefinition object. – Blinxen Feb 22 at 13:19
0

I had similar issues using the processing module. I solved doing like this:

####### Initialize you input and output layers ########

input_layer = QgsRasterLayer(path_to_some_raster_file,string_with_layer_name,'ogr')
output_path = path_to_some_tmp_file   #Full path with file name AND EXTENSION
output_layer = QgsRasterLayer(path_to_some_tmp_file,string_with_layer_name,'ogr') 

#NOTE if your output layer is a vector layer, use QgsVectorLayer instead

####### Set your parameters ########

param = {"input": input_layer, ########### This line was changed
             "type": 2,
             "output": output_path, #########This line was changed
             'GRASS_OUTPUT_TYPE_PARAMETER': 0
             }

#NOTE pass the input layer directly, but set as output the file path, and not the layer

####### Run the algorithm ########

processing.runAndLoadResults('grass7:r.to.vect', param) ###########This line was changed

####### Reload your output layer ########

output_layer = QgsRasterLayer(path_to_some_tmp_file,string_with_layer_name,'ogr')

In this way, the output_layer should contain the results of your processing algorithm.

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.