npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jollie/geohash

v1.0.13

Published

GeoHash encode/decode that support MongoDB, ElasticSearch and GeoJSON formats for convenience

Downloads

14

Readme

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 :

  • GeoJSON https://en.wikipedia.org/wiki/GeoJSON
  • MongoDB https://docs.mongodb.com/manual/reference/geojson/#point
  • Elasticsearch https://www.elastic.co/guide/en/elasticsearch/reference/7.6/geo-point.html
  • { longitude, latitude } object commonly used in some packages

See list of supported formats

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

Install

yarn add @jollie/geohash

or

npm install @jollie/geohash

Usage

import GeoHash from '@jollie/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
//   }
// }

// Neightbors
GeoHash.neighbors('sndbuh');
// Return array of 8 neightbors n, ne, e, se, s, sw, w, nw 
// ['sndbuj','sndbum','sndbuk','sndbu7','sndbu5','sndbgg','sndbgu','sndbgv']

// Bbox (bounding box)
GeoHash.bbox('sndbuh');
// Return array [min_lon, min_lat, max_lon, max_lat]

Params

GeoHash.encode(location, len);

| Prop | Type | Note | |------------|---------------------------------|---------------------------------------------------------------------| | location | object or array or string | See list of supported formats | | len | string | geohash length(affect the precision of geohash) |

Return a GeoHash of length chars


GeoHash.decode(hash);

| Prop | Type | Note | |--------|----------|-------------------| | hash | string | Geohash 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,
}