3

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',
             'from_type':[0,1,3],
             'to':'C:\QGIS\Projects\QGIS3 Dev\area 1\singleasn.shp',
             'to_type':[0,1,3],
             'dmax':-1,
             'dmin':-1,
             'upload':[0],
             'column':['calculated'],
             'to_column':None,
             'from_output':'memory:nearest',
             'output':'memory:distance',
             'GRASS_REGION_PARAMETER':extents_str,
             'GRASS_SNAP_TOLERANCE_PARAMETER':-1,
             'GRASS_MIN_AREA_PARAMETER':0.0001,
             'GRASS_OUTPUT_TYPE_PARAMETER':0,
             'GRASS_VECTOR_DSCO':'',
             'GRASS_VECTOR_LCO':'',
             'GRASS_VECTOR_EXPORT_NOCAT':False}


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?

0

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:

----------------
Outputs
----------------

from_output:  <QgsProcessingOutputVectorLayer>
    Nearest

output:  <QgsProcessingOutputVectorLayer>
    Distance
None
| 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
0

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

qgis.doc

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.