Thursday, 8 September 2016

qgis - Using v.distance python script parameter with PyQGIS?


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:


execfile(u'C:/OSGeo4W64/Test1.py'.encode('mbcs'))
Error: Wrong parameter value: None


I believe it is associated with the v.distance GRASS_REGION_PARAMETER 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
>>>processing.alghelp('grass7:v.distance')

ALGORITHM: v.distance - Finds the nearest element in vector map 'to' for elements in vector map 'from'.
from
from_type

to
to_type
dmax
dmin
upload
column
to_column
GRASS_REGION_PARAMETER
GRASS_SNAP_TOLERANCE_PARAMETER
GRASS_MIN_AREA_PARAMETER

from_output
output

EDIT2


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()

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,\
'C:\OSGeo4W64\TestFiles\Outputfile0.shp',\
'C:\OSGeo4W64\TestFiles\Outputfile1.shp')


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



Answer



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.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...