I have a script which is trying to run v.distance on two existing shape files, buildings.shp which containing some polygons and singleasn.shp which contains a single point. This will be run from the console using QGIS 3.4

Using the algorithm via the Processing toolbox works fine, but the code has a problem.

I'm using the following python code:

e = buildings_layer.extent()
extents_str = str(e.xMinimum()) +"," + str(e.yMinimum()) +"," + str(e.xMaximum()) +"," + str( e.yMaximum())

parameters = {'from':'C:\QGIS\Projects\QGIS3 Dev\area 1\buildings.shp',
             'to':'C:\QGIS\Projects\QGIS3 Dev\area 1\singleasn.shp',

algout = processing.run('grass7:v.distance', parameters)
for ft in algout['output'].getFeatures():
    #do something here#

The algorithm appears to run ok, but when it reaches the for loop i get the following error:

AttributeError: 'str' object has no attribute 'getFeatures'

Similar code to this worked for a native algorithm but not for a grass one - is there a difference in the output and if so how do I address it?


You are trying to iterate over a string variable. Check out the output. Execute print(processing.algorithmHelp('grass7:v.distance')) to get help information for this algorithm:


from_output:  <QgsProcessingOutputVectorLayer>

output:  <QgsProcessingOutputVectorLayer>
| improve this answer | |
  • Ok, yes, Thanks for the tip, I see that and there appears to be two other accepted data types, QgsProperty and QGSProcessingOutputLayerDefinition. How do I get the output of the algorithm into one of these other types? Or How can I convert the string into a useable format to iterate over? – K. Osborne Apr 3 '19 at 13:12

I'm not completely sure but i think the string of your output is the path for the vector output. Print it and check it. If so, you can 'cast' that output into a QgsVectorLayer.

The run method returns a dictionary with one or more output names (the ones shown in the algorithm description) as keys and the file paths of those outputs as values


algout = processing.run('grass7:v.distance', parameters)['output']
vector = QgsVectorLayer(algout, '', 'ogr')
for ft in vector.getFeatures():
    #do something here#

hope it helps

| improve this answer | |

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.