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?

| improve this question | | | | |

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)



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

(QGIS from Nightly Mac Build of QGIS from 'master' Branch)

| improve this answer | | | | |
  • 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.

| improve this answer | | | | |

Your Answer

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

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