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 testing spatial relationships between points, lines, and polygons.
Tests whether a point is inside a polygon using ray casting algorithm.
/**
* Test if point is inside polygon using ray casting algorithm
* @param point - Point coordinate to test
* @param polygon - Array of coordinates defining polygon vertices
* @returns True if point is inside polygon, false otherwise
*/
function isPointInPolygon(
point: GeolibInputCoordinates,
polygon: GeolibInputCoordinates[]
): boolean;Usage Examples:
import { isPointInPolygon } from "geolib";
// Define a polygon (square area)
const polygon = [
{ latitude: 51.513357, longitude: 7.45574 },
{ latitude: 51.515400, longitude: 7.45574 },
{ latitude: 51.515504, longitude: 7.47150 },
{ latitude: 51.513357, longitude: 7.47150 }
];
// Test if points are inside
const point1 = { latitude: 51.514, longitude: 7.46 };
const point2 = { latitude: 51.510, longitude: 7.46 };
console.log(isPointInPolygon(point1, polygon)); // true (inside)
console.log(isPointInPolygon(point2, polygon)); // false (outside)
// Works with GeoJSON format
const pointGeoJSON = [7.46, 51.514]; // [lng, lat]
console.log(isPointInPolygon(pointGeoJSON, polygon)); // trueTests whether a point is within a specified radius of a center point.
/**
* Test if point is within specified radius of center point
* @param point - Point coordinate to test
* @param center - Center point coordinate
* @param radius - Radius in meters
* @returns True if point is within radius, false otherwise
*/
function isPointWithinRadius(
point: GeolibInputCoordinates,
center: GeolibInputCoordinates,
radius: number
): boolean;Usage Examples:
import { isPointWithinRadius } from "geolib";
// Check if locations are within delivery radius
const restaurant = { latitude: 51.515, longitude: 7.453 };
const deliveryRadius = 2000; // 2km in meters
const customer1 = { latitude: 51.520, longitude: 7.460 };
const customer2 = { latitude: 51.530, longitude: 7.480 };
console.log(isPointWithinRadius(customer1, restaurant, deliveryRadius)); // true
console.log(isPointWithinRadius(customer2, restaurant, deliveryRadius)); // false
// Find nearby locations
const locations = [
{ name: "Location A", lat: 51.516, lng: 7.454 },
{ name: "Location B", lat: 51.520, lng: 7.460 },
{ name: "Location C", lat: 51.530, lng: 7.480 }
];
const nearbyLocations = locations.filter(loc =>
isPointWithinRadius(loc, restaurant, deliveryRadius)
);
console.log(nearbyLocations); // Only locations within 2kmTests whether a point lies exactly on a line segment.
/**
* Test if point lies on line segment
* @param point - Point coordinate to test
* @param lineStart - Line segment start coordinate
* @param lineEnd - Line segment end coordinate
* @returns True if point is on line, false otherwise
*/
function isPointInLine(
point: GeolibInputCoordinates,
lineStart: GeolibInputCoordinates,
lineEnd: GeolibInputCoordinates
): boolean;Usage Examples:
import { isPointInLine } from "geolib";
// Test if point is on road segment
const roadStart = { lat: 51.515, lng: 7.453 };
const roadEnd = { lat: 51.517, lng: 7.455 };
const checkPoint = { lat: 51.516, lng: 7.454 }; // Midpoint
console.log(isPointInLine(checkPoint, roadStart, roadEnd)); // true or falseTests whether a point is within a specified tolerance distance of a line segment.
/**
* Test if point is within tolerance distance of line segment
* @param point - Point coordinate to test
* @param lineStart - Line segment start coordinate
* @param lineEnd - Line segment end coordinate
* @param tolerance - Maximum distance in meters
* @returns True if point is within tolerance of line, false otherwise
*/
function isPointNearLine(
point: GeolibInputCoordinates,
lineStart: GeolibInputCoordinates,
lineEnd: GeolibInputCoordinates,
tolerance: number
): boolean;Usage Examples:
import { isPointNearLine } from "geolib";
// Check if GPS point is close to planned route
const routeStart = { lat: 51.515, lng: 7.453 };
const routeEnd = { lat: 51.517, lng: 7.455 };
const gpsPosition = { lat: 51.5155, lng: 7.4535 }; // Slightly off route
const tolerance = 50; // 50 meters tolerance
const isOnRoute = isPointNearLine(gpsPosition, routeStart, routeEnd, tolerance);
console.log(isOnRoute); // true if within 50m of routeFinds the closest coordinate from an array to a reference point.
/**
* Find nearest coordinate from array to reference point
* @param point - Reference point coordinate
* @param coords - Array of coordinates to search
* @returns Nearest coordinate from the array
*/
function findNearest(
point: GeolibInputCoordinates,
coords: GeolibInputCoordinates[]
): GeolibInputCoordinates;Usage Examples:
import { findNearest } from "geolib";
// Find nearest store location
const customerLocation = { latitude: 51.515, longitude: 7.453 };
const storeLocations = [
{ name: "Store A", lat: 51.516, lng: 7.454 },
{ name: "Store B", lat: 51.520, lng: 7.460 },
{ name: "Store C", lat: 51.510, lng: 7.450 }
];
const nearestStore = findNearest(customerLocation, storeLocations);
console.log(nearestStore); // Store with shortest distanceSorts coordinates by distance from a reference point.
/**
* Sort coordinates by distance from reference point
* @param point - Reference point coordinate
* @param coords - Array of coordinates to sort
* @param distanceFn - Optional custom distance function (default: getDistance)
* @returns Array of coordinates sorted by distance (nearest first)
*/
function orderByDistance(
point: GeolibInputCoordinates,
coords: GeolibInputCoordinates[],
distanceFn?: (
from: GeolibInputCoordinates,
to: GeolibInputCoordinates
) => number
): GeolibInputCoordinates[];Usage Examples:
import { orderByDistance, getPreciseDistance } from "geolib";
// Sort restaurants by distance from user
const userLocation = { latitude: 51.515, longitude: 7.453 };
const restaurants = [
{ name: "Restaurant A", lat: 51.520, lng: 7.460 },
{ name: "Restaurant B", lat: 51.510, lng: 7.450 },
{ name: "Restaurant C", lat: 51.516, lng: 7.454 }
];
const sortedRestaurants = orderByDistance(userLocation, restaurants);
console.log(sortedRestaurants); // Sorted by distance, nearest first
// Use high-precision distance function
const preciseSorted = orderByDistance(
userLocation,
restaurants,
getPreciseDistance
);