2

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?

injecteer
  • 20,038
  • 4
  • 45
  • 89

2 Answers2

0

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
injecteer
  • 20,038
  • 4
  • 45
  • 89
0

Turf expects data in (longitude, latitude) order per the GeoJSON standard, see https://github.com/Turfjs/turf#data-in-turf

In your case input data should be:

const p1 = [ 11.6558318, 48.1039072 ]
const p2 = [ 11.6555873, 48.1035817 ] 
ssubbotin
  • 536
  • 5
  • 6