最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Geohash: How to calculate the eight surrounding boxes - Stack Overflow

programmeradmin0浏览0评论

I use the code from 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?

I use the code from https://github./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?

Share Improve this question asked Feb 9, 2014 at 12:20 YelloveYellove 1651 gold badge2 silver badges8 bronze badges 1
  • I am looking for the same thing? Did you find a solution? – Roger Commented Jan 23, 2017 at 21:04
Add a ment  | 

3 Answers 3

Reset to default 2

https://github./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'));
}

Read the README at https://github./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.

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./davetroy/geohash-js/blob/master/geohash.js

发布评论

评论列表(0)

  1. 暂无评论