【问题标题】:PyQGIS for spatial join用于空间连接的 PyQGIS
【发布时间】: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 循环。查看有关当前结果的有问题的编辑。

标签: python qgis


【解决方案1】:

将迭代器移到循环中就可以了:

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

此外,如果您使用数据提供程序,则无需提交更改。编辑数据有两种方式:

  1. 在图层上使用其edit buffer,但您必须先启用编辑。编辑完成后,您必须提交更改。
  2. 根据需要在数据提供者上。无需提交更改,它们在使用 changeAttributeValues 时直接应用。

通常,建议在图层上进行编辑,以防止对未处于编辑模式的图层进行修改。对于插件来说尤其如此。但是,如果此代码完全适合您,那么与数据提供者合作可能会更容易。编辑缓冲区的一个优点是您可以一次提交更改,如果在循环期间出现问题,则丢弃更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    相关资源
    最近更新 更多