用于空间连接的PyQGIS

2024-10-01 09:31:11 发布

您现在位置:Python中文网/ 问答频道 /正文

对于一个QGIS 2.0/2.2和Python2.7插件,我试图根据几何函数用另一层的字段属性更新一个层的字段属性QgsGeometry.intersects(). 我的第一层是点层,第二层是包含方位角测量的向量多段线层的缓冲区。我想更新点层,以包括它相交的缓冲区多边形的方位角信息(本质上是一个空间连接)。它将自动化所描述的here。目前,在提交更改后,只有我的points图层的方位字段中的第一个要素被更新(我希望所有要素都会更新)。在

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})

Tags: 属性prcapspoint缓冲区要素rotateidx
1条回答
网友
1楼 · 发布于 2024-10-01 09:31:11

将迭代器移动到循环中可以实现以下目的:

for rotatefeat in rotateBUFF.getFeatures():
  for pointfeat in pointLayer.getFeatures():

另外,如果在数据提供程序上工作,则不需要提交更改。有两种编辑数据的方法:

  1. 在使用其edit buffer的层上,但必须首先启用编辑。编辑完成后,必须提交更改。在
  2. 根据需要在数据提供程序上。无需提交更改,使用ChangeAttributeValue时直接应用这些更改。在

通常,建议在图层上进行编辑,以防止对未处于编辑模式的图层进行修改。对于插件来说尤其如此。但是,如果这段代码是严格针对您的,那么使用数据提供程序可能会更容易。编辑缓冲区的一个优点是可以立即提交更改,如果在循环过程中出现错误,则放弃更改。在

相关问题 更多 >