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.
Parameters
Examples
var point1 = {
"type": "Feature",
"properties": {
"marker-color": '#f00'
},
"geometry": {
"type": "Point",
"coordinates": [-75.343, 39.984]
}
};
var point2 = {
"type": "Feature",
"properties": {
"marker-color": '#0f0'
},
"geometry": {
"type": "Point",
"coordinates": [-75.534, 39.123]
}
};
var points = {
"type": "FeatureCollection",
"features": [point1, point2]
};
var bearing = turf.bearing(point1, point2);
Returns number bearing in decimal degrees
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