For a QGIS 2.0 and 2.2 Python plugin, I am trying to update one layer's field attribute with another layer's field attribute based on geometry function QgsGeometry.within(). My first layer is a point layer, the second layer is a buffer of a vector polyline layer containing azimuth measurements. I would like to update the point layer to include azimuth information of the buffer polygon it is within (essentially a spatial join). It is to automatize the process described here: How to horizontally align irregularly shaped point symbol to vector layer in QGIS?. Currently, I'm not getting an error, but nothing is happening either.

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().within(rotatefeat.geometry()):
            pointID = pointfeat.id()
            if point_caps & QgsVectorDataProvider.ChangeAttributeValues:
                bearing = rotatefeat.attributes()[rotate_IDX]
                attrs = {point_IDX : bearing}
                point_pr.changeAttributesValues({pointID : attrs})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.