3

I use the code from https://github.com/davetroy/geohash-js But I don't know how to calculate the surrounding geohashcode of a given geohashcode. I need to use the function witch calculate them both in php and javascript. Does anyone have such code or some method to solve this problem?

Yellove
  • 165
  • 1
  • 2
  • 8

3 Answers3

1

Read the README at https://github.com/davetroy/geohash-js ... in javascript you create a GeoHash by calling e.g.

mygh= encodeGeoHash( 53.1, -0.24 );

which will give you a value like: 'gcry4d91zt7n', if you then decode this:

mydecoded= decodeGeoHash(mygh);

you get an object containing:

Object {latitude: Array[3], longitude: Array[3]}
latitude: Array[3]
0: 53.099999986588955
1: 53.10000015422702
2: 53.10000007040799

longitude: Array[3]
0: -0.24000003933906555
1: -0.2399997040629387
2: -0.23999987170100212 

If you simply truncate the geohash to, say 'gcry', then you have reduced the resolution and a call to decodeGeoHash will return:

Object {latitude: Array[3], longitude: Array[3]}
latitude: Array[3]
0: 53.0859375
1: 53.26171875
2: 53.173828125

longitude: Array[3]
0: -0.3515625
1: 0
2: -0.17578125

i.e. the three points now describe a much larger area. As is mentioned in the README, this larger box is not centred on the original (higher resolution) box - for that you need to calculate adjacent (top,bottom, left,right) geohashes by using e.g.

mygh_top= calculateAdjacent( mygh, 'top')

look at the source of geohash-demo.js which shows an example of this.

TonyWilk
  • 1,447
  • 10
  • 13
1

https://github.com/davetroy/geohash-js/blob/master/geohash-demo.js#L63 has the exmaple:

GeoHashBox.prototype.showNeighbors = function () {
var geohashPrefix = this.geohash.substr(0,this.geohash.length-1);

this.neighbors.top = new GeoHashBox(calculateAdjacent(this.geohash, 'top'));
this.neighbors.bottom = new GeoHashBox(calculateAdjacent(this.geohash, 'bottom'));
this.neighbors.right = new GeoHashBox(calculateAdjacent(this.geohash, 'right'));
this.neighbors.left = new GeoHashBox(calculateAdjacent(this.geohash, 'left'));
this.neighbors.topleft = new GeoHashBox(calculateAdjacent(this.neighbors.left.geohash, 'top'));
this.neighbors.topright = new GeoHashBox(calculateAdjacent(this.neighbors.right.geohash, 'top'));
this.neighbors.bottomright = new GeoHashBox(calculateAdjacent(this.neighbors.right.geohash, 'bottom'));
this.neighbors.bottomleft = new GeoHashBox(calculateAdjacent(this.neighbors.left.geohash, 'bottom'));
}
Roger
  • 2,063
  • 4
  • 32
  • 65
0

You can use the following code to get the eight surrounding boxes :

var BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz";
var NEIGHBORS = {
  right: { even: "bc01fg45238967deuvhjyznpkmstqrwx" },
  left: { even: "238967debc01fg45kmstqrwxuvhjyznp" },
  top: { even: "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },
  bottom: { even: "14365h7k9dcfesgujnmqp0r2twvyx8zb" },
};
var BORDERS = {
  right: { even: "bcfguvyz" },
  left: { even: "0145hjnp" },
  top: { even: "prxz" },
  bottom: { even: "028b" },
};

NEIGHBORS.bottom.odd = NEIGHBORS.left.even;
NEIGHBORS.top.odd = NEIGHBORS.right.even;
NEIGHBORS.left.odd = NEIGHBORS.bottom.even;
NEIGHBORS.right.odd = NEIGHBORS.top.even;

BORDERS.bottom.odd = BORDERS.left.even;
BORDERS.top.odd = BORDERS.right.even;
BORDERS.left.odd = BORDERS.bottom.even;
BORDERS.right.odd = BORDERS.top.even;

function calculateAdjacent(srcHash, dir) {
    srcHash = srcHash.toLowerCase();
    var lastChr = srcHash.charAt(srcHash.length-1);
    var type = (srcHash.length % 2) ? 'odd' : 'even';
    var base = srcHash.substring(0,srcHash.length-1);
    if (BORDERS[dir][type].indexOf(lastChr)!=-1)
        base = calculateAdjacent(base, dir);
    return base + BASE32[NEIGHBORS[dir][type].indexOf(lastChr)];
}

const areaGeoHash = 'tsjcn';

this.neighbors.left = calculateAdjacent(areaGeoHash, 'left');
this.neighbors.right = calculateAdjacent(areaGeoHash, 'right');
this.neighbors.top = calculateAdjacent(areaGeoHash, 'top');
this.neighbors.bottom = calculateAdjacent(areaGeoHash, 'bottom');
this.neighbors.topLeft = calculateAdjacent(this.neighbors.left, 'top');
this.neighbors.topRight = calculateAdjacent(this.neighbors.right, 'top');
this.neighbors.bottomLeft = calculateAdjacent(this.neighbors.left, 'bottom');
this.neighbors.bottomRight = calculateAdjacent(this.neighbors.right, 'bottom');

console.log(' \nNeighboring Hash Values - ', this.neighbors);

NOTE: This code is referenced from https://github.com/davetroy/geohash-js/blob/master/geohash.js

starball
  • 20,030
  • 7
  • 43
  • 238
Aman Kumar Gupta
  • 2,640
  • 20
  • 18