Geohash
At Firstclasspostcodes, we use geohashes inside DynamoDB to quickly retrieve matching postcodes for a particular coordinate. Our solution has been adapted from this original blog post and we've extracted some of the code from an existing NPM library to suit our own purposes.
Install
npm i @firstclasspostcodes/geohash --save
Key
We can construct a hash key with the following:
This products an object consisting of a "hash key" (geokey
) and "hash range" (georange
). This is enough to start storing points, ready for querying within DynamoDB!
BoundingBox
Let's start testing bounding boxes by determining if a specific point is contained within the matching area of another point. A bounding box is used to produce all the matching geohash ranges for querying points within DynamoDB (or another client).
Once a bounding box for a particular center has been constructed, its possible to produce the hash ranges (at a configurable key length) that can be marshalled into requests.
The length of the "hash key" to adopt depends on the data and types of queries you need to work with. Refer to this blog post for more information.
Query
Querying for matching points is relatively trivial, but only supports DynamoDB currently. It uses an instance of AWS.DynamoDB.DocumentClient
to execute all the necessary queries (a query for each tuple returned from BoundingBox#hashRanges
).
Lets take a look at an example below, which provides a pre-configured instance of DocumentClient
.
The docClient
is configured to send the TableName
and IndexName
along with every #query
request.