kungfoo / geohash-java

Implementation of GeoHashes in java. We try to be/stay compliant to the spec, as far as possible.
Other
981 stars 310 forks source link

Request: sample code on how to use this library to get restaurants closest to user with a NoSQL database like DynamoDB #64

Closed waterdrake closed 1 year ago

waterdrake commented 1 year ago

Is there any sample code for how one might use this library with a NoSQL database such as DynamoDB?

I have a large number of NoSQL objects containing the geohash of the lat & long of restaurants that looks like this:

id | geohash     | restaurant_name
1  | 9q8ymjj56343  | pizza hut
2  | 9q8ymjj29365  | maxies
3  | 9q8ymjj29334  | ralphs
4  | 9q8ymjj98552  | pizza zen
5  | 9q8ymjj294m5  | 5 guys
6  | 9q8ymjj34543  | in-n-out

I want to be able to create a query where I can pass in the user's location as a geohash and return all restaurants in a 100 meter radius. I would like the results to be sorted so that the restaurants closest to the user appear at the top of the list.

So far, I understand that I can take the lat/long of a restaurant and turn it into a geohash like this:

GeoHash geoHash = GeoHash.withCharacterPrecision(lat, long, 12);

Then, I can use the same code to get the current position of the user:

GeoHash usersPosition = GeoHash.withCharacterPrecision(lat, long, 12); I know that after this I need to make a query on my restaurants where I filter results based on the user's geohash. I know how to make such queries with my database code, but I am not sure how to use this library to help me.

Could someone provide sample code on how to use this library to create my query? (pseudo code for the part where you query the NoSQL database is fine). I know there may be false-positives I need to account for as well, and throw them out. And that there may be multiple queries I need to make to my database.

This seems like a really good library, and I want to make sure I am using it right.

kungfoo commented 1 year ago

You should be able to do something along these lines:

WGS84Point location = new WGS84Point(/* users location */);
GeoHashCircleQuery query = new GeoHashCircleQuery(location, 100);

List<GeoHash> queryHashes = query.getSearchHashes();

/*
Now use the list of hashes generated (in string representation) to prefix query the rows in the database. 
*/
waterdrake commented 1 year ago

After getting queryHashes how do we get the actual hash out of it? is it geoHash.toString()?

        List<GeoHash> queryHashes = query.getSearchHashes();
        for (GeoHash geoHash: queryHashes) {
             geoHash.toString()
        }
kungfoo commented 1 year ago

.toBase32() is the method you might be looking for, if you store the base-32 encoded string as a field and want to use a prefix match. You can however only get the base-32 encoded string for multiples of 5 bits of precision (since each character encodes 5 bits).