Search code examples
reactjsturfjsazimuth

Turf returns wrong (?) bearing and distance


In my react app I have this piece of code:

import * as turfBearing from '@turf/bearing'
import * as turfDistance from '@turf/distance'

  distance( p1, p2 ) {
    return Math.round( turfDistance.default( p1, p2 ) * 1000 )
  }

  bearing( p1, p2 ) {
    return ( Math.round( turfBearing.default( p1, p2 ) ) + 360 ) % 360
  }

Given the data:

const p1 = [ 48.1039072, 11.6558318 ]
const p2 = [ 48.1035817, 11.6555873 ] 

The results are:

bearing = 233, dist = 45

If I feed the same data to an online service like https://www.omnicalculator.com/other/azimuth it gives results:

enter image description here

which are considerably different from turf's.

Is this turf's problem or online calculator's?


Solution

  • Indeed, Turfjs has some problems in calc. I swapped it for a randomly picked library "sgeo": "^0.0.6", and the code:

    distance( p1, p2 ) {
      return Math.round( new sgeo.latlon( ...p1 ).distanceTo( new sgeo.latlon( ...p2 ) ) * 1000 )
    }
    
    bearing( p1, p2 ) {
      return Math.round( new sgeo.latlon( ...p1 ).bearingTo( new sgeo.latlon( ...p2 ) ) )
    }
    

    produces relevant results:

    bearing = 206 dist = 40