I have an application where I need to query geohashes by specifying the bounding rectangle. Since geohashes map 2D to 1D is there a method by which I may find the prefixes of all geohashes which satisfy the query condition (bounding rectangle). I tried to google but I did not find any algorithm.

It will be great if there is some library which does that for me efficiently in C/c++. Even if there is no library then too I can can work with an algorithmic solution.

  • Are you looking for a PostGIS answer or just a general one. You have this marked as PostGIS but looks like you are looking for a C/C++ library you can plug directly into your app.
    – LR1234567
    Jul 27 '14 at 20:16
  • @LR1234567 Sorry for the blunder. I have untagged postgis. but do u know of any such c/c++ library Jul 27 '14 at 20:19

Even though this isn't a PostGIS question hopefully this will help.

It's my understanding that all solutions to a bounding box geohash must start with the bounding box hash. So here is a demonstration from PostGIS world.

SELECT ST_GeoHash(ST_MakeEnvelope(12.45, 41.8, 12.49, 41.99,4326 ));

Gives a geohash of: sr2

Now normally in PostGIS, I'd just use an overlap query like so:

SELECt ST_GeoHash(geog)
  from ch16.places 
   WHERE ST_MakeEnvelope(12.45, 41.8, 12.49, 41.99,4326 )::geography && geog;

Which gives me an answer of two records with geohashes


Note how they all start with sr2.

Note that an equivalent albeit slower approach(unless I have a functional index on ST_GeoHash) would be:

SELECT ST_GeoHash(geog)
 FROM ch16.places 
  WHERE ST_GeoHash(geog) LIKE ST_GeoHash(ST_MakeEnvelope(12.45, 41.8, 12.49, 41.99,4326 )) || '%';

Would give me the same answer. So in short I could be wrong, but I think the solution to any GeoHash bounding box filter is any geohash that starts with the geohash of the bounding box.

At a certain point if you make your bounding box big enough, it has no geohash which in short means your bounding box is too big to be satisfied by a geohash query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.