2

In QGIS 2.18.6, I am using the v.distance tool based on two point layers.

Layers are:

TestPhotos
TestObjects

The inputs are: enter image description here

I have a Python file called Test.py, located C:\OSGeo4W64

I want to use this file to run v.distance with these two layers as per the abovementioned input (in the image). The output is two layers (a line and point layer). The points are the photos and the lines are from photos to the object.

All I want is that data (the line and point shapefiles).

I was thinking I could just use OSGeo4W.bat or the QGIS Python console to run the file and achieve the same result as I would if I used the GUI shown above.

I figure that it would look something like this site:

v.distance [-pa] from=name [from_layer=string] [from_type=string[,string,...]] to=name [to_layer=string] [to_type=string[,string,...]] [output=name] [dmax=float] [dmin=float] upload=string[,string,...] [column=name[,name,...]] [to_column=name] [table=name] [separator=character] [--overwrite] [--help] [--verbose] [--quiet] [--ui]

So when customised for my need, it would be like this:

v.distance from="TestPhotos" from_type="point,line,area" to="TestObjects" to_type="point,line,area" dmax="-1" dmin="-1" upload="to_attr" column="OBJ_ID" to_column="OBJ_ID"

This is what I have so far in Test.py:

import sys 
sys.path.append("C:/OSGeo4W64")
import qgis.core

v.distance from="TestPhotos" from_type="point,line,area" to="TestObjects" to_type="point,line,area" dmax="-1" dmin="-1" upload="to_attr" column="OBJ_ID" to_column="OBJ_ID"

How would I write the python file Test.py so I can run it with OSGeo4W.bat and get the same result as I would with using the GUI?

1 Answer 1

4

Basically you import the processing module and run the algorithm

import processing
...
processing.runalg(...)

You can get a demo of v.distance if you build a model in processing and exporting the model to a Python script:

enter image description here

This will give something like:

outputs_GRASS7V.DISTANCE_1=processing.runalg('grass7:v.distance', testphotos,'point,line,area',testphotos,'point,line,area',-1.0,-1.0,'cat',None,None,None,-1.0,0.0001,None,None)

I did not fill out any parameters besides the two vector layers. You can read up on the Processing API in QGIS: https://docs.qgis.org/2.8/en/docs/user_manual/processing/console.html

2
  • Thanks Jakob. I updated the question. I seem to be having an issue with the None variable. I had to put some quotation marks around the exported python file from the model builder as those parameters were resulting in an error because they were not defined.
    – LHo
    Apr 18, 2017 at 23:16
  • I removed the update and created a new question here gis.stackexchange.com/questions/237236/…
    – LHo
    Apr 19, 2017 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.