I calculate the azimuth of a vector and update a field in my QGIS 2.0 Python 2.7 plugin. This works currently, but it takes much longer than i'd like to. I believe it is because i'm querying the data source on every call. Any suggestions on updating the fields all in one go as was suggested in another one of my questions? I'm having issues trying to adapt the suggestion for this particular evaluate function.

    bear_idx = self.rotateLayer.fieldNameIndex('angleRot')
    bearCalc = QgsExpression('(atan((xat(-1)-xat(0))/(yat(-1)-yat(0)))) * 180/3.14159 + (180 *(((yat(-1)-yat(0)) < 0) + (((xat(-1)-xat(0)) < 0 AND (yat(-1) - yat(0)) >0)*2)))')
    for fields in self.rotateLayer.getFeatures():
        fields[bear_idx] = bearCalc.evaluate(fields)

1 Answer 1


I don't know what you are trying to do. In PyQGIS, the azimuth of a vector is given by


Look at How to add Direction and Distance to attribute table?, How do I find vector line bearing in QGIS or GRASS? or Create Intersecting lines and remove dangles

  • thanks for the tip, i will eventually try to adapt it, as I did not see this before. I'm wondering, though, if solution would actually help speed things up. My code works--it just takes a long time. Taking a look at the suggestion on "How to add Direction and Distance to the attribute table?", just selecting my thousand vectors would take a while as well.
    – user25976
    Commented Oct 9, 2014 at 18:00
  • With only Python with Fiona, it is much faster.
    – gene
    Commented Oct 9, 2014 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.