This question is an extension of Copying code generated when you put data into GUI via QGIS Plugins?

I have a Python file I want to run on two loaded layers in QGIS. The layers are TestPhotos (point) and TestObj (point). The tool I want the Python file to run is v.distance.

The column in TestObjects I use in v.distance is OBJ_ID

The Python file is called Test1.py. I have this code:

import processing

processing.runalg('grass7:v.distance', 'TestPhotos','point,line,area','TestObj','point,line,area',-1.0,-1.0,'to_attr','OBJ_ID','OBJ_ID',None,-1.0,0.0001,'nearestobj','distance')

I get the following output using the QGIS Python console:

Error: Wrong parameter value: None

I believe it is associated with the v.distance GRASS_REGION_PARAMETER <ParameterExtent> parameter. Any idea how to deal with this to get the Python script working?

EDIT1 - responding to comment

When I typed in processing.alghelp('grass7:v.distance'), i got this:

>>>import processing

    ALGORITHM: v.distance - Finds the nearest element in vector map 'to' for elements in vector map 'from'.
        from <ParameterVector>
        from_type <ParameterString>
        to <ParameterVector>
        to_type <ParameterString>
        dmax <ParameterNumber>
        dmin <ParameterNumber>
        upload <ParameterString>
        column <ParameterString>
        to_column <parameters from to>
        GRASS_REGION_PARAMETER <ParameterExtent>
        GRASS_MIN_AREA_PARAMETER <ParameterNumber>
        from_output <OutputVector>
        output <OutputVector>


When I try 'Test.py' with the following code:

import processing 
TestPhotos = QgsVectorLayer('c:\OSGeo4w64\TestFiles\TestPhotos.shp', 'TestPhotos', 'ogr')
TestObj = QgsVectorLayer('c:\OSGeo4w64\TestFiles\TestObj.shp', 'TestObj', 'ogr')

extent = TestPhotos.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

'grass7:v.distance', \
"%f,%f,%f,%f" %(xmin, xmax, ymin, ymax),\

The output files are correctly generated with the above code. But they are not loaded in QGIS.

  • what does processing.alghelp('grass7:v.distance') print out? Grass Algorithms in QGIS can change the order of certain params – julsbreakdown Apr 19 '17 at 6:02
  • Thanks Julsbreakdown. See updated question for your answer. – Joe Apr 20 '17 at 11:34

You get the error because an extent is required and it can't be set to None.

Before posting the code, I think there is another error linked to the input layers: they should be added as objects, and not as strings. If 'TestPhotos' and 'TestObj' are the names with which they are loaded in the Layers Panel, you may call them in this way (the following is a general method because I don't know if you are using the Python Console or a new script from the Processing Toolbox):

TestPhotos = QgsVectorLayer('path to the vector layer', 'TestPhotos', 'ogr')
TestObj = QgsVectorLayer('path to the vector layer', 'TestObj', 'ogr')

Then, since it seems you want to use the minimum covering extent, you may try to use the following code:

import processing

extent = TestPhotos.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

processing.runalg('grass7:v.distance', TestPhotos,'point,line,area',TestObj,'point,line,area',-1.0,-1.0,'to_attr','OBJ_ID','OBJ_ID',"%f,%f,%f,%f" %(xmin, xmax, ymin, ymax),-1.0,0.0001,'nearestobj','distance')

instead of what you have provided.

EDIT Perhaps, also the 'distance' parameter could be wrong because it should be a path or None.

  • Hi mgri. I am actually using the console to call a new python file I am writing, which calls v.distance. Is that what you meant by 'new script from the Processing Toolbox'? I should clarify this in my question but just need to make sure the detail you were referring to. For the purpose of this question, assume I will be hard coding the layer name in the new python file. – Joe Apr 20 '17 at 11:42
  • I added the path to the last two parameters and it worked!! (almost fully). Can you please edit your answer to explain how to add the layers in QGIS canvas once they're generated? At this stage, the files are just generated in the directory but not loaded in QGIS. – Joe Apr 20 '17 at 13:09
  • @Joe - You could try replacing runalg with runandload. If that doesn't work then use runalg and add the following at the end: iface.addVectorLayer('C:/OSGeo4W64/TestFiles/Outputfile0.shp', 'layerName', 'ogr') – Joseph Apr 20 '17 at 13:12
  • @Joe You may follow the suggestions provided by Joseph: do they work for you? – mgri Apr 20 '17 at 15:09

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.