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 calculating centers, bounds, areas, and other geometric properties of coordinate sets.
Calculates the geometric center (centroid) of multiple points using 3D Cartesian coordinates.
/**
* Calculate geometric center of multiple points
* @param points - Array of coordinates
* @returns Center point coordinates or false if invalid input
*/
function getCenter(points: GeolibInputCoordinates[]):
{ longitude: number; latitude: number } | false;Usage Examples:
import { getCenter } from "geolib";
// Find center of multiple locations
const locations = [
{ latitude: 52.516272, longitude: 13.377722 }, // Berlin
{ latitude: 51.515, longitude: 7.453619 }, // Dortmund
{ latitude: 51.503, longitude: -0.119 } // London
];
const center = getCenter(locations);
console.log(center); // { longitude: 6.904114, latitude: 51.678095 }
// Handle invalid input
const invalidCenter = getCenter([]);
console.log(invalidCenter); // falseCalculates the minimum bounding rectangle that contains all given points.
/**
* Calculate bounding box of multiple points
* @param points - Array of coordinates (must not be empty)
* @returns Bounding box with max/min latitude and longitude
* @throws Error if no points provided
*/
function getBounds(points: GeolibInputCoordinates[]): GeolibBounds;
interface GeolibBounds {
maxLat: number;
minLat: number;
maxLng: number;
minLng: number;
}Usage Examples:
import { getBounds } from "geolib";
// Calculate bounds of a region
const regionPoints = [
{ lat: 51.515, lng: 7.453 },
{ lat: 51.516, lng: 7.454 },
{ lat: 51.517, lng: 7.455 },
{ lat: 51.514, lng: 7.452 }
];
const bounds = getBounds(regionPoints);
console.log(bounds);
// {
// maxLat: 51.517,
// minLat: 51.514,
// maxLng: 7.455,
// minLng: 7.452
// }Calculates the bounding box of all points within a specified distance from a center point.
/**
* Calculate bounding box of points within distance from center
* @param point - Center point coordinate
* @param distance - Radius distance in meters
* @returns Array with southwest and northeast corner coordinates
*/
function getBoundsOfDistance(
point: GeolibInputCoordinates,
distance: number
): [
{ latitude: number; longitude: number },
{ latitude: number; longitude: number }
];Usage Examples:
import { getBoundsOfDistance } from "geolib";
// Get bounds of 1km radius around a point
const centerPoint = { latitude: 51.515, longitude: 7.453 };
const radius = 1000; // 1km in meters
const [southwest, northeast] = getBoundsOfDistance(centerPoint, radius);
console.log("SW:", southwest); // { latitude: 51.506, longitude: 7.438 }
console.log("NE:", northeast); // { latitude: 51.524, longitude: 7.468 }Calculates the center point of a bounding box.
/**
* Calculate center point of bounding box
* @param bounds - Bounding box object
* @returns Center point coordinates
*/
function getCenterOfBounds(bounds: GeolibBounds):
{ latitude: number; longitude: number };Usage Examples:
import { getCenterOfBounds, getBounds } from "geolib";
// First get bounds, then find center
const points = [
{ lat: 51.515, lng: 7.453 },
{ lat: 51.517, lng: 7.455 }
];
const bounds = getBounds(points);
const center = getCenterOfBounds(bounds);
console.log(center); // { latitude: 51.516, longitude: 7.454 }Calculates the surface area of a polygon using spherical geometry.
/**
* Calculate surface area of polygon in square meters
* @param points - Array of coordinates defining polygon vertices
* @returns Area in square meters
*/
function getAreaOfPolygon(points: GeolibInputCoordinates[]): number;Usage Examples:
import { getAreaOfPolygon, convertArea } from "geolib";
// Calculate area of a park
const parkBoundary = [
{ lat: 51.515, lng: 7.453 },
{ lat: 51.516, lng: 7.453 },
{ lat: 51.516, lng: 7.455 },
{ lat: 51.515, lng: 7.455 },
{ lat: 51.515, lng: 7.453 } // Close the polygon
];
const areaInSquareMeters = getAreaOfPolygon(parkBoundary);
console.log(areaInSquareMeters); // e.g., 15000 (square meters)
// Convert to hectares
const areaInHectares = convertArea(areaInSquareMeters, "ha");
console.log(areaInHectares); // e.g., 1.5 (hectares)Converts Well-Known Text (WKT) polygon format to coordinate arrays.
/**
* Convert Well-Known Text polygon to coordinate array
* @param wkt - WKT polygon string
* @returns Array of coordinates representing polygon vertices
*/
function wktToPolygon(wkt: string): GeolibInputCoordinates[];Usage Examples:
import { wktToPolygon, getAreaOfPolygon } from "geolib";
// Convert WKT polygon string
const wktString = "POLYGON((7.453 51.515, 7.454 51.515, 7.454 51.516, 7.453 51.516, 7.453 51.515))";
const polygon = wktToPolygon(wktString);
console.log(polygon);
// [
// { latitude: 51.515, longitude: 7.453 },
// { latitude: 51.515, longitude: 7.454 },
// { latitude: 51.516, longitude: 7.454 },
// { latitude: 51.516, longitude: 7.453 },
// { latitude: 51.515, longitude: 7.453 }
// ]
// Use with other functions
const area = getAreaOfPolygon(polygon);
console.log(area); // Area in square meters