/// <summary> /// Filter out any points outside of the queried area from the input list. /// </summary> /// <param name="list">List of items return by Amazon DynamoDB. It may contains points outside of the actual area queried.</param> /// <param name="geoQueryRequest">List of items within the queried area.</param> /// <returns></returns> private IEnumerable <IDictionary <string, AttributeValue> > Filter(IEnumerable <IDictionary <string, AttributeValue> > list, GeoQueryRequest geoQueryRequest) { var result = new List <IDictionary <String, AttributeValue> >(); S2LatLngRect?latLngRect = null; S2LatLng? centerLatLng = null; double radiusInMeter = 0; if (geoQueryRequest is QueryRectangleRequest) { latLngRect = S2Util.GetBoundingLatLngRect(geoQueryRequest); } foreach (var item in list) { var geoJson = item[_config.GeoJsonAttributeName].S; var geoPoint = GeoJsonMapper.GeoPointFromString(geoJson); var latLng = S2LatLng.FromDegrees(geoPoint.lat, geoPoint.lng); if (latLngRect != null && latLngRect.Value.Contains(latLng)) { result.Add(item); } else if (centerLatLng != null && radiusInMeter > 0 && centerLatLng.Value.GetEarthDistance(latLng) <= radiusInMeter) { result.Add(item); } } return(result); }
public async Task <PutPointResult> PutPointAsync(PutPointRequest putPointRequest, CancellationToken cancellationToken = default(CancellationToken)) { if (putPointRequest == null) { throw new ArgumentNullException("putPointRequest"); } var geohash = S2Manager.GenerateGeohash(putPointRequest.FromGeoPoint); var rangeHash = S2Manager.GenerateGeohash(putPointRequest.ToGeoPoint); var hashKey = S2Manager.GenerateHashKey(geohash, _config.HashKeyLength); var geoJson = GeoJsonMapper.StringFromGeoObject(putPointRequest.FromGeoPoint); var putItemRequest = putPointRequest.PutItemRequest; putItemRequest.TableName = _config.TableName; var hashKeyValue = new AttributeValue { N = hashKey.ToString(CultureInfo.InvariantCulture) }; putItemRequest.Item[_config.HashKeyAttributeName] = hashKeyValue; putItemRequest.Item[_config.RangeKeyAttributeName] = new AttributeValue { S = rangeHash.ToString(CultureInfo.InvariantCulture) }; var geohashValue = new AttributeValue { N = geohash.ToString(CultureInfo.InvariantCulture) }; putItemRequest.Item[_config.GeohashAttributeName] = geohashValue; var geoJsonValue = new AttributeValue { S = geoJson }; putItemRequest.Item[_config.GeoJsonAttributeName] = geoJsonValue; PutItemResponse putItemResult = await _config.DynamoDBClient.PutItemAsync(putItemRequest, cancellationToken).ConfigureAwait(false); var putPointResult = new PutPointResult(putItemResult); return(putPointResult); }