Leaflet JavaScript bearings do not match Python calculations

18 views Asked by At

In my app, I use bearing for detection of signal intersection. I use the following python code for detecting intersection of bearings:

import nvector as nv

def bearing_intersect_nvector(point_0, bearing_0, point_1, bearing_1):
    point_a1 = nv.GeoPoint(point_0[0], point_0[1], degrees=True)
    point_a2 = point_a1.displace(10, bearing_0, degrees=True)[0]
    point_b1 = nv.GeoPoint(point_1[0], point_1[1], degrees=True)
    point_b2 = point_b1.displace(10, bearing_1, degrees=True)[0]
    path_a = nv.GeoPath(point_a1, point_a2)

    path_b = nv.GeoPath(point_b1, point_b2)
    point_c = path_a.intersect(path_b)
    point_c = point_c.to_geo_point()
    lat, lon = point_c.latitude_deg, point_c.longitude_deg
    return Coord(lon, lat)

On JavaScript I use the following code for second point of line for drawing bearing line:

  const end_x = (startCoords as LatLng).lng + length * Math.cos(-(angle - 90) * Math.PI / 180);
  const end_y = (startCoords as LatLng).lat + length * Math.sin(-(angle - 90) * Math.PI / 180);

But the issue is that bearing drawn with JavaScript code using end_x and end_y as end coordinates do not match with bearing drawn calculated point of intersection using python code above.

The same issue I have with the following python code, it matches above JavaScript bearing calculation, but the bearing do not match with nvector calculation.

import math
from geojson import LineString, Feature, Point
from turfpy.misc import line_intersect

def bearing_intersect_turfpy(point_0, bearing_0, point_1, bearing_1):
    def add_bearing(coord, angle):
        length = 1
        end_lat = coord["coordinates"][0] + length * math.sin(-(angle - 90) * math.pi / 180)
        end_lon = coord["coordinates"][1] + length * math.cos(-(angle - 90) * math.pi / 180)
        azimuth = Feature(geometry=LineString([coord, Point((end_lat, end_lon))]))
        return azimuth

    point_0 = Point((point_0[0], point_0[1]))
    point_1 = Point((point_1[0], point_1[1]))
    bearing_0 = add_bearing(point_0, bearing_0)
    bearing_1 = add_bearing(point_1, bearing_1)
    point_2 = line_intersect(bearing_0, bearing_1)["features"][0]["geometry"]["coordinates"]
    return Coord(point_2[1], point_2[0])

I wondering why ?? And which calculation is proper ?? From my experience nvector implementation works much better, than simple geometry calculated, but I am not sure why ...

0

There are 0 answers