Geo Library for Amazon DynamoDB
- Box Queries: Return all of the items that fall within a pair of geo points that define a rectangle as projected onto a sphere.
- Radius Queries: Return all of the items that are within a given radius of a geo point.
- Basic CRUD Operations: Create, retrieve, update, and delete geospatial data items.
- Fully Typed: This port is written in typescript and declaration files are bundled into releases.
First you'll need to import the AWS sdk and set up your DynamoDB connection:
const AWS = ;const ddb = endpoint: '' ; // Local development
Next you must create an instance of
GeoDataManagerConfiguration for each geospatial table you wish to interact with. This is a container for various options (see API below), but you must always provide a
DynamoDB instance and a table name.
const ddbGeo = ;const config = ddb 'MyGeoTable';
You may modify the config to change defaults.
configlongitudeFirst = true; // Use spec-compliant GeoJSON, incompatible with awslabs/dynamodb-geo
Finally, you should instantiate a manager to query and write to the table using this config object.
const myGeoTableManager = config;
hashKeyLength (optimising for performance and cost)
hashKeyLength is the number of most significant digits (in base 10) of the 64-bit geo hash to use as the hash key. Larger numbers will allow small geographical areas to be spread across DynamoDB partitions, but at the cost of performance as more queries need to be executed for box/radius searches that span hash keys. See these tests for an idea of how query performance scales with
hashKeyLength for different search radii.
If your data is sparse, a large number will mean more RCUs since more empty queries will be executed and each has a minimum cost. However if your data is dense and
hashKeyLength too short, more RCUs will be needed to read a hash key and a higher proportion will be discarded by server-side filtering.
From the AWS
DynamoDB calculates the number of read capacity units consumed based on item size, not on the amount of data that is returned to an application. ... The number will also be the same whether or not you use a
Optimally, you should pick the largest
hashKeyLength your usage scenario allows. The wider your typical radius/box queries, the smaller it will need to be.
Note that the Java version uses a
6 by default. The same value will need to be used if you access the same data with both clients.
This is an important early choice, since changing your
hashKeyLength will mean recreating your data.
Creating a table
GeoTableUtil has a static method
getCreateTableRequest for helping you prepare a DynamoDB CreateTable request request, given a
You can modify this request as desired before executing it using AWS's DynamoDB SDK.
// Pick a hashKeyLength appropriate to your usageconfighashKeyLength = 3;// Use GeoTableUtil to help construct a CreateTableInput.const createTableInput = ddbGeoGeoTableUtil;// Tweak the schema as desiredcreateTableInputProvisionedThroughputReadCapacityUnits = 2;console;consoledircreateTableInput depth: null ;// Create the tableddb// Wait for it to become ready;
See also DynamoDB PutItem request
Updating a specific point
Note that you cannot update the hash key, range key, geohash or geoJson. If you want to change these, you'll need to recreate the record.
You must specify a
GeoPoint, and an
UpdateItemInput matching the DynamoDB UpdateItem request (
Key are filled in for you).
Deleting a specific point
You must specify a
RangeKeyValue and a
GeoPoint. Optionally, you can pass
DeleteItemInput matching DynamoDB DeleteItem request (
Key are filled in for you).
Query by rectangle by specifying a
// Querying a rectanglemyGeoTableManager// Print the results, an array of DynamoDB.AttributeMaps;
Query by radius by specifying a
// Querying 100km from Cambridge, UKmyGeoTableManager// Print the results, an array of DynamoDB.AttributeMaps;
TODO: Docs (see the example for an example of a batch write)
These are public properties of a
GeoDataManagerConfiguration instance. After creating the config object you may modify these properties.
consistentRead: boolean = false
Whether queries use the
ConsistentRead option (for strongly consistent reads) or not (for eventually consistent reads, at half the cost).
This can also be overridden for individual queries as a query config option.
longitudeFirst: boolean = true
This fork allows you to choose between awslabs/dynamodb-geo compatibility and GeoJSON standard compliance.
[lat, lon]) for compatibility with awslabs/dynamodb-geo
[lon, lat]) for GeoJSON standard compliance. (default)
Note that this value should match the state of your existing data - if you change it you must update your database manually, or you'll end up with ambiguously mixed data.
geohashAttributeName: string = "geohash"
The name of the attribute storing the full 64-bit geohash. Its value is auto-generated based on item coordinates.
hashKeyAttributeName: string = "hashKey"
The name of the attribute storing the first
hashKeyLength digits (default 2) of the geo hash, used as the hash (aka partition) part of a hash/range primary key pair. Its value is auto-generated based on item coordinates.
hashKeyLength: number = 2
rangeKeyAttributeName: string = "rangeKey"
The name of the attribute storing the range key, used as the range (aka sort) part of a hash/range key primary key pair. Its value must be specified by you (hash-range pairs must be unique).
geoJsonAttributeName: string = "geoJson"
The name of the attribute which will contain the longitude/latitude pair in a GeoJSON-style point (see also
geohashIndexName: string = "geohash-index"
The name of the index to be created against the geohash. Only used for creating new tables.
See the example on Github
No composite key support
Currently, the library does not support composite keys. You may want to add tags such as restaurant, bar, and coffee shop, and search locations of a specific category; however, it is currently not possible. You need to create a table for each tag and store the items separately.
Queries retrieve all paginated data
Although low level DynamoDB Query requests return paginated results, this library automatically pages through the entire result set. When querying a large area with many points, a lot of Read Capacity Units may be consumed.
More Read Capacity Units
The library retrieves candidate Geo points from the cells that intersect the requested bounds. The library then post-processes the candidate data, filtering out the specific points that are outside the requested bounds. Therefore, the consumed Read Capacity Units will be higher than the final results dataset. Typically 8 queries are exectued per radius or box search.
High memory consumption
Because all paginated
Query results are loaded into memory and processed, it may consume substantial amounts of memory for large datasets.
Dataset density limitation
The Geohash used in this library is roughly centimeter precision. Therefore, the library is not suitable if your dataset has much higher density.