UNPKG

@cdeshpande/geo-utils

Version:

A lightweight, blazing-fast TypeScript library for calculating distances (Haversine) and geospatial math with support for kilometers and miles.

160 lines (116 loc) โ€ข 6.44 kB
# 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 | Param | Type | Required | Description | | ----- | ----------------- | -------- | ------------------------------------ | | lat1 | `number` | โœ… | Latitude of the first point | | lon1 | `number` | โœ… | Longitude of the first point | | lat2 | `number` | โœ… | Latitude of the second point | | lon2 | `number` | โœ… | 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 } | Param | Type | Required | Description | | ----- | -------- | -------- | ----------------------------- | | lat1 | `number` | โœ… | Latitude of the first point | | lon1 | `number` | โœ… | Longitude of the first point | | lat2 | `number` | โœ… | Latitude of the second point | | lon2 | `number` | โœ… | Longitude of the second point | ### vincentyDistance(lat1: number, lon1: number, lat2: number, lon2: number): number | Param | Type | Required | Description | | ----- | -------- | -------- | ----------------------------- | | lat1 | `number` | โœ… | Latitude of the first point | | lon1 | `number` | โœ… | Longitude of the first point | | lat2 | `number` | โœ… | Latitude of the second point | | lon2 | `number` | โœ… | Longitude of the second point | ### initialBearing(lat1, lon1, lat2, lon2): number | Param | Type | Required | Description | | ----- | -------- | -------- | ----------------------------- | | lat1 | `number` | โœ… | Latitude of the first point | | lon1 | `number` | โœ… | Longitude of the first point | | lat2 | `number` | โœ… | Latitude of the second point | | lon2 | `number` | โœ… | Longitude of the second point | ### destinationPoint( lat: number, lon: number, distance: number, bearing: number, unit: 'km' | 'miles'): { latitude: number; longitude: number } | Param | Type | Required | Description | | ------- | ----------------- | -------- | ------------------------------------------------ | | lat1 | `number` | โœ… | Latitude of the first point | | lon1 | `number` | โœ… | Longitude of the first point | | lat2 | `number` | โœ… | Latitude of the second point | | lon2 | `number` | โœ… | Longitude of the second point | | bearing | `number` | โœ… | 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