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
Functions for converting between different coordinate formats, including decimal to sexagesimal conversion and coordinate extraction utilities.
Converts decimal coordinates to degrees, minutes, seconds format.
/**
* Convert decimal coordinate to sexagesimal (DMS) format
* @param decimal - Decimal coordinate value
* @param format - Output format string (default: "DMS")
* @returns Formatted sexagesimal coordinate string
*/
function decimalToSexagesimal(decimal: number, format?: string): string;Usage Examples:
import { decimalToSexagesimal } from "geolib";
// Convert latitude to DMS format
const latDMS = decimalToSexagesimal(51.5103);
console.log(latDMS); // "51° 30′ 37″"
// Convert longitude with custom format
const lngDMS = decimalToSexagesimal(7.49347, "DM");
console.log(lngDMS); // "7° 29.608′"Converts sexagesimal (degrees, minutes, seconds) coordinates to decimal format.
/**
* Convert sexagesimal coordinate to decimal format
* @param sexagesimal - Sexagesimal coordinate string (e.g., "51° 30′ 37″ N")
* @returns Decimal coordinate value
*/
function sexagesimalToDecimal(sexagesimal: string): number;Usage Examples:
import { sexagesimalToDecimal } from "geolib";
// Convert DMS string to decimal
const decimal = sexagesimalToDecimal("51° 30′ 37″ N");
console.log(decimal); // 51.5103
// Convert longitude
const lngDecimal = sexagesimalToDecimal("7° 29′ 36″ E");
console.log(lngDecimal); // 7.4933Converts various coordinate formats to decimal, handling objects and arrays.
/**
* Convert coordinate value to decimal format
* @param value - Coordinate in various formats (object, array, string, number)
* @returns Converted coordinate in decimal format
*/
function toDecimal(value: any): any;Usage Examples:
import { toDecimal } from "geolib";
// Convert object coordinates
const decimalCoords = toDecimal({
lat: "51° 30′ 37″ N",
lng: "7° 29′ 36″ E"
});
console.log(decimalCoords); // { lat: 51.5103, lng: 7.4933 }
// Convert array coordinates
const arrayCoords = toDecimal(["7° 29′ 36″ E", "51° 30′ 37″ N"]);
console.log(arrayCoords); // [7.4933, 51.5103]Extract latitude and longitude values from coordinate objects with flexible property names.
/**
* Extract latitude value from coordinate object
* @param point - Coordinate object with lat/latitude property
* @param raw - If true, returns raw value without conversion (default: false)
* @returns Latitude value as number, or undefined if not found
*/
function getLatitude(point: GeolibInputCoordinates, raw?: boolean): number | undefined;
/**
* Extract longitude value from coordinate object
* @param point - Coordinate object with lng/lon/longitude property
* @param raw - If true, returns raw value without conversion (default: false)
* @returns Longitude value as number, or undefined if not found
*/
function getLongitude(point: GeolibInputCoordinates, raw?: boolean): number | undefined;Usage Examples:
import { getLatitude, getLongitude } from "geolib";
// Extract from various coordinate formats
const lat1 = getLatitude({ lat: 51.5103, lng: 7.49347 });
const lat2 = getLatitude({ latitude: 51.5103, longitude: 7.49347 });
const lat3 = getLatitude([7.49347, 51.5103]); // GeoJSON format
const lng1 = getLongitude({ lat: 51.5103, lng: 7.49347 });
const lng2 = getLongitude({ latitude: 51.5103, lon: 7.49347 });
console.log(lat1, lng1); // 51.5103, 7.49347
// Extract raw values (without decimal conversion)
const rawLat = getLatitude({ lat: "51° 30′ 37″ N" }, true);
console.log(rawLat); // "51° 30′ 37″ N" (raw string)
const convertedLat = getLatitude({ lat: "51° 30′ 37″ N" }); // raw = false (default)
console.log(convertedLat); // 51.5103 (converted to decimal)Functions for working with coordinate property names and extracting values by key.
/**
* Get coordinate key from point using flexible key lookup
* @param point - Coordinate object
* @param keysToLookup - Array of possible key names to search for
* @returns First matching key found, or undefined if none match
*/
function getCoordinateKey<Keys>(
point: GeolibInputCoordinates,
keysToLookup: Keys[]
): Keys | undefined;
/**
* Get all coordinate keys (latitude, longitude, altitude) from input
* @param point - Coordinate object
* @param keysToLookup - Optional custom key lookup configuration
* @returns Object with latitude, longitude, and optionally altitude keys
*/
function getCoordinateKeys(
point: GeolibInputCoordinates,
keysToLookup?: {
longitude: LongitudeKeys[];
latitude: LatitudeKeys[];
altitude: AltitudeKeys[];
}
): {
latitude: LatitudeKeys | undefined;
longitude: LongitudeKeys | undefined;
altitude?: AltitudeKeys | undefined;
};Usage Examples:
import { getCoordinateKey, getCoordinateKeys, latitudeKeys, longitudeKeys } from "geolib";
// Extract using flexible key lookup arrays
const point = { latitude: 51.5103, longitude: 7.49347 };
const latKey = getCoordinateKey(point, latitudeKeys); // Returns 'latitude'
const lngKey = getCoordinateKey(point, longitudeKeys); // Returns 'longitude'
// Works with different coordinate formats
const point2 = { lat: 51.5103, lng: 7.49347 };
const latKey2 = getCoordinateKey(point2, latitudeKeys); // Returns 'lat'
// Get all coordinate keys
const allKeys = getCoordinateKeys(point);
console.log(allKeys); // { latitude: 'latitude', longitude: 'longitude' }
// With custom key lookup
const customKeys = getCoordinateKeys(point2, {
latitude: ['lat', 'latitude'],
longitude: ['lng', 'lon', 'longitude'],
altitude: ['alt', 'altitude']
});
console.log(customKeys); // { latitude: 'lat', longitude: 'lng' }Basic mathematical conversion functions for angle calculations.
/**
* Convert degrees to radians
* @param degrees - Angle in degrees
* @returns Angle in radians
*/
function toRad(degrees: number): number;
/**
* Convert radians to degrees
* @param radians - Angle in radians
* @returns Angle in degrees
*/
function toDeg(radians: number): number;Usage Examples:
import { toRad, toDeg } from "geolib";
// Convert degrees to radians for trigonometric calculations
const radians = toRad(90);
console.log(radians); // 1.5708 (π/2)
// Convert back to degrees
const degrees = toDeg(Math.PI);
console.log(degrees); // 180