Geographic Information Systems Asked by K. Osborne on October 22, 2020
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:QGISProjectsQGIS3 Devarea 1buildings.shp',
'from_type':[0,1,3],
'to':'C:QGISProjectsQGIS3 Devarea 1singleasn.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?
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
Answered by Comrade Che on October 22, 2020
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
Answered by jfoguet on October 22, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP