Library to provide basic geospatial operations like distance calculation, conversion of decimal coordinates to sexagesimal and vice versa, etc.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core distance measurement functions using various algorithms from simple spherical calculations to high-precision geodetic formulas.
Calculates the distance between two points using the spherical law of cosines formula.
/**
* Calculate distance between two coordinates using spherical formula
* @param from - Starting coordinate
* @param to - Destination coordinate
* @param accuracy - Precision for rounding (default: 1 meter)
* @returns Distance in meters
*/
function getDistance(
from: GeolibInputCoordinates,
to: GeolibInputCoordinates,
accuracy?: number
): number;Usage Examples:
import { getDistance } from "geolib";
// Basic distance calculation
const distance = getDistance(
{ latitude: 51.5103, longitude: 7.49347 },
{ latitude: 51.514, longitude: 7.4138 }
);
console.log(distance); // 5504 (meters)
// Using different coordinate formats
const distanceGeoJSON = getDistance(
[7.49347, 51.5103], // [lng, lat] format
[7.4138, 51.514]
);
// With custom accuracy
const preciseDistance = getDistance(
{ lat: 51.5103, lng: 7.49347 },
{ lat: 51.514, lng: 7.4138 },
0.01 // Round to centimeter precision
);Calculates distance using Vincenty's inverse formula for ellipsoids, providing higher accuracy especially for long distances.
/**
* Calculate distance using high-precision Vincenty formula
* @param start - Starting coordinate
* @param end - Destination coordinate
* @param accuracy - Precision for rounding (default: 1 meter)
* @returns Distance in meters with high precision
*/
function getPreciseDistance(
start: GeolibInputCoordinates,
end: GeolibInputCoordinates,
accuracy?: number
): number;Usage Examples:
import { getPreciseDistance } from "geolib";
// High-precision distance (more accurate for long distances)
const preciseDistance = getPreciseDistance(
{ latitude: 40.7589, longitude: -73.9851 }, // New York
{ latitude: 34.0522, longitude: -118.2437 } // Los Angeles
);
console.log(preciseDistance); // 3944420 (meters, very precise)
// Compare with simple distance
const simpleDistance = getDistance(
{ latitude: 40.7589, longitude: -73.9851 },
{ latitude: 34.0522, longitude: -118.2437 }
);
console.log(simpleDistance); // 3944416 (slightly different)Calculates the shortest distance from a point to a line segment.
/**
* Calculate shortest distance from point to line segment
* @param point - Point coordinate
* @param lineStart - Line segment start coordinate
* @param lineEnd - Line segment end coordinate
* @returns Distance in meters from point to line
*/
function getDistanceFromLine(
point: GeolibInputCoordinates,
lineStart: GeolibInputCoordinates,
lineEnd: GeolibInputCoordinates
): number;Usage Examples:
import { getDistanceFromLine } from "geolib";
// Distance from point to road segment
const distanceToRoad = getDistanceFromLine(
{ lat: 51.516, lng: 7.456 }, // Point location
{ lat: 51.515, lng: 7.45 }, // Road start
{ lat: 51.517, lng: 7.46 } // Road end
);
console.log(distanceToRoad); // Distance in metersCalculates the total length of a path through multiple points.
/**
* Calculate total length of path through multiple points
* @param points - Array of coordinates defining the path
* @param distanceFn - Optional custom distance function (default: getDistance)
* @returns Total path length in meters
*/
function getPathLength(
points: GeolibInputCoordinates[],
distanceFn?: (
from: GeolibInputCoordinates,
to: GeolibInputCoordinates
) => number
): number;Usage Examples:
import { getPathLength, getPreciseDistance } from "geolib";
// Calculate route length
const routePoints = [
{ lat: 51.515, lng: 7.453 },
{ lat: 51.516, lng: 7.454 },
{ lat: 51.517, lng: 7.455 },
{ lat: 51.518, lng: 7.456 }
];
const routeLength = getPathLength(routePoints);
console.log(routeLength); // Total distance in meters
// Using high-precision distance function
const preciseRouteLength = getPathLength(routePoints, getPreciseDistance);
console.log(preciseRouteLength);