How to compute azimuth between QgsPointV2s?

The QgsPoint class has an azimuth() method but the new QgsPointV2 class does not. What's the best way to compute the azimuth between QgsPointV2s? I couldn't find a convenient way to convert V2 to the old version, so my fallback is to construct a QgsPoint from the V2's x and y:

self.traj = QgsLineStringV2(...)
p_i = self.traj.pointN(self.i) # this returns a QgsPointV2
p_j = self.traj.pointN(self.j)
a = QgsPoint(p_i.x(),p_i.y()).azimuth(QgsPoint(p_j.x(),p_j.y()))

Is there a better way?

You can use the simple Euclidean function :

def azimuth(point1, point2):
#interval 0-180° here
angle = math.atan2(point2.x() - point1.x(), point2.y()-point1.y())
return math.degrees(angle) if angle > 0 else math.degrees(angle) + 180

a = QgsPoint(10,12)
b = QgsPoint(20,30)
azimuth(a,b)
29.054604099077146

but

a.azimuth(b)
29.054604099077146

still works in my case (double QgsPoint::azimuth(const QgsPoint & other) const)

• Yes, QgsPoint.azimuth() exists but there's no QgsPointV2.azimuth(). I'll probably take your solution and write my own function if there is no convenient way around. – underdark Nov 3 '16 at 19:28

azimuth() has been added to QgsPointV2 in a recent commit from 2016-11-05.

Guess I'll stick to the code outlined in the question until this change appears in the releases.