What is @turf/bearing?
@turf/bearing is a module within the Turf.js library that provides geospatial analysis tools. Specifically, it calculates the bearing between two geographical points. This is useful in various applications such as navigation, mapping, and geographic information systems (GIS).
What are @turf/bearing's main functionalities?
Calculate Bearing
This feature calculates the bearing between two geographical points. The bearing is the angle between the north direction and the line connecting the two points, measured in degrees.
const turf = require('@turf/turf');
const point1 = turf.point([0, 0]);
const point2 = turf.point([10, 10]);
const bearing = turf.bearing(point1, point2);
console.log(bearing);
Calculate Bearing with Initial and Final Bearings
This feature allows you to calculate both the initial and final bearings between two points. The initial bearing is the angle at the starting point, while the final bearing is the angle at the destination point.
const turf = require('@turf/turf');
const point1 = turf.point([0, 0]);
const point2 = turf.point([10, 10]);
const initialBearing = turf.bearing(point1, point2, {final: false});
const finalBearing = turf.bearing(point1, point2, {final: true});
console.log(`Initial Bearing: ${initialBearing}, Final Bearing: ${finalBearing}`);
Other packages similar to @turf/bearing
geolib
Geolib is a library for geospatial calculations. It provides functions to calculate distances, bearings, and other geographic information. Compared to @turf/bearing, Geolib offers a broader range of geospatial functionalities but may not be as specialized in GIS analysis.
geodesy
Geodesy is a library for geodesic calculations. It includes methods for calculating distances, bearings, and other geospatial metrics. It is similar to @turf/bearing in terms of bearing calculation but also includes more advanced geodesic functions.
@turf/bearing
bearing
Takes two points and finds the geographic bearing between them,
i.e. the angle measured in degrees from the north line (0 degrees)
Parameters
start
Coord starting Pointend
Coord ending Pointoptions
Object Optional parameters (optional, default {}
)
options.final
boolean calculates the final bearing if true (optional, default false
)
Examples
var point1 = turf.point([-75.343, 39.984]);
var point2 = turf.point([-75.534, 39.123]);
var bearing = turf.bearing(point1, point2);
var addToMap = [point1, point2]
point1.properties['marker-color'] = '#f00'
point2.properties['marker-color'] = '#0f0'
point1.properties.bearing = bearing
Returns number bearing in decimal degrees, between -180 and 180 degrees (positive clockwise)
This module is part of the Turfjs project, an open source
module collection dedicated to geographic algorithms. It is maintained in the
Turfjs/turf repository, where you can create
PRs and issues.
Installation
Install this module individually:
$ npm install @turf/bearing
Or install the Turf module that includes it as a function:
$ npm install @turf/turf