public List<PointData> getPointsWithinRadius(double latitude, double longitude, double radiusInMeter, String... fetchAttributes) { GeoPoint centerPoint = new GeoPoint(latitude, longitude); //System.out.println(latitude + "/" + longitude); //TODO remove DEBUG code List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config().getRangeKeyAttributeName()); attributesToGet.add(config().getGeoJsonAttributeName()); for (String fa : fetchAttributes) { attributesToGet.add(fa); } QueryRadiusRequest queryRadiusRequest = new QueryRadiusRequest(centerPoint, radiusInMeter); queryRadiusRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRadiusResult queryRadiusResult = geoDataManager.queryRadius(queryRadiusRequest); List<PointData> result = new LinkedList<PointData>(); for (Map<String, AttributeValue> item : queryRadiusResult.getItem()) { //System.out.println(" => " + item); //TODO remove DEBUG code PointData pd = extractPointData(item, fetchAttributes); result.add(pd); } return result; }
private void queryRadius(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint centerPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); double radiusInMeter = requestObject.getDouble("radiusInMeter"); String filterUserId = requestObject.getString("filterUserId"); List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config.getRangeKeyAttributeName()); attributesToGet.add(config.getGeoJsonAttributeName()); attributesToGet.add("title"); attributesToGet.add("userId"); QueryRadiusRequest queryRadiusRequest = new QueryRadiusRequest(centerPoint, radiusInMeter); queryRadiusRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRadiusResult queryRadiusResult = geoDataManager.queryRadius(queryRadiusRequest); printGeoQueryResult(queryRadiusResult, out, filterUserId); }
/** * 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 latLngRect * Queried area. Any points outside of this area need to be discarded. * * @return List of items within the queried area. */ private List<Map<String, AttributeValue>> filter(List<Map<String, AttributeValue>> list, GeoQueryRequest geoQueryRequest) { List<Map<String, AttributeValue>> result = new ArrayList<Map<String, AttributeValue>>(); S2LatLngRect latLngRect = null; S2LatLng centerLatLng = null; double radiusInMeter = 0; if (geoQueryRequest instanceof QueryRectangleRequest) { latLngRect = S2Util.getBoundingLatLngRect(geoQueryRequest); } else if (geoQueryRequest instanceof QueryRadiusRequest) { GeoPoint centerPoint = ((QueryRadiusRequest) geoQueryRequest).getCenterPoint(); centerLatLng = S2LatLng.fromDegrees(centerPoint.getLatitude(), centerPoint.getLongitude()); radiusInMeter = ((QueryRadiusRequest) geoQueryRequest).getRadiusInMeter(); } for (Map<String, AttributeValue> item : list) { String geoJson = item.get(config.getGeoJsonAttributeName()).getS(); GeoPoint geoPoint = GeoJsonMapper.geoPointFromString(geoJson); S2LatLng latLng = S2LatLng.fromDegrees(geoPoint.getLatitude(), geoPoint.getLongitude()); if (latLngRect != null && latLngRect.contains(latLng)) { result.add(item); } else if (centerLatLng != null && radiusInMeter > 0 && centerLatLng.getEarthDistance(latLng) <= radiusInMeter) { result.add(item); } } return result; }
private void queryRadius(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint centerPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); double radiusInMeter = requestObject.getDouble("radiusInMeter"); List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config.getRangeKeyAttributeName()); attributesToGet.add(config.getGeoJsonAttributeName()); attributesToGet.add("schoolName"); QueryRadiusRequest queryRadiusRequest = new QueryRadiusRequest(centerPoint, radiusInMeter); queryRadiusRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRadiusResult queryRadiusResult = geoDataManager.queryRadius(queryRadiusRequest); printGeoQueryResult(queryRadiusResult, out); }
/** * <p> * Query a circular area constructed by a center point and its radius. * </p> * <b>Sample usage:</b> * * <pre> * GeoPoint centerPoint = new GeoPoint(47.5, -122.3); * * QueryRadiusRequest queryRadiusRequest = new QueryRadiusRequest(centerPoint, 100); * QueryRadiusResult queryRadiusResult = geoIndexManager.queryRadius(queryRadiusRequest); * * for (Map<String, AttributeValue> item : queryRadiusResult.getItem()) { * System.out.println("item: " + item); * } * </pre> * * @param queryRadiusRequest * Container for the necessary parameters to execute radius query request. * * @return Result of radius query request. * */ public QueryRadiusResult queryRadius(QueryRadiusRequest queryRadiusRequest) { S2LatLngRect latLngRect = S2Util.getBoundingLatLngRect(queryRadiusRequest); S2CellUnion cellUnion = S2Manager.findCellIds(latLngRect); List<GeohashRange> ranges = mergeCells(cellUnion); cellUnion = null; return new QueryRadiusResult(dispatchQueries(ranges, queryRadiusRequest)); }