1.0.9 • Published 5 years ago

@thejellyfish/geohash v1.0.9

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Version Licence Build Coverage Downloads

GeoHash encode/decode

More infos about GeoHash https://en.wikipedia.org/wiki/Geohash

GeoHash encode/decode with support of MongoDB, ElasticSearch and GeoJSON formats :

See list of supported formats

Inspired by https://github.com/sunng87/node-geohash.

Install

yarn add @thejellyfish/geohash

or

npm install @thejellyfish/geohash

Usage

import GeoHash from '@thejellyfish/geohash';

// ... coordinates example
const longitude = 4.2122126;
const latitude = 36.4511093;

// Encode a GeoJSON Point
GeoHash.encode({
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [4.2122126, 36.4511093]
  },
}, 7);

// Encode a MongoDB GeoJSON Point
GeoHash.encode({
  type: 'Point',
  coordinates: [4.2122126, 36.4511093]
}, 7);

// Encode an Elasticsearch GeoPoint as an object
GeoHash.encode({ lon: 4.2122126, lat: 36.4511093 }, 7);

// Encode an Elasticsearch GeoPoint as string
GeoHash.encode('36.4511093,4.2122126', 7);

// Encode an Elasticsearch GeoPoint as an array
GeoHash.encode([4.2122126, 36.4511093], 7);

// Encode an Elasticsearch GeoPoint as WKT POINT primitive
GeoHash.encode('POINT (4.2122126 36.4511093)', 7);

// Encode { longitude, latitude } object
GeoHash.encode({ longitude: 4.2122126, latitude: 36.4511093 }, 7);

// Decode
GeoHash.decode('sn6zrge');
// Return 
// {
//   type: 'Feature',
//   geometry: {
//     type: 'Point',
//     coordinates: [ 4.2125701904296875, 36.45057678222656 ]
//   },
//   properties: {
//     longitude_error: 0.0006866455078125,
//     latitude_error: 0.0006866455078125
//   }
// }

Params

GeoHash.encode(location, len);
PropTypeNote
locationobject or array or stringSee list of supported formats
lenstringgeohash length(affect the precision of geohash)

Return a GeoHash of length chars

GeoHash.decode(hash); 
PropTypeNote
hashstringGeohash to decode

Return a GeoJSON

{
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [lon, lat],  // Coordinates decoded
  },
  properties: {
   longitude_error,  // Longitude error 
   latitude_error,   // Latitude error
  },
}

Exception

Throw TypeError if bad location parameter

Supported formats

GeoJSON :

{
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [longitude, latitude]
  },
}

GeoJSON Point (specially used in MongoDB) :

{
  type: 'Point',
  coordinates: [longitude, latitude]
}

Elasticsearch GeoPoint as an object :

{
  lon: longitude,
  lat: latitude,
}

Elasticsearch GeoPoint as string :

'latitude,longitude'

Elasticsearch GeoPoint as array :

[longitude, latitude]

Elasticsearch GeoPoint as WKT POINT primitive :

'POINT (longitude,latitude)'

object :

{
  longitude,
  latitude,
}
1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago