Geolib

Zero dependency library to provide some basic geo functions

README

Geolib


Library to provide basic geospatial operations like distance calculation, conversion of decimal coordinates to sexagesimal and vice versa, etc. This library is currently 2D, meaning that altitude/elevation is not yet supported by any of its functions!
CircleCI
undefined
undefined
undefined styled with prettier

Changelog


A detailed changelog can be found in CHANGELOG.md

Install


  1. ```sh
  2. npm install geolib
  3. ```

  1. ```sh
  2. yarn add geolib
  3. ```

Usage


There is a UMD build and an ES Module build. You can either use the UMD build in Node like any other library:

  1. ``` js
  2. const geolib = require('geolib');
  3. ```

or in the browser by using a simple script element:

  1. ``` html
  2. <script src="lib/geolib.js"></script>
  3. ```

If you load it in the browser, you can access all the functions via window.geolib.

If you're working with a bundler (like Webpack or Parcel) or have an environment that supports ES Modules natively, you can either import certain functions from the package directly:

  1. ``` js
  2. import { getDistance } from 'geolib';
  3. ```

or load the whole library:

  1. ``` js
  2. import * as geolib from 'geolib';
  3. ```

or you can import single functions directly to potentially make use of treeshaking (recommended):

  1. ``` js
  2. import getDistance from 'geolib/es/getDistance';
  3. ```

General


This library is written in TypeScript. You don't have to know TypeScript to use Geolib but the type definitions give you valuable information about the general usage, input parameters etc.

Supported values and formats


All methods that are working with coordinates accept either an object with a lat/latitude and a lon/lng/longitude property, or a GeoJSON coordinates array, like: [lon, lat]. All values can be either in decimal (53.471) or sexagesimal (53° 21' 16") format.

Distance values are always floats and represent the distance in meters.

Functions


getDistance(start, end, accuracy = 1)


Calculates the distance between two geo coordinates.

This function takes up to 3 arguments. First 2 arguments must be valid GeolibInputCoordinates (e.g. {latitude: 52.518611, longitude: 13.408056}). Coordinates can be in sexagesimal or decimal format. The third argument is accuracy (in meters). By default the accuracy is 1 meter. If you need a more accurate result, you can set it to a lower value, e.g. to 0.01 for centimeter accuracy. You can set it higher to have the result rounded to the next value that is divisible by your chosen accuracy (e.g. 25428 with an accuracy of 100 becomes 25400).

  1. ``` js
  2. getDistance(
  3.     { latitude: 51.5103, longitude: 7.49347 },
  4.     { latitude: "51° 31' N", longitude: "7° 28' E" }
  5. );
  6. ```

  1. ``` js
  2. // Working with W3C Geolocation API
  3. navigator.geolocation.getCurrentPosition(
  4.     (position) => {
  5.         console.log(
  6.             'You are ',
  7.             geolib.getDistance(position.coords, {
  8.                 latitude: 51.525,
  9.                 longitude: 7.4575,
  10.             }),
  11.             'meters away from 51.525, 7.4575'
  12.         );
  13.     },
  14.     () => {
  15.         alert('Position could not be determined.');
  16.     }
  17. );
  18. ```

Returns the distance in meters as a numeric value.

getPreciseDistance(start, end, accuracy = 1)


Calculates the distance between two geo coordinates. This method is more accurate then getDistance, especially for long distances but it is also slower. It is using the Vincenty inverse formula for ellipsoids.

It takes the same (up to 3) arguments as getDistance.

  1. ``` js
  2. geolib.getPreciseDistance(
  3.     { latitude: 51.5103, longitude: 7.49347 },
  4.     { latitude: "51° 31' N", longitude: "7° 28' E" }
  5. );
  6. ```

getCenter(coords)


Calculates the geographical center of all points in a collection of geo coordinates. Takes an array of coordinates and calculates the center of it.

  1. ``` js
  2. geolib.getCenter([
  3.     { latitude: 52.516272, longitude: 13.377722 },
  4.     { latitude: 51.515, longitude: 7.453619 },
  5.     { latitude: 51.503333, longitude: -0.119722 },
  6. ]);
  7. ```

Returns an object:

  1. ``` js
  2. {
  3.     "latitude": centerLat,
  4.     "longitude": centerLon
  5. }
  6. ```

getCenterOfBounds(coords)


Calculates the center of the bounds of geo coordinates.

Takes an array of coordinates, calculate the border of those, and gives back the center of that rectangle.

On polygons like political borders (eg. states), this may gives a closer result to human expectation, than getCenter, because that function can be disturbed by uneven distribution of point in different sides.

Imagine the US state Oklahoma: getCenter on that gives a southern point, because the southern border contains a lot more nodes, than the others.

  1. ``` js
  2. geolib.getCenterOfBounds([
  3.     { latitude: 51.513357512, longitude: 7.45574331 },
  4.     { latitude: 51.515400598, longitude: 7.45518541 },
  5.     { latitude: 51.516241842, longitude: 7.456494328 },
  6.     { latitude: 51.516722545, longitude: 7.459863183 },
  7.     { latitude: 51.517443592, longitude: 7.463232037 },
  8. ]);
  9. ```

Returns an object:

  1. ``` js
  2. {
  3.     "latitude": centerLat,
  4.     "longitude": centerLng
  5. }
  6. ```

getBounds(points)


Calculates the bounds of geo coordinates.

  1. ``` js
  2. geolib.getBounds([
  3.     { latitude: 52.516272, longitude: 13.377722 },
  4.     { latitude: 51.515, longitude: 7.453619 },
  5.     { latitude: 51.503333, longitude: -0.119722 },
  6. ]);
  7. ```

It returns minimum and maximum latitude and minimum and maximum longitude as an object:

  1. ``` js
  2. {
  3.     "minLat": minimumLatitude,
  4.     "maxLat": maximumLatitude,
  5.     "minLng": minimumLongitude,
  6.     "maxLng": maximumLongitude,
  7. }
  8. ```

isPointInPolygon(point, polygon)


Checks whether a point is inside of a polygon or not.

  1. ``` js
  2. geolib.isPointInPolygon({ latitude: 51.5125, longitude: 7.485 }, [
  3.     { latitude: 51.5, longitude: 7.4 },
  4.     { latitude: 51.555, longitude: 7.4 },
  5.     { latitude: 51.555, longitude: 7.625 },
  6.     { latitude: 51.5125, longitude: 7.625 },
  7. ]);
  8. ```

Returns true or false

isPointWithinRadius(point, centerPoint, radius)


Checks whether a point is inside of a circle or not.

  1. ``` js
  2. // checks if 51.525/7.4575 is within a radius of 5 km from 51.5175/7.4678
  3. geolib.isPointWithinRadius(
  4.     { latitude: 51.525, longitude: 7.4575 },
  5.     { latitude: 51.5175, longitude: 7.4678 },
  6.     5000
  7. );
  8. ```

Returns true or false

getRhumbLineBearing(origin, destination)


Gets rhumb line bearing of two points. Find out about the difference between rhumb line and great circle bearing on Wikipedia. Rhumb line should be fine in most cases:

http://en.wikipedia.org/wiki/Rhumb_line#General_and_mathematical_description

Function is heavily based on Doug Vanderweide's great PHP version (licensed under GPL 3.0)
http://www.dougv.com/2009/07/13/calculating-the-bearing-and-compass-rose-direction-between-two-latitude-longitude-coordinates-in-php/

  1. ``` js
  2. geolib.getRhumbLineBearing(
  3.     { latitude: 52.518611, longitude: 13.408056 },
  4.     { latitude: 51.519475, longitude: 7.46694444 }
  5. );
  6. ```

Returns calculated bearing as number.

getGreatCircleBearing(origin, destination)


Gets great circle bearing of two points. This is more accurate than rhumb line bearing but also slower.

  1. ``` js
  2. geolib.getGreatCircleBearing(
  3.     { latitude: 52.518611, longitude: 13.408056 },
  4.     { latitude: 51.519475, longitude: 7.46694444 }
  5. );
  6. ```

Returns calculated bearing as number.

getCompassDirection(origin, destination, bearingFunction = getRhumbLineBearing)


Gets the compass direction from an origin coordinate to a destination coordinate. Optionally a function to determine the bearing can be passed as third parameter. Default is getRhumbLineBearing.

  1. ``` js
  2. geolib.getCompassDirection(
  3.     { latitude: 52.518611, longitude: 13.408056 },
  4.     { latitude: 51.519475, longitude: 7.46694444 }
  5. );
  6. ```

Returns the direction (e.g. NNE, SW, E, …) as string.

orderByDistance(point, arrayOfPoints)


Sorts an array of coords by distance to a reference coordinate.

  1. ``` js
  2. geolib.orderByDistance({ latitude: 51.515, longitude: 7.453619 }, [
  3.     { latitude: 52.516272, longitude: 13.377722 },
  4.     { latitude: 51.518, longitude: 7.45425 },
  5.     { latitude: 51.503333, longitude: -0.119722 },
  6. ]);
  7. ```

Returns an array of points ordered by their distance to the reference point.

findNearest(point, arrayOfPoints)


Finds the single one nearest point to a reference coordinate. It's actually just a convenience method that uses orderByDistance under the hood and returns the first result.

  1. ``` js
  2. geolib.findNearest({ latitude: 52.456221, longitude: 12.63128 }, [
  3.     { latitude: 52.516272, longitude: 13.377722 },
  4.     { latitude: 51.515, longitude: 7.453619 },
  5.     { latitude: 51.503333, longitude: -0.119722 },
  6.     { latitude: 55.751667, longitude: 37.617778 },
  7.     { latitude: 48.8583, longitude: 2.2945 },
  8.     { latitude: 59.3275, longitude: 18.0675 },
  9.     { latitude: 59.916911, longitude: 10.727567 },
  10. ]);
  11. ```

Returns the point nearest to the reference point.

getPathLength(points, distanceFunction = getDistance)


Calculates the length of a collection of coordinates. Expects an array of points as first argument and optionally a function to determine the distance as second argument. Default is getDistance.

  1. ``` js
  2. geolib.getPathLength([
  3.     { latitude: 52.516272, longitude: 13.377722 },
  4.     { latitude: 51.515, longitude: 7.453619 },
  5.     { latitude: 51.503333, longitude: -0.119722 },
  6. ]);
  7. ```

Returns the length of the path in meters as number.

getDistanceFromLine(point, lineStart, lineEnd, accuracy = 1)


Gets the minimum distance from a point to a line of two points.

  1. ``` js
  2. geolib.getDistanceFromLine(
  3.     { latitude: 51.516, longitude: 7.456 },
  4.     { latitude: 51.512, longitude: 7.456 },
  5.     { latitude: 51.516, longitude: 7.459 }
  6. );
  7. ```

Returns the shortest distance to the given line as number.

Note: if all points are too close together the function might return NaN. In this case it usually helps to slightly increase the accuracy (e.g. 0.01).

getBoundsOfDistance(point, distance)


Computes the bounding coordinates of all points on the surface of the earth less than or equal to the specified great circle distance.

  1. ``` js
  2. geolib.getBoundsOfDistance(
  3.     { latitude: 34.090166, longitude: -118.276736555556 },
  4.     1000
  5. );
  6. ```

Returns an array with the southwestern and northeastern coordinates.

isPointInLine(point, lineStart, lineEnd)


Calculates if given point lies in a line formed by start and end.

  1. ``` js
  2. geolib.isPointInLine(
  3.     { latitude: 0, longitude: 10 },
  4.     { latitude: 0, longitude: 0 },
  5.     { latitude: 0, longitude: 15 }
  6. );
  7. ```

sexagesimalToDecimal(value)


Converts a sexagesimal coordinate into decimal format

  1. ``` js
  2. geolib.sexagesimalToDecimal(`51° 29' 46" N`);
  3. ```

Returns the new value as decimal number.

decimalToSexagesimal(value)


Converts a decimal coordinate to sexagesimal format

  1. ``` js
  2. geolib.decimalToSexagesimal(51.49611111); // -> 51° 29' 46`
  3. ```

Returns the new value as sexagesimal string.

geolib.getLatitude(point, raw = false)


geolib.getLongitude(point, raw = false)


Returns the latitude/longitude for a given point and converts it to decimal. If the second argument is set to true it does not convert the value to decimal.

  1. ``` js
  2. geolib.getLatitude({ lat: 51.49611, lng: 7.38896 }); // -> 51.49611
  3. geolib.getLongitude({ lat: 51.49611, lng: 7.38896 }); // -> 7.38896
  4. ```

Returns the value as decimal or in its original format if the second argument was set to true.

toDecimal(point)


Checks if a coordinate is already in decimal format and, if not, converts it to. Works with single values (e.g. 51° 32' 17") and complete coordinates (e.g. {lat: 1, lon: 1}) as long as it in a supported format.

  1. ``` js
  2. geolib.toDecimal(`51° 29' 46" N`); // -> 51.59611111
  3. geolib.toDecimal(51.59611111); // -> 51.59611111
  4. ```

Returns a decimal value for the given input value.

computeDestinationPoint(point, distance, bearing, radius = earthRadius)


Computes the destination point given an initial point, a distance (in meters) and a bearing (in degrees). If no radius is given it defaults to the mean earth radius of 6,371,000 meters.

Attention: this formula is not _100%_ accurate (but very close though).

  1. ``` js
  2. geolib.computeDestinationPoint(
  3.     { latitude: 52.518611, longitude: 13.408056 },
  4.     15000,
  5.     180
  6. );
  7. ```

  1. ``` js
  2. geolib.computeDestinationPoint(
  3.     [13.408056, 52.518611]
  4.     15000,
  5.     180
  6. );
  7. ```

Returns the destination in the same format as the input coordinates. So if you pass a GeoJSON point, you will get a GeoJSON point.

getAreaOfPolygon(points)


Calculates the surface area of a polygon.

  1. ``` js
  2. geolib.getAreaOfPolygon([
  3.     [7.453635617650258, 51.49320556213869],
  4.     [7.454583481047989, 51.49328893754685],
  5.     [7.454778172179346, 51.49240881084831],
  6.     [7.453832678225655, 51.49231619246726],
  7.     [7.453635617650258, 51.49320556213869],
  8. ]);
  9. ```

Returns the result as number in square meters.

getCoordinateKeys(point)


Gets the property names of that are used in the point in a normalized form:

  1. ``` js
  2. geolib.getCoordinateKeys({ lat: 1, lon: 1 });
  3. // -> { latitude: 'lat', longitude: 'lon' }
  4. ```

Returns an object with a latitude and a longitude property. Their values are the property names for latitude and longitude that are used in the passed point. Should probably only be used internally.

getCoordinateKey(point, keysToLookup)


Is used by getCoordinateKeys under the hood and returns the property name out of a list of possible names.

  1. ``` js
  2. geolib.getCoordinateKey({ latitude: 1, longitude: 2 }, ['lat', 'latitude']);
  3. // -> latitude
  4. ```

Returns the name of the property as string or undefined if no there was no match.

isValidCoordinate(point)


Checks if a given point has at least a latitude and a longitude and is in a supported format.

  1. ``` js
  2. // true:
  3. geolib.isValidCoordinate({ latitude: 1, longitude: 2 });

  4. // false, longitude is missing:
  5. geolib.isValidCoordinate({ latitude: 1 });

  6. // true, GeoJSON format:
  7. geolib.isValidCoordinate([2, 1]);
  8. ```

Returns true or false.

getSpeed(startPointWithTime, endPointWithTime)


Calculates the speed between two points within a given time span.

  1. ``` js
  2. geolib.getSpeed(
  3.     { latitude: 51.567294, longitude: 7.38896, time: 1360231200880 },
  4.     { latitude: 52.54944, longitude: 13.468509, time: 1360245600880 }
  5. );
  6. ```

Return the speed in meters per second as number.

convertSpeed(value, unit)


Converts the result from getSpeed into a more human friendly format. Currently available units are mph and kmh.

Units


unit can be one of:

-   kmh (kilometers per hour)
-   mph (miles per hour)

  1. ``` js
  2. geolib.convertSpeed(29.8678, 'kmh'));
  3. ```

Returns the converted value as number.

convertDistance(value, unit)


Converts a given distance (in meters) into another unit.

Units


unit can be one of:

-   m (meter)
-   km (kilometers)
-   cm (centimeters)
-   mm (millimeters)
-   mi (miles)
-   sm (seamiles)
-   ft (feet)
-   in (inches)
-   yd (yards)

  1. ``` js
  2. geolib.convertDistance(14200, 'km'); // 14.2
  3. geolib.convertDistance(500, 'km'); // 0.5
  4. ```

Returns the converted distance as number.

convertArea(value, unit)


Converts the result from getAreaForPolygon into a different unit.

Units


unit can be one of:

-   m2, sqm (square meters)
-   km2, sqkm (square kilometers)
-   ha (hectares)
-   a (ares)
-   ft2, sqft (square feet)
-   yd2, sqyd (square yards)
-   in2, sqin (square inches)

  1. ``` js
  2. geolib.convertArea(298678, 'km2'));
  3. ```

Returns the converted area as number.

wktToPolygon(wkt)


Converts the Well-known text (a.k.a WKT) to polygon that Geolib understands.

  1. ``` js
  2. geolib.wktToPolygon('POLYGON ((30 10.54321, 40 40, 20 40, 10 20, 30 10))');
  3. // [
  4. //     { latitude: 10.54321, longitude: 30 },
  5. //     { latitude: 40, longitude: 40 },
  6. //     { latitude: 40, longitude: 20 },
  7. //     { latitude: 20, longitude: 10 },
  8. //     { latitude: 10, longitude: 30 },}
  9. // ]
  10. ```

Returns the array of coordinates.

Breaking Changes in 3.0.0 and migration from 2.x.x


In version 3.0.0 I'm trying to get a little bit _back to the roots_. Geolib was once started because I needed a handful of methods to perform very specific geo related tasks like getting the distance or the direction between two points. Since it was one of the very first libraries on npm back then to do these kind of things in a very simple way it became very popular (with more than 300k downloads per month as of April 2019!) and as a consequence got a lot of contributions over the years. Many of which I just merged as long as they had accompanying tests, without looking at consistency, conventions, complexity, coding style or even the overall quality of the functions that I sometimes didn't even fully understand.

I have now cleaned up the codebase completely, rebuilt the entire library "from scratch", unified all the function arguments, removed a few functions where I wasn't sure if they should be in here (feel free to add them back of you're using them!) or if they were even used (did a few searches on GitHub for the function names, turned out there are zero results).

Elevation support was dropped, as well as a few functions that unnecessarily made the library really large in size (e.g. isPointInsideRobust alone was over 700[!] lines of code and was basically taken from a different library).

I removed Grunt from the build process, added "modern" tools like ESLint and Prettier. I switched from Travis CI to Circle CI and I am in the process of further automating the release process of new versions using semantic-release and conventional-commits. I also switched from pure JavaScript to TypeScript because I think it does have some benefits.

-   All functions are pure functions now. No input data is mutated anymore. You give the same input, you get the same output. No side effects or whatsoever.
-   I changed the default getDistance function from being the slow, accurate one to being the fast, slightly inaccurate one. The old getDistance function is now named getPreciseDistance while the old getDistanceSimple function is now the default getDistance function. You can, however, pass getPreciseDistance as argument to any function that uses distance calculation internally.
-   Artificial limitation to 8 decimal places in decimal coordinates was removed
-   getBoundsOfDistance() now returns the _exact_ coordinates due to the removal of the artificial 8 decimal place limitation
-   getCompassDirection() does no longer return an object with an _exact_ and a _rough_ direction but only the exact direction as string
-   third argument to getCompassDirection() is no longer a string ("circle", "line") but a function to determine the bearing (you can pass getRhumbLineBearing or getGreatCircleBearing). The function receives the origin and the destination as first and second argument. If no 3rd argument was given, getRhumbLineBearing(origin, dest) is used by default.
-   There is now a new helper function roughCompassDirection(exact) if you _really_ only need a very rough (and potentially inaccurate or inappropriate) direction. Better don't use it.
-   orderByDistance() does no longer modify its input so does not add a distance and key property to the returned coordinates.
-   The result of getSpeed() is now always returned as meters per second. It can be converted using the new convenience function convertSpeed(mps, targetUnit)
-   Relevant value (usually point or distance) is now consistently the first argument for each function (it wasn't before, how confusing is that?)
-   findNearest() does no longer take offset and limit arguments. It's only a convenience method to get the single one nearest point from a set of coordinates. If you need more than one, have a look at the implementation and implement your own logic using orderByDistance
-   Whereever distances are involved, they are returned as meters or meters per second. No more inconsistent defaults like kilometers or kilometers per hour.
-   The method how sexagesimal is formatted differs a little bit. It may now potentially return ugly float point units like 52° 46' 21.0004" in rare cases but it is also more accurate than it was before.
-   Dropped support for Meteor (feel free to add it back if you like)

✅ Functions with the same name


-   computeDestinationPoint
-   getBounds
-   getBoundsOfDistance
-   getCenter
-   getCenterOfBounds
-   getCompassDirection
-   getDistanceFromLine
-   getPathLength
-   getRhumbLineBearing
-   getSpeed
-   isDecimal
-   isPointInLine
-   isPointNearLine
-   isSexagesimal
-   orderByDistance

❗ Renamed functions


-   getKeys renamed to getCoordinateKeys
-   validate renamed to isValidCoordinate
-   getLat renamed to getLatitude
-   getLon renamed to getLongitude
-   latitude -> renamed to getLatitude
-   longitude -> renamed to getLongitude
-   convertUnit -> remamed to convertDistance, because name was too ambiguous
-   useDecimal renamed to toDecimal
-   decimal2sexagesimal renamed to decimalToSexagesimal
-   sexagesimal2decimal renamed to sexagesimalToDecimal
-   getDistance renamed to getPreciseDistance
-   getDistanceSimple renamed to getDistance
-   isPointInside renamed to isPointInPolygon
-   isPointInCircle renamed to isPointWithinRadius
-   getBearing renamed to getGreatCircleBearing to be more explicit

🗑 Removed functions


-   getElev -> removed
-   elevation -> removed
-   coords -> removed (might be re-added as getCoordinate or getNormalizedCoordinate)
-   ll -> removed (because wtf?)
-   preparePolygonForIsPointInsideOptimized -> removed due to missing documentation and missing tests
-   isPointInsideWithPreparedPolygon -> removed due to missing documentation
-   isInside alias -> removed (too ambiguous) - use isPointInPolygon or isPointWithinRadius
-   withinRadius -> removed, use isPointWithinRadius
-   getDirection alias -> removed (unnecessary clutter) - use getCompassDirection

🆕 Added functions


-   getAreaOfPolygon to calculate the area of a polygon
-   getCoordinateKey to get a property name (e.g. lat or lng of an object based on an array of possible names)