问题
I would like to subtract two values retrieved from qgs feature iterator objects and append the value of the feature id to a list depending on a condition.
In the context of qgis, I have two layers: roadsLayer pathsLayer
What I have so far returns an empty list:
evenList = []
azimuthRoadsIDX = roadsLayer.fieldNameIndex('azimuth')
azimuthPathsIDX = pathsLayer.fieldNameIndex('azimuth')
roads = roadsLayer.getFeatures()
paths = pathsLayer.getFeatures()
for feat in roads:
for feature in paths:
roadsAzimuth = feat.attributes()[azimuthRoadsIDX]
pathsAzimuth = feature.attributes()[azimuthPathsIDX]
if (roadsAzimuth - pathsAzimiuth == 90) or (roadsAzimuth - pathsAzimth == -270):
evenList.append(feature.id())
Don't know why, but this works. The only problem is it doesn't work in all cases (even when roadsAzimuth - pathsAzimuth == 90, it is not appended in some cases):
for feat in roadsLayer.getFeatures():
roadsAzimuth = feat.attributes()[azimuthRoadsIDX]
for feature in pathsLayer.getFeatures():
pathsAzimuth = feature.attributes()[azimuthPathsIDX]
if (roadsAzimuth - pathsAzimiuth == 90) or (roadsAzimuth - pathsAzimth == -270):
evenList.append(feature.id())
来源:https://stackoverflow.com/questions/29569546/python-2-7-and-qgis-2-6-subtracting-two-values-and-append-id-of-feature-to-list