1.0.6 • Published 7 months ago

@shgysk8zer0/geoutils v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

geoutils

A utility library for geolocation and geohashing in JavaScript.

CodeQL Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Installation

npm install @shgysk8zer0/geoutils

Usage

import { getDistance, encodeGeohash, decodeGeohash, getGeohashDistance, checkLocation, checkGeohash, getGeohashBounds, estimateGeohashAccuracy } from '@shgysk8zer0/geoutils';

// Example usage
const distance = getDistance(
  { latitude: 40.7128, longitude: -74.0060 },
  { latitude: 34.0522, longitude: -118.2437 }
);

const geohash = encodeGeohash({ latitude: 40.7128, longitude: -74.0060 });
const coordinates = decodeGeohash(geohash);
const geohashDistance = getGeohashDistance('dr5ru7', '9q5ctr');
const isWithinRadius = checkLocation(
  { latitude: 40.7128, longitude: -74.0060 },
  { latitude: 34.0522, longitude: -118.2437 },
  { radius: 5000000 }
);
const geohashBounds = getGeohashBounds('dr5ru7');
const geohashAccuracy = estimateGeohashAccuracy('dr5ru7');

API

getDistance(coords1, coords2, options)

ParameterTypeDescription
coords1objectObject containing latitude and longitude of the first location.
coords1.latitudenumberLatitude of the first location.
coords1.longitudenumberLongitude of the first location.
coords2objectObject containing latitude and longitude of the second location.
coords2.latitudenumberLatitude of the second location.
coords2.longitudenumberLongitude of the second location.
optionsobjectOptional parameters.
options.highAccuracybooleanWhether to use high accuracy (Haversine formula) or lower accuracy approximation.

Returns the distance in meters.

encodeGeohash(location, precision)

ParameterTypeDescription
locationobjectObject containing latitude and longitude.
location.latitudenumberLatitude of the location.
location.longitudenumberLongitude of the location.
precisionnumberDesired geohash precision (length).

Returns the geohash string.

decodeGeohash(geohash)

ParameterTypeDescription
geohashstringThe geohash to parse.

Returns an object with latitude and longitude.

getGeohashDistance(geohash1, geohash2, options)

ParameterTypeDescription
geohash1stringThe first geohash.
geohash2stringThe second geohash.
optionsobjectOptional parameters.
options.highAccuracybooleanWhether to use high accuracy (Haversine formula) or lower accuracy approximation.

Returns the distance in meters.

checkLocation(location, location2, options)

ParameterTypeDescription
locationobjectObject containing latitude and longitude of the target location.
location.latitudenumberLatitude of the target location.
location.longitudenumberLongitude of the target location.
location2objectObject containing latitude and longitude of the reference location.
location2.latitudenumberLatitude of the reference location.
location2.longitudenumberLongitude of the reference location.
optionsobjectOptional parameters.
options.radiusnumberRadius in meters to check within.
options.highAccuracybooleanWhether to use high accuracy (Haversine formula) or lower accuracy approximation.

Returns true if the location is within the specified radius, false otherwise.

checkGeohash(geohash, coords, options)

ParameterTypeDescription
geohashstringThe geohash to check against.
coordsobjectObject containing latitude and longitude of the coordinates.
coords.latitudenumberLatitude of the coordinates.
coords.longitudenumberLongitude of the coordinates.
optionsobjectOptional parameters.
options.highAccuracybooleanWhether to use high accuracy (Haversine formula) or lower accuracy approximation.
options.radiusnumberRadius in kilometers to check within.

Returns true if the geohash is within the specified radius, false otherwise.

getGeohashBounds(geohash)

ParameterTypeDescription
geohashstringThe geohash to get bounds for.

Returns an object with latitude and longitude bounds.

estimateGeohashAccuracy(geohash)

ParameterTypeDescription
geohashstringThe geohash to estimate accuracy for.

Returns the estimated accuracy in meters.

1.0.6

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago