Fireburn
Fireburn

Reputation: 1021

Geospatial query using GeoHash

Suppose I have these GeoHash encoded geolocation coordinates

Location #1

1110001100111000010001010111101001010000100010000000111000010000 -> (14.580919034779072,120.97617074847221) -> (14.580918867141008,120.97617108374834) -> wdw4bykhj071

Location #2

1110001100111000010001001010110101101001111100010000010011100000 -> (14.5118791423738,121.01672273129225) -> (14.511878974735737,121.01672306656837) -> wdw49cc9y42f

Location #3

1110001100111000010001010111101001010111111110110100001100000000 -> (14.581578020006418,120.9766186773777) -> (14.581577852368355,120.97661901265383) -> wdw4bykrze1h

And both the Long value and String hash is stored in the database. And I understand that both Location #1 and Location #2 are within 1KM radius from this location below:

1110001100111000010001010111101001010111111110110100001100000000 -> (14.581578020006418,120.9766186773777) -> (14.581577852368355,120.97661901265383) -> wdw4bykrze1h

I'm trying to see if I can do something like a database query with startsWith:


txn.findStartsWith("Stores", "geoHash", queryHash);

But both Location #1 and #2 looks like this: "wdw4bykrze1h", "wdw4bykhj071", obviously, "wdw4bykrze1h" will be found as it is the exact same location, but, what should be the hash value in terms of GeoHash that would mean "within 1KM" radius? Is it queryHash = "wdw4byk"? Or something else?

String hash = "wdw4bykrze1h"; // this is the reference point query
String queryHash = geohash.toBase32().substring(0, 3); 

What should be the value of the substring here that would make it a "1 KM" radius query?

Upvotes: 1

Views: 196

Answers (1)

Ralf Kleberhoff
Ralf Kleberhoff

Reputation: 7290

It's not that easy.

The Wikipedia article on Geohash lists two issues: edge cases and non-linearity.

Edge cases mean that, if you happen to be close to some border of the tiling system behind Geohash, some locations within your 1km radius can have a very different prefix.

Non-linearity means that e.g. a 4-character prefix close to the equator names an area of 20km * 40km, whereas close to the poles it can be 20km * 1km.

The prefix length that, on average, comes closest to 1 square kilometer, is 6 characters. But you should consider computing neighbor prefixes to cover edge cases, and longitude span (non-linearity), especially when close to the poles.

And, as the queries cover some more or less rectangular shape, and you're asking for a circle, you should filter the results for really being within your desired circle.

Upvotes: 3

Related Questions