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
  • 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 at 13:12

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.