[GIS] How to get the geohash of a bounding box

geohash

I am storing a large collection of 5m x 5m geohash values. I want to be able to query this collection and find all the geohash values which fall within a particular georect – which is defined by a 2 latitude points – a NW point and a SW point.

What I don't understand is this: lets say I have all my locations in the data store (along with a latitude, longitude and a geohash (with high precision) of each location.)

I know that I should use the prefix of the geohash (to match locations within), but how do I calculate a geohash of a bounding box? Considering the bounding box is made up of two points, North-East and South-West, I do not understand how to go about doing this..

In order for me to query which locations should be returned for the currently visible bounding box, I need the geohash of the visible/viewable bounding box – Now I know I can geohash the center location on the viewable map, but I do not know how many letters to cut off (to reduce precision) to achieve 'a fit' to the actual bounding box. (Or maybe that isn't the way…?)

What do you do when the bounding box container to geohashes? (like in the middle of the viewable area it splits between 'dqcjr0' and 'dqcjqb')

Also, lets assume I have a 5 letter geohash, how can I convert that back into a viewable bounding box? or in other words, how do I know what is 'included' in the hash, and what is in adjacent hashes?

Best Answer

Following is one of the solutions I could find.

There are 12 levels of a geohash. The size varies from a
5,009.4km x 4,992.6km at level 1 to
3.7cm x 1.9cm at level 12.

First thing, you need to fix on a level.

Get the center point of the bounding box. Once you have the max distance from the center and the geohash size, find grid points inside the box at distance (the geohash size).

Convert grid points to geohash.

Will share the link of of my open source project which does something similar. (Needs some improvement before I push the code :-))

On your second question, converting a geohash back into a pair gives us the center of geohash area*(a rectangular area)*.

Given the size of the geohash at that level you can get yourself a bounding box.

Hope this helps.