对于QGIS2.0/2.2和Python2.7插件,我尝试基于几何函数QgsGeometry.intersects()将一个层的字段属性更新为另一个层的字段属性。我的第一层是点层,第二层是包含方位测量的矢量多线层的缓冲器。我想更新点层,以包括它相交的缓冲区多边形的方位信息(基本上是一个空间连接)。它是自动化的过程所描述的这里。目前,只有我的点层的轴承字段的第一个功能是更新后提交更改(我预期所有的功能将被更新)。
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})发布于 2014-05-08 05:18:39
将迭代器移动到循环中可以完成以下任务:
for rotatefeat in rotateBUFF.getFeatures():
for pointfeat in pointLayer.getFeatures():此外,如果您在数据提供程序上工作,则不需要提交更改。有两种编辑数据的方法:
通常,建议在层上进行编辑,以防止对不处于编辑模式的层进行修改。对于插件来说尤其如此。但是,如果这段代码是严格针对您的,那么使用数据提供程序可能会更容易。编辑缓冲区的一个优点是可以立即提交更改,如果在循环中发生了错误,则放弃这些更改。
https://stackoverflow.com/questions/23506358
复制相似问题