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

latlon-geohash

v2.0.0

Published

Gustavo Niemeyer’s geocoding system

Downloads

40,181

Readme

geohash

Functions to convert a geohash to/from a latitude/longitude point, and to determine bounds of a geohash cell and find neighbours of a geohash.

Note this version 2 uses ES classes and modules: for older browsers (or Node.js <8.0.0), v1.1.0 is ES5-based.

API

  • Geohash.encode(lat, lon, [precision]): encode latitude/longitude point to geohash of given precision (number of characters in resulting geohash); if precision is not specified, it is inferred from precision of latitude/longitude values.
  • Geohash.decode(geohash): return { lat, lon } of centre of given geohash, to appropriate precision.
  • Geohash.bounds(geohash): return { sw, ne } bounds of given geohash.
  • Geohash.adjacent(geohash, direction): return adjacent cell to given geohash in specified direction (N/S/E/W).
  • Geohash.neighbours(geohash): return all 8 adjacent cells (n/ne/e/se/s/sw/w/nw) to given geohash.

Note to obtain neighbours as an array, you can use

const neighboursObj = Geohash.neighbours(geohash);
const neighboursArr = Object.keys(neighboursObj).map(n => neighboursObj[n]);

The parent of a geocode is simply geocode.slice(0, -1).

If you want the geohash converted from Base32 to Base4, you can e.g.:

parseInt(Geohash.encode(52.20, 0.12, 6), 32).toString(4);

Usage in browser

Geohash can be used in the browser by taking a local copy, or loading it from jsDelivr: for example,

<!doctype html><title>geohash example</title><meta charset="utf-8">
<script type="module">
    import Geohash from 'https://cdn.jsdelivr.net/npm/[email protected]';

    const geohash = Geohash.encode(52.20, 0.12, 6);
    console.assert(geohash == 'u120fw');

    const latlon = Geohash.decode('u120fw');
    console.assert(JSON.stringify(latlon) == '{"lat":52.1988,"lon":0.115}');
</script>

Usage in Node.js

Geohash can be used in a Node.js app from npm (currently the esm package is required to load ES-modules):

$ npm install latlon-geohash esm
$ node -r esm
> import Geohash from 'latlon-geohash';
> const geohash = Geohash.encode(52.20, 0.12, 6);
> console.assert(geohash == 'u120fw');
> const latlon = Geohash.decode('u120fw');
> console.assert(JSON.stringify(latlon) == '{"lat":52.1988,"lon":0.115}');

Further details

More information (with interactive conversion) at www.movable-type.co.uk/scripts/geohash.html.

Full JsDoc at www.movable-type.co.uk/scripts/js/latlon-geohash/docs/Geohash.html.