pythongispointsazimuth

How to Calculate Azimuth in Python


I have two points in 3d demension (x,y,z) and I want to calculate the Azimuth between then using Python3. Thanks ahead.


Solution

  • Please try this,

    In analytic geometry,

    The distance = SQRT((x2 –x1)2+(y2 –y1)2+(z2 –z1)2)

    The plunge = arcsin ((z2 – z1) / distance)

    The azimuth = arctan((x2 –x1)/(y2 –y1)) (always in two dimensions)

    The value θ returned will be in the range of ±90° and must be corrected to give the true azimuth over the range of 0 to 360°

    import math
    x1,y1,z1 = 5.0,6.7,1.5
    x2,y2,z2 = 4.0,1.2,1.6
    distance = math.sqrt((x2-x1)**2+(y2-y1)**2+(z2 -z1)**2)
    print(distance)
    # 5.5910642993977451
    plunge = math.degrees(math.asin((z2-z1)/distance))
    print(plunge)
    # 1.0248287567800018 # the resulting dip_plunge is positive downward if z2 > z1
    azimuth = math.degrees(math.atan2((x2-x1),(y2-y1)))
    print(azimuth)
    # -169.69515353123398 # = 360 + azimuth = 190.30484646876602 or  180+ azimuth = 10.304846468766016 over the range of 0 to 360°