Search code examples
node.jsalgorithmgeohashing

How to calculate the neighbouring grids on geohash. Algorithms required


Hi I am working with a database with an implementation of geohash

enter image description here

enter image description here

So as shown above, as the zoom level goes down (6 zoom levels), more of abcd gets inserted into each grid. I have represented them as a rigid grid; however, the central point is different for all the grids. So for example, distance from a to b will not be the same as distance from a to c.

If it was a rigid grid, I can just get the closest four neighbouring grids; however, I cannot do that as the distances vary and the closest neighbours are not necessarily orthogonal. Only information that I have from the database is the centra point of each grid and the geohash key e.g. aa, ab, etc..

How will I find girds that are just north, west, east and south of each grid for every zoom level? (have 6 zoom levels)


Solution

  • As you can see a cell that ends in, for example, d, has a cell with the same prefix but ending in b north of it. This allows you to set up a mapping table.

    In the cases where, like cb going north, you end up at ad, the trick is to recognize that going north from *b you need to look at the previous character (in this case, the c), go north from that character (a) and add the south-most edge of that column (d).

    In short, it's a matter of implementing some mapping tables and some logic to deal with the edges.