In the picture below I am finding the bearing of two close lines. This is the picture of two complete lines. The brown line might be a bit large from the blue line.

Should not be the bearing of these two lines should be close to each other with a difference of may be few degrees?

I am checking whether they are equal or not by getting the difference of both lines bearing. I am using python to calculate the bearing and get the difference between there bearing. The code I am using to get out the bearing is:

def calculate_initial_compass_bearing(pointA, pointB):
    Calculates the bearing between two points.
    The formulae used is the following:
        θ = atan2(sin(Δlong).cos(lat2),
                  cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong))
      - `pointA: The tuple representing the latitude/longitude for the
        first point. Latitude and longitude must be in decimal degrees
      - `pointB: The tuple representing the latitude/longitude for the
        second point. Latitude and longitude must be in decimal degrees
      The bearing in degrees
    :Returns Type:
    if (type(pointA) != tuple) or (type(pointB) != tuple):
        raise TypeError("Only tuples are supported as arguments")

    lat1 = math.radians(pointA[0])
    lat2 = math.radians(pointB[0])

    diffLong = math.radians(pointB[1] - pointA[1])

    x = math.sin(diffLong) * math.cos(lat2)
    y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1)
            * math.cos(lat2) * math.cos(diffLong))

    initial_bearing = math.atan2(x, y)

    # Now we have the initial bearing but math.atan2 return values
    # from -180° to + 180° which is not what we want for a compass bearing
    # The solution is to normalize the initial bearing as shown below
    initial_bearing = math.degrees(initial_bearing)
    compass_bearing = (initial_bearing + 360) % 360

return compass_bearing

In the end what I am doing is calculate there difference like that:

DifferenceInBearing = abs(BlueLineBearing - BrownLineBearing)

My problem is that the difference between them is huge just around 179.71922333147256 degrees.

The WKT values of blue line is:

LINESTRING (327854.7919419953 6809188.658530097, 327854.2288694207 6809187.856443238)

The Wkt value for brown line:

LINESTRING (327854.139971 6809187.723519, 327854.714599625 6809188.541579125)

Shouldn't the bearing be close enough?

enter image description here

  • @BERA that is what the problem the difference between there bearing is huge approximately 179. Do you have some idea why is it? – mimetype Sep 27 '17 at 8:58

One line starts at bottom left and one starts at top right so a diffence of about 180 should be correct.

I use this calculation, taken from Find the bearing angle between two points in a 2D space:

import math

def calculate_initial_compass_bearing(pointA, pointB):
    angle=math.atan2(endy-starty, endx-startx)
    if angle>=0:
        return math.degrees(angle)
        return math.degrees((angle+2*math.pi))

Which for the first line gives:

calculate_initial_compass_bearing((327854.139971,6809187.723519), (327854.714599625,6809188.541579125))

And second line:

calculate_initial_compass_bearing((327854.7919419953, 6809188.658530097), (327854.2288694207, 6809187.856443238))
| improve this answer | |
  • why you changed your code? – mimetype Sep 28 '17 at 10:12

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.