dynamodb-geo
Advanced tools
Comparing version 0.2.4 to 0.3.0
@@ -141,3 +141,3 @@ import { AWSError, DynamoDB, Request } from "aws-sdk"; | ||
* | ||
* @param queryRectangleRequest | ||
* @param queryRectangleInput | ||
* Container for the necessary parameters to execute rectangle query request. | ||
@@ -165,3 +165,3 @@ * | ||
* | ||
* @param queryRadiusRequest | ||
* @param queryRadiusInput | ||
* Container for the necessary parameters to execute radius query request. | ||
@@ -227,6 +227,6 @@ * | ||
* | ||
* @param coveringCellIds | ||
* @param covering | ||
* A list of geohash ranges that will be used to query Amazon DynamoDB. | ||
* | ||
* @param latLngRect | ||
* @param geoQueryInput | ||
* The rectangle area that will be used as a reference point for precise filtering. | ||
@@ -236,3 +236,3 @@ * | ||
*/ | ||
private dispatchQueries(coveringCellIds, geoQueryInput); | ||
private dispatchQueries(covering, geoQueryInput); | ||
/** | ||
@@ -242,10 +242,14 @@ * Filter out any points outside of the queried area from the input list. | ||
* @param list | ||
* List of items return by Amazon DynamoDB. It may contains points outside of the actual area queried. | ||
* @param geoQueryInput | ||
* @returns DynamoDB.ItemList | ||
*/ | ||
private filterByRadius(list, geoQueryInput); | ||
/** | ||
* Filter out any points outside of the queried area from the input list. | ||
* | ||
* @param list | ||
* @param geoQueryInput | ||
* Queried area. Any points outside of this area need to be discarded. | ||
* | ||
* @return List of items within the queried area. | ||
* @returns DynamoDB.ItemList | ||
*/ | ||
private filter(list, geoQueryInput); | ||
private filterByRectangle(list, geoQueryInput); | ||
} |
@@ -20,4 +20,4 @@ "use strict"; | ||
var S2Util_1 = require("./s2/S2Util"); | ||
var GeohashRange_1 = require("./model/GeohashRange"); | ||
var nodes2ts_1 = require("nodes2ts"); | ||
var Covering_1 = require("./model/Covering"); | ||
/** | ||
@@ -169,3 +169,3 @@ * <p> | ||
* | ||
* @param queryRectangleRequest | ||
* @param queryRectangleInput | ||
* Container for the necessary parameters to execute rectangle query request. | ||
@@ -176,5 +176,7 @@ * | ||
GeoDataManager.prototype.queryRectangle = function (queryRectangleInput) { | ||
var latLngRect = S2Util_1.S2Util.getBoundingLatLngRect(queryRectangleInput); | ||
var ranges = this.config.s2RegionCoverer.getCoveringCells(latLngRect); | ||
return this.dispatchQueries(ranges, queryRectangleInput); | ||
var _this = this; | ||
var latLngRect = S2Util_1.S2Util.latLngRectFromQueryRectangleInput(queryRectangleInput); | ||
var covering = new Covering_1.Covering(new this.config.S2RegionCoverer().getCoveringCells(latLngRect)); | ||
return this.dispatchQueries(covering, queryRectangleInput) | ||
.then(function (results) { return _this.filterByRectangle(results, queryRectangleInput); }); | ||
}; | ||
@@ -198,3 +200,3 @@ /** | ||
* | ||
* @param queryRadiusRequest | ||
* @param queryRadiusInput | ||
* Container for the necessary parameters to execute radius query request. | ||
@@ -205,5 +207,7 @@ * | ||
GeoDataManager.prototype.queryRadius = function (queryRadiusInput) { | ||
var latLngRect = S2Util_1.S2Util.getBoundingLatLngRect(queryRadiusInput); | ||
var coveringCellIds = this.config.s2RegionCoverer.getCoveringCells(latLngRect); | ||
return this.dispatchQueries(coveringCellIds, queryRadiusInput); | ||
var _this = this; | ||
var latLngRect = S2Util_1.S2Util.getBoundingLatLngRectFromQueryRadiusInput(queryRadiusInput); | ||
var covering = new Covering_1.Covering(new this.config.S2RegionCoverer().getCoveringCells(latLngRect)); | ||
return this.dispatchQueries(covering, queryRadiusInput) | ||
.then(function (results) { return _this.filterByRadius(results, queryRadiusInput); }); | ||
}; | ||
@@ -269,6 +273,6 @@ /** | ||
* | ||
* @param coveringCellIds | ||
* @param covering | ||
* A list of geohash ranges that will be used to query Amazon DynamoDB. | ||
* | ||
* @param latLngRect | ||
* @param geoQueryInput | ||
* The rectangle area that will be used as a reference point for precise filtering. | ||
@@ -278,17 +282,11 @@ * | ||
*/ | ||
GeoDataManager.prototype.dispatchQueries = function (coveringCellIds, geoQueryInput) { | ||
GeoDataManager.prototype.dispatchQueries = function (covering, geoQueryInput) { | ||
var _this = this; | ||
var promises = []; | ||
coveringCellIds.forEach(function (outerRange) { | ||
var hashRange = new GeohashRange_1.GeohashRange(outerRange.rangeMin().id, outerRange.rangeMax().id); | ||
hashRange.trySplit(_this.config.hashKeyLength).forEach(function (range) { | ||
var hashKey = S2Manager_1.S2Manager.generateHashKey(range.rangeMin, _this.config.hashKeyLength); | ||
promises.push(_this.dynamoDBManager.queryGeohash(geoQueryInput.QueryInput, hashKey, range).then(function (queryResults) { | ||
return queryResults.map(function (queryResult) { return _this.filter(queryResult.Items, geoQueryInput); }); | ||
})); | ||
}); | ||
var promises = covering.getGeoHashRanges(this.config.hashKeyLength).map(function (range) { | ||
var hashKey = S2Manager_1.S2Manager.generateHashKey(range.rangeMin, _this.config.hashKeyLength); | ||
return _this.dynamoDBManager.queryGeohash(geoQueryInput.QueryInput, hashKey, range); | ||
}); | ||
return Promise.all(promises).then(function (results) { | ||
var mergedResults = []; | ||
results.forEach(function (listList) { return listList.forEach(function (list) { return mergedResults.push.apply(mergedResults, list); }); }); | ||
results.forEach(function (queryOutputs) { return queryOutputs.forEach(function (queryOutput) { return mergedResults.push.apply(mergedResults, queryOutput.Items); }); }); | ||
return mergedResults; | ||
@@ -301,24 +299,13 @@ }); | ||
* @param list | ||
* List of items return by Amazon DynamoDB. It may contains points outside of the actual area queried. | ||
* | ||
* @param geoQueryInput | ||
* Queried area. Any points outside of this area need to be discarded. | ||
* | ||
* @return List of items within the queried area. | ||
* @returns DynamoDB.ItemList | ||
*/ | ||
GeoDataManager.prototype.filter = function (list, geoQueryInput) { | ||
GeoDataManager.prototype.filterByRadius = function (list, geoQueryInput) { | ||
var _this = this; | ||
var result = []; | ||
var latLngRect = null; | ||
var centerLatLng = null; | ||
var radiusInMeter = 0; | ||
if (geoQueryInput.hasOwnProperty('MinPoint')) { | ||
latLngRect = S2Util_1.S2Util.getBoundingLatLngRect(geoQueryInput); | ||
} | ||
else if (geoQueryInput.hasOwnProperty('RadiusInMeter')) { | ||
var centerPoint = geoQueryInput.CenterPoint; | ||
centerLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude, centerPoint.longitude); | ||
radiusInMeter = geoQueryInput.RadiusInMeter; | ||
} | ||
list.forEach(function (item) { | ||
var centerPoint = geoQueryInput.CenterPoint; | ||
centerLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude, centerPoint.longitude); | ||
radiusInMeter = geoQueryInput.RadiusInMeter; | ||
return list.filter(function (item) { | ||
var geoJson = item[_this.config.geoJsonAttributeName].S; | ||
@@ -329,14 +316,26 @@ var coordinates = JSON.parse(geoJson).coordinates; | ||
var latLng = nodes2ts_1.S2LatLng.fromDegrees(latitude, longitude); | ||
if (latLngRect != null && latLngRect.containsLL(latLng)) { | ||
result.push(item); | ||
} | ||
else if (centerLatLng != null && radiusInMeter > 0 | ||
&& centerLatLng.getEarthDistance(latLng).toNumber() <= radiusInMeter) { | ||
result.push(item); | ||
} | ||
return centerLatLng.getEarthDistance(latLng).toNumber() <= radiusInMeter; | ||
}); | ||
return result; | ||
}; | ||
/** | ||
* Filter out any points outside of the queried area from the input list. | ||
* | ||
* @param list | ||
* @param geoQueryInput | ||
* @returns DynamoDB.ItemList | ||
*/ | ||
GeoDataManager.prototype.filterByRectangle = function (list, geoQueryInput) { | ||
var _this = this; | ||
var latLngRect = S2Util_1.S2Util.latLngRectFromQueryRectangleInput(geoQueryInput); | ||
return list.filter(function (item) { | ||
var geoJson = item[_this.config.geoJsonAttributeName].S; | ||
var coordinates = JSON.parse(geoJson).coordinates; | ||
var longitude = coordinates[_this.config.longitudeFirst ? 0 : 1]; | ||
var latitude = coordinates[_this.config.longitudeFirst ? 1 : 0]; | ||
var latLng = nodes2ts_1.S2LatLng.fromDegrees(latitude, longitude); | ||
return latLngRect.containsLL(latLng); | ||
}); | ||
}; | ||
return GeoDataManager; | ||
}()); | ||
exports.GeoDataManager = GeoDataManager; |
@@ -15,4 +15,4 @@ import { DynamoDB } from "aws-sdk"; | ||
* The order of the GeoJSON coordinate pair in data. | ||
* Use false [lat, lon] for compatibility with the Java library https://github.com/awslabs/dynamodb-geo (default) | ||
* Use true [lon, lat] for GeoJSON standard compliance. | ||
* Use false [lat, lon] for compatibility with the Java library https://github.com/awslabs/dynamodb-geo | ||
* Use true [lon, lat] for GeoJSON standard compliance. (default) | ||
* | ||
@@ -25,4 +25,4 @@ * Note that this value should match the state of your existing data - if you change it you must update your database manually | ||
dynamoDBClient: DynamoDB; | ||
s2RegionCoverer: S2RegionCoverer; | ||
S2RegionCoverer: typeof S2RegionCoverer; | ||
constructor(dynamoDBClient: any, tableName: string); | ||
} |
@@ -12,7 +12,7 @@ "use strict"; | ||
this.geohashIndexName = "geohash-index"; | ||
this.hashKeyLength = 6; | ||
this.hashKeyLength = 2; | ||
/** | ||
* The order of the GeoJSON coordinate pair in data. | ||
* Use false [lat, lon] for compatibility with the Java library https://github.com/awslabs/dynamodb-geo (default) | ||
* Use true [lon, lat] for GeoJSON standard compliance. | ||
* Use false [lat, lon] for compatibility with the Java library https://github.com/awslabs/dynamodb-geo | ||
* Use true [lon, lat] for GeoJSON standard compliance. (default) | ||
* | ||
@@ -23,6 +23,6 @@ * Note that this value should match the state of your existing data - if you change it you must update your database manually | ||
*/ | ||
this.longitudeFirst = false; | ||
this.longitudeFirst = true; | ||
this.dynamoDBClient = dynamoDBClient; | ||
this.tableName = tableName; | ||
this.s2RegionCoverer = new nodes2ts_1.S2RegionCoverer(); | ||
this.S2RegionCoverer = nodes2ts_1.S2RegionCoverer; | ||
} | ||
@@ -29,0 +29,0 @@ return GeoDataManagerConfiguration; |
import { QueryRadiusInput, QueryRectangleInput } from "../types"; | ||
import { S2LatLngRect } from "nodes2ts"; | ||
export declare class S2Util { | ||
/** | ||
* An utility method to get a bounding box of latitude and longitude from a given GeoQueryRequest. | ||
* | ||
* @param geoQueryRequest | ||
* It contains all of the necessary information to form a latitude and longitude box. | ||
* | ||
* */ | ||
static getBoundingLatLngRect(geoQueryRequest: QueryRadiusInput | QueryRectangleInput): S2LatLngRect; | ||
static latLngRectFromQueryRectangleInput(geoQueryRequest: QueryRectangleInput): S2LatLngRect; | ||
static getBoundingLatLngRectFromQueryRadiusInput(geoQueryRequest: QueryRadiusInput): S2LatLngRect; | ||
} |
"use strict"; | ||
/* | ||
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -21,42 +7,31 @@ var nodes2ts_1 = require("nodes2ts"); | ||
} | ||
/** | ||
* An utility method to get a bounding box of latitude and longitude from a given GeoQueryRequest. | ||
* | ||
* @param geoQueryRequest | ||
* It contains all of the necessary information to form a latitude and longitude box. | ||
* | ||
* */ | ||
S2Util.getBoundingLatLngRect = function (geoQueryRequest) { | ||
if (geoQueryRequest.hasOwnProperty('MinPoint')) { | ||
var queryRectangleRequest = geoQueryRequest; | ||
var minPoint = queryRectangleRequest.MinPoint; | ||
var maxPoint = queryRectangleRequest.MaxPoint; | ||
var latLngRect = null; | ||
if (minPoint != null && maxPoint != null) { | ||
var minLatLng = nodes2ts_1.S2LatLng.fromDegrees(minPoint.latitude, minPoint.longitude); | ||
var maxLatLng = nodes2ts_1.S2LatLng.fromDegrees(maxPoint.latitude, maxPoint.longitude); | ||
latLngRect = nodes2ts_1.S2LatLngRect.fromLatLng(minLatLng, maxLatLng); | ||
} | ||
return latLngRect; | ||
S2Util.latLngRectFromQueryRectangleInput = function (geoQueryRequest) { | ||
var queryRectangleRequest = geoQueryRequest; | ||
var minPoint = queryRectangleRequest.MinPoint; | ||
var maxPoint = queryRectangleRequest.MaxPoint; | ||
var latLngRect = null; | ||
if (minPoint != null && maxPoint != null) { | ||
var minLatLng = nodes2ts_1.S2LatLng.fromDegrees(minPoint.latitude, minPoint.longitude); | ||
var maxLatLng = nodes2ts_1.S2LatLng.fromDegrees(maxPoint.latitude, maxPoint.longitude); | ||
latLngRect = nodes2ts_1.S2LatLngRect.fromLatLng(minLatLng, maxLatLng); | ||
} | ||
else if (geoQueryRequest.hasOwnProperty('RadiusInMeter')) { | ||
var queryRadiusRequest = geoQueryRequest; | ||
var centerPoint = queryRadiusRequest.CenterPoint; | ||
var radiusInMeter = queryRadiusRequest.RadiusInMeter; | ||
var centerLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude, centerPoint.longitude); | ||
var latReferenceUnit = centerPoint.latitude > 0.0 ? -1.0 : 1.0; | ||
var latReferenceLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude + latReferenceUnit, centerPoint.longitude); | ||
var lngReferenceUnit = centerPoint.longitude > 0.0 ? -1.0 : 1.0; | ||
var lngReferenceLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude, centerPoint.longitude | ||
+ lngReferenceUnit); | ||
var latForRadius = radiusInMeter / centerLatLng.getEarthDistance(latReferenceLatLng).toNumber(); | ||
var lngForRadius = radiusInMeter / centerLatLng.getEarthDistance(lngReferenceLatLng).toNumber(); | ||
var minLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude - latForRadius, centerPoint.longitude - lngForRadius); | ||
var maxLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude + latForRadius, centerPoint.longitude + lngForRadius); | ||
return nodes2ts_1.S2LatLngRect.fromLatLng(minLatLng, maxLatLng); | ||
} | ||
return null; | ||
return latLngRect; | ||
}; | ||
S2Util.getBoundingLatLngRectFromQueryRadiusInput = function (geoQueryRequest) { | ||
var centerPoint = geoQueryRequest.CenterPoint; | ||
var radiusInMeter = geoQueryRequest.RadiusInMeter; | ||
var centerLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude, centerPoint.longitude); | ||
var latReferenceUnit = centerPoint.latitude > 0.0 ? -1.0 : 1.0; | ||
var latReferenceLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude + latReferenceUnit, centerPoint.longitude); | ||
var lngReferenceUnit = centerPoint.longitude > 0.0 ? -1.0 : 1.0; | ||
var lngReferenceLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude, centerPoint.longitude | ||
+ lngReferenceUnit); | ||
var latForRadius = radiusInMeter / centerLatLng.getEarthDistance(latReferenceLatLng).toNumber(); | ||
var lngForRadius = radiusInMeter / centerLatLng.getEarthDistance(lngReferenceLatLng).toNumber(); | ||
var minLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude - latForRadius, centerPoint.longitude - lngForRadius); | ||
var maxLatLng = nodes2ts_1.S2LatLng.fromDegrees(centerPoint.latitude + latForRadius, centerPoint.longitude + lngForRadius); | ||
return nodes2ts_1.S2LatLngRect.fromLatLng(minLatLng, maxLatLng); | ||
}; | ||
return S2Util; | ||
}()); | ||
exports.S2Util = S2Util; |
{ | ||
"name": "dynamodb-geo", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "A javascript port of awslabs/dynamodb-geo, for dynamodb geospatial querying", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
[![npm version](https://badge.fury.io/js/dynamodb-geo.svg)](https://badge.fury.io/js/dynamodb-geo) | ||
[![npm version](https://badge.fury.io/js/dynamodb-geo.svg)](https://badge.fury.io/js/dynamodb-geo) [![CircleCI](https://circleci.com/gh/rh389/dynamodb-geo.js.svg?style=shield)](https://circleci.com/gh/rh389/dynamodb-geo.js) | ||
@@ -13,2 +13,6 @@ # Geo Library for Amazon DynamoDB | ||
## Installation | ||
Using [npm] or [yarn]: | ||
`npm install --save dynamodb-geo` or `yarn add dynamodb-geo`. | ||
## Getting started | ||
@@ -133,3 +137,3 @@ First you'll need to import the AWS sdk and set up your DynamoDB connection: | ||
#### consistentRead: boolean = true; | ||
#### consistentRead: boolean = false | ||
Whether queries use the [`ConsistentRead`][readconsistency] option (for strongly consistent reads) or not (for eventually consistent reads, at half the cost). | ||
@@ -143,17 +147,17 @@ | ||
* Use `false` (`[lat, lon]`) for compatibility with [awslabs/dynamodb-geo][dynamodb-geo] (default) | ||
* Use `true` (`[lon, lat]`) for GeoJSON standard compliance. | ||
* Use `false` (`[lat, lon]`) for compatibility with [awslabs/dynamodb-geo][dynamodb-geo] | ||
* Use `true` (`[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"; | ||
#### 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 6) of the geo hash, used as the hash (aka partition) part of a [hash/range primary key pair][hashrange]. Its value is auto-generated based on item coordinates. | ||
#### hashKeyLength: number = 6; | ||
#### 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][hashrange]. Its value is auto-generated based on item coordinates. | ||
#### hashKeyLength: number = 2 | ||
The number of digits (in base 10) of the 64-bit geo hash to use as the hash key. | ||
#### rangeKeyAttributeName: string = "rangeKey"; | ||
#### 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][hashrange]. Its value must be specified by you (hash-range pairs must be unique). | ||
#### geoJsonAttributeName: string = "geoJson"; | ||
#### geoJsonAttributeName: string = "geoJson" | ||
The name of the attribute which will contain the longitude/latitude pair in a GeoJSON-style point (see also `longitudeFirst`). | ||
#### geohashIndexName: string = "geohash-index"; | ||
#### geohashIndexName: string = "geohash-index" | ||
The name of the index to be created against the geohash. Only used for creating new tables. | ||
@@ -181,2 +185,4 @@ | ||
[npm]: https://www.npmjs.com | ||
[yarn]: https://yarnpkg.com | ||
[updateitem]: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html | ||
@@ -183,0 +189,0 @@ [putitem]: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
178872
29
1469
194