【发布时间】:2014-06-23 18:07:14
【问题描述】:
对于 QGIS 2.0/2.2 和 Python 2.7 插件,我正在尝试使用基于几何函数 QgsGeometry.intersects() 的另一层的字段属性来更新一层的字段属性。我的第一层是点层,第二层是包含方位角测量的矢量折线层的缓冲区。我想更新点层以包含它相交的缓冲区多边形的方位角信息(本质上是空间连接)。它是自动化描述here的过程。目前,在提交更改后,只有我的点图层的轴承字段中的第一个要素会更新(我希望所有要素都会更新)。
rotateBUFF = my buffer polygon layer
pointLayer = my point layer to obtain azimuth data
rotate_IDX = rotateBUFF.fieldNameIndex('bearing')
point_IDX = pointLayer.fieldNameIndex('bearing')
rotate_pr = rotateBUFF.dataProvider()
point_pr = pointLayer.dataProvider()
rotate_caps = rotate_pr.capabilities()
point_caps = point_pr.capabilities()
pointFeatures = pointLayer.getFeatures()
rotateFeatures = rotateBUFF.getFeatures()
for rotatefeat in rotateFeatures:
for pointfeat in pointFeatures:
if pointfeat.geometry().intersects(rotatefeat.geometry()) == True:
pointID = pointfeat.id()
if point_caps & QgsVectorDataProvider.ChangeAttributeValues:
bearing = rotatefeat.attributes()[rotate_IDX]
attrs = {point_IDX : bearing}
point_pr.changeAttributesValues({pointID : attrs})
【问题讨论】:
-
您能否打印最后一行(changeAttributesValues)以查看是否已到达,如果是,则查看是否已提交更改。
-
@DenisRouzaud changeAttributesValue() 返回 True 并且 commitChanges() 返回 True 一次(之前在另一个函数中我使用了 commitChanges() 并且它为每个更新的功能返回 True)。我相信 if 语句中发生了一些事情。
-
@DenisRouzaud 实际上,也许它更有可能是 for 循环。查看有关当前结果的有问题的编辑。