Python 2.7 and QGIS 2.6: Subtracting two values and append ID of feature to list

拈花ヽ惹草 提交于 2019-12-11 17:01:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!