Git Product home page Git Product logo

node-geohash's Introduction

node-geohash

Geohash library for nodejs.

Build Status NPM version Dependencies Donate

Install

npm install ngeohash

Usage

var geohash = require('ngeohash');
console.log(geohash.encode(37.8324, 112.5584));
// prints ww8p1r4t8
var latlon = geohash.decode('ww8p1r4t8');
console.log(latlon.latitude);
console.log(latlon.longitude);

Basic Methods

geohash.encode (latitude, longitude, precision=9)

Encode a pair of latitude and longitude values into a geohash. The third argument is optional, you can specify a length of this hash string, which also affects the precision of the geohash.

geohash.decode (hashstring)

Decode a hash string into pair of latitude and longitude values. A javascript object is returned with latitude and longitude keys.

geohash.decode_bbox (hashstring)

Decode hashstring into a bounding box that matches it. Data is returned as a four-element array: [minlat, minlon, maxlat, maxlon].

geohash.bboxes (minlat, minlon, maxlat, maxlon, precision=9)

Get all hashstringes between [minlat, minlon] and [maxlat, maxlon]. These keys can be used to find a poi stored in the cache with hashstring keys. eg. show all points in the visible range of map. [minlat, minlon] correspond to the southwest starting hash, whereas [maxlat, maxlon] correspond to the northeast starting hash.

geohash.neighbor (hashstring, direction)

Find the neighbor of a geohash string in certain direction. Direction is a two-element array, i.e. [1,0] means north, [-1,-1] means southwest.

geohash.neighbors (hashstring)

Find all 8 geohash neighbors [n, ne, e, se, s, sw, w, nw] of a geohash string. This method is more efficient than running the geohash.neighbor method multiple times for all directions. array: [hashstr1, hashstr2, ... ]

Integer Methods

Note that integer precision is capped at 52bits in Javscript. These are just regular javascript floating point numbers, but the functionality should mimic other uint64 geohash functions in geohash libraries for other languages and are compatible with the geohash integers produced by them, however any geohash integer encoded in anything more than 52bits will be resolved to 52bits resolution.

geohash.encode_int (latitude, longitude, bitDepth=52)

Encode a pair of latitude and longitude values into a geohash integer. The third argument is optional, you can specify the bitDepth of this number, which affects the precision of the geohash but also must be used consistently when decoding. Bit depth must be even.

geohash.decode_int (hashinteger, bitDepth=52)

Decode a integer geohashed number into pair of latitude and longitude value approximations. A javascript object is returned with latitude and longitude keys. You should also provide the bitDepth at which to decode the number (ie. what bitDepth the number was originally produced with), but will default to 52.

geohash.decode_bbox_int (hashinteger, bitDepth=52)

Decode an integer geohash into the bounding box that matches it. Data is returned as a four-element array: [minlat, minlon, maxlat, maxlon].

geohash.bboxes_int (minlat, minlon, maxlat, maxlon, bitDepth=52)

Get all geohash integers between [minlat, minlon] and [maxlat, maxlon]. These keys can be used to find a poi stored in the cache with geohash integer keys. eg. show all points in the visible range of map

geohash.neighbor_int (hashinteger, direction, bitDepth=52)

Find the neighbor of a geohash integer in certain direction. Direction is a two-element array, i.e. [1,0] means north, [-1,-1] means southwest. The bitDepth should be specified, but defaults to 52 bits.

geohash.neighbors_int (hashinteger, bitDepth=52)

Find all 8 neighbors [n, ne, e, se, s, sw, w, nw] of a geohash integer. This method is more efficient than running the geohash.neighbor method multiple times for all directions. The bitDepth should be specified, but defaults to 52 bits.

Node-Geohash for browser

We recently added a browserify script with npm run browserify. ngeohash.js will be generated and you can use this file in your javascript application for browser.

<script src="./path/to/ngeohash.js"></script>
<script>
alert(ngeohash.encode(114.23, 38.23));
</script>

About Geohash

Check Wikipedia for more information.

See Also

Geohash.rs: node-geohash ported to Rust: A rust library for geohash.

License

node-geohash is open sourced under MIT License, of course.

Donation

I'm now accepting donation on liberapay, if you find my work helpful and want to keep it going.

node-geohash's People

Contributors

alsotang avatar arjunmehta avatar four43 avatar ilancoulon avatar lofimichael avatar ovider avatar sunng87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-geohash's Issues

[email protected] installation failed on react-native

I am trying to install [email protected] on react-native app, and it fails with this error:

npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] install: browserify . -o ngeohash.js -s ngeohash
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/haialaluf/.npm/_logs/2018-12-16T15_36_52_892Z-debug.log

What I had to do is installing the module using --ignore-scripts.
[email protected] works just fine.

Add option to base4

The structure of the Geohash is a base4 hierarchy: powers of 4 in the spatial representation (grid cell divided into 4 cels each iteration).

In applications that need to access "the full hierarchy", is important to use base4 (2 bits). In the Javascript code is only a inclusion of one parameter in the Encode method:

  • @param {Number|String} latitude
  • @param {Number|String} longitude
  • @param {Number} numberOfChars
  • @param {boolean} useBase4 (default is false)
var encode = function (latitude, longitude, numberOfChars, useBase4) {
...
var maxBits = (useBase4===true)? 2: 5;
...
    if (bits === maxBits) ... 

Auto Precision for the encode() method

We have a need to encode a large amount of data with varying precisions. The goal is to evaluate how many significant figures are in the passed latitude and longitude and encode a varying length hash based on that. The website where geohashing was developed does a pretty good job of it: http://geohash.org/

How much precise is precision?

In mathematical terms, how precise is the option precision? Like how many meters is precision 9 or 8? Does the option stand for the number of digits after the decimal point?

Convert to string notation required for auto precision

It is possible to change

    if (typeof(latitude) === 'number' || typeof(longitude) === 'number') {
      throw new Error('string notation required for auto precision.');
    }

To something like

    if (typeof(latitude) === 'number') {
      latitude = parseFloat(latitude).toString();
    }
    if (typeof(longitude) === 'number') {
      longitude = parseFloat(longitude).toString();
    }

or direct enforce input.toString(), without checks or parseFloats.

encode() seems broken

Using the latest tag of Node.js it seems the encode() function is broke and returning nothing. I'll have more time to look when I get home, just an FYI.

method to get bboxes based on distance from a point...

Would be nice if there were methods to get bboxes based on the distance from a point...

// distance_bboxes(lat, lon, distance, unit='radians')
// distance_bboxes_int(point, distance, unit='radians')
//   unit is one of ( 'radians', 'miles', 'mi', 'kilometers', 'km', 'meters', 'm' )

this can then, if necessary convert the units to radians (based on a sphere roughly the size of earth), then do the calculation for a square container...

may also want a distance_bboxes_circle, distance_bboxes_circle_int set as well, but this will probably be more complicated to slightly less use.

ngeohash.js is missing in action

First of all, I'm not using browserify for my project, I did however install it and ran npm install afterwards, as mentioned in the docs. However, I cannot seem to find ngeohash.js after running that. Am I doing something wrong?

elasticsearch geohashes produce duplicate

Hello, I am almost certain this isnt an ngeohash issue, but I am hoping there is might be a way to leverage ngeohash to mitigate the problem.

I am using a 3rd party mapping js api to display elasticsearch aggregations. When a user selects an area of interest to display (by panning or zooming) the actual request that is sent thru the api to elasticsearch is broken into 8 separate bounding boxes, each with a distinct set of non overlapping coordinates. However, when I bring each each request back I am getting overlapping geohashes at the edges, see attached. The darker squares are actually two, overlapping records from separate requests. I am trying to show heatmaps and this really makes elasticsearch unusable because its not actually conveying the whole truth.

Is there anyway to make sure that the geohashes dont overlap with seperate requests?

image

Can I change MIN/MAX_LAT/LON to suit my custom "map"

I have a custom map which is just a rectangular CSS button and I want to map button clicks on the coordinates. Can I safely change the MIN_LAT, etc. constants in the library and will it just work correctly?

bboxes sometimes returns nothing.

I am drawing the bounding boxes on google map and there are certain cases where the bboxes returns nothing while it should.
For example

//  precision; 1
// google maps zoom; 4
// returns nothing
geo.bboxes(-50.077637397640146, 100.810015625, 1.0336827697921598, -179.385296875, 1)

//  precision; 1
// google maps zoom; 4
// returns 6
geo.bboxes(-49.51027862137093, 99.6674375, 1.912289970207342, 179.472125, 1);

With higher precision this becomes even more clear on certain coordinates.

not able to install ngeohash

I have npm installed in my machine. But not able to install module ngeohash via npm. Please find the error message below. Any help will be appreciated.
npm -v
6.5.0

npm install ngeohash

[email protected] install /Users/babu.yadav/repo/devices_api_node/node_modules/ngeohash
browserify . -o ngeohash.js -s ngeohash

sh: browserify: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] install: browserify . -o ngeohash.js -s ngeohash
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/babu.yadav/.npm/_logs/2018-12-16T09_35_23_786Z-debug.log

geohash int bitdepth broken

location = "u281yyxg1"
location1 = 3669078753079491  // Bitdepth 52
location2 = 917269688269872  // Bitdepth 50
location3 = 895771179951  // Bitdepth 40

The integer locations are changing all digits when changing bitdepth on the same location. From my understanding the higher_order_digits need to be similar no matter what bitdepth is selected. There must be something wrong in the implementation.

Calculating neighbors is incorrect around +/- 180 meridian and poles

Have spotted several errors in the neighbor/neighbors function. Not all neighbors are return correctly. For instance take:

geohash.neighbor("r")

will output

x, x, r, p, p, n, q, w

Instead it should be

x, 8, 2, 0, p, n, q, w

North East, East and South East are incorrect.

Need it in browser

I think the lib can be used in browser, but it pollute global namespace with lots of stuff. Is it possible to make a client-side version? I think all is needed is adding intro and outro like this:

intro.js:
(function(module) {

outro.js:
})(window);

And add client lib build in npm scripts.

Bit depth does not have to be even

In the readme under geohash.encode_int it is stated that bit depth must be even, this is not the case as this would simply mean that the longitude and latitude would have differing precision/error. The code handles this perfectly fine, so I assume it is just a type-o or something you forgot to omit.

Changelog

Heyo

Maybe there should be a changelog for this project?

I'm not sure if i can upgrade my ngeohash dependency from 0.6.0 because I don't know if there is any breaking changes.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.