1.0.14 โ€ข Published 6 months ago

@cdeshpande/geo-utils v1.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

geo-utils

A lightweight and extensible TypeScript utility library for geospatial calculations such as:

  • ๐ŸŒ Haversine distance between two coordinates
  • ๐Ÿ“ Midpoint between two geographic points
  • ๐ŸŒ Vincenty distance
  • ๐ŸŒ Bearing (also called forward azimuth)
  • ๐ŸŒ DestinationPoint โ€” Compute Destination from Distance & Bearing

Supports kilometers and miles, with built-in input validation.

vincentyDistance calculates the geodesic distance between two geographic coordinates using the Vincenty inverse formula on the WGS-84 ellipsoid. It provides high-precision results suitable for long-distance measurements and real-world applications like aviation and GPS.

Calculates the initial bearing (also called forward azimuth) between two geographic coordinates. The result is the angle in degrees from North (0ยฐ) to the direction of the destination point, moving along the great-circle path.

Calculates the initial bearing (also called forward azimuth) between two geographic coordinates. The result is the angle in degrees from North (0ยฐ) to the direction of the destination point, moving along the great-circle path.

Calculate the destination point given: - a starting latitude/longitude, - a distance (in kilometers or miles), - and a bearing (angle from true north in degrees).

๐Ÿ“ฆ Installation

npm install @cdeshpande/geo-utils

๐Ÿš€ Haversine Usage

import { haversineDistance } from '@cdeshpande/geo-utils';

// Define coordinates
const start = {
  latitude: 30.849635,
  longitude: -83.24559
};

const end = {
  latitude: 27.950575,
  longitude: -82.457178
};

// Calculate distance in kilometers (default)
console.log(haversineDistance(start.latitude, start.longitude, end.latitude, end.longitude));
// โ†’ 403.28 (km)

// Calculate distance in miles
console.log(haversineDistance(start.latitude, start.longitude, end.latitude, end.longitude, 'miles'));
// โ†’ 250.47 (miles)

๐Ÿš€ Midpoint Usage

// Midpoint
const mid = midpoint(start.latitude, start.longitude, end.latitude, end.longitude);
console.log(mid); // โ†’ { latitude: 29.41499, longitude: -82.851384 }

๐Ÿš€ Vincenty Usage

import { vincentyDistance } from '@cdeshpande/geo-utils';

// Define two geographic points (latitude, longitude)
const paris = { lat: 48.8566, lon: 2.3522 };
const nyc = { lat: 40.7128, lon: -74.006 };

// Get distance in meters
const distance = vincentyDistance(paris.lat, paris.lon, nyc.lat, nyc.lon);
console.log(`Distance: ${(distance / 1000).toFixed(2)} km`);
// Output: Distance: 5853.00 km

๐Ÿš€ Bearing Usage

import { initialBearing } from '@cdeshpande/geo-utils';
const bearing = initialBearing(30, -90, 40, -80);
console.log(`Initial Bearing: ${bearing.toFixed(2)}ยฐ`); // โ†’ Initial Bearing: 37.23ยฐ

๐Ÿš€ DestinationPoint Usage

import { destinationPoint } from '@cdeshpande/geo-utils';

const start = { latitude: 0, longitude: 0 };
const distance = 100; // in kilometers
const bearing = 90;   // due east

const destination = destinationPoint(
  start.latitude,
  start.longitude,
  distance,
  bearing
);

console.log(destination);
// โ†’ { latitude: ~0.0, longitude: ~0.899 }

๐Ÿ“˜ API

haversineDistance(lat1: number, lon1: number, lat2: number, lon2: number, unit?: string): number

ParamTypeRequiredDescription
lat1numberโœ…Latitude of the first point
lon1numberโœ…Longitude of the first point
lat2numberโœ…Latitude of the second point
lon2numberโœ…Longitude of the second point
unit'km' \| 'miles'โŒUnit of distance (default is 'km')

midpoint(lat1: number, lon1: number, lat2: number, lon2: number): { latitude: number, longitude: number }

ParamTypeRequiredDescription
lat1numberโœ…Latitude of the first point
lon1numberโœ…Longitude of the first point
lat2numberโœ…Latitude of the second point
lon2numberโœ…Longitude of the second point

vincentyDistance(lat1: number, lon1: number, lat2: number, lon2: number): number

ParamTypeRequiredDescription
lat1numberโœ…Latitude of the first point
lon1numberโœ…Longitude of the first point
lat2numberโœ…Latitude of the second point
lon2numberโœ…Longitude of the second point

initialBearing(lat1, lon1, lat2, lon2): number

ParamTypeRequiredDescription
lat1numberโœ…Latitude of the first point
lon1numberโœ…Longitude of the first point
lat2numberโœ…Latitude of the second point
lon2numberโœ…Longitude of the second point

destinationPoint( lat: number, lon: number, distance: number, bearing: number, unit: 'km' | 'miles'): { latitude: number; longitude: number }

ParamTypeRequiredDescription
lat1numberโœ…Latitude of the first point
lon1numberโœ…Longitude of the first point
lat2numberโœ…Latitude of the second point
lon2numberโœ…Longitude of the second point
bearingnumberโœ…Direction to travel (in degrees from true north)
unit'km' \| 'miles'โŒUnit of distance (default is 'km')

๐Ÿ”’ Validations

  • Throws TypeError if latitude/longitude is not a number.
  • Throws RangeError if coordinates are out of bounds.
  • Throws Error if unit is not 'km' or 'miles'.

๐Ÿงพ License

MIT ยฉ Chaitanya Deshpande