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 PointData extractPointData(Map<String, AttributeValue> item, String... fetchAttributes) { PointData pd = new PointData(); try { String geoJsonString = item.get(config().getGeoJsonAttributeName()).getS(); GeoPoint geoPoint = GeoJsonMapper.geoPointFromString(geoJsonString); pd.setLatitude(geoPoint.getLatitude()); pd.setLongitude(geoPoint.getLongitude()); pd.setRangeKey(item.get(config().getRangeKeyAttributeName()).getS()); for (String fa : fetchAttributes) { pd.getData().put(fa, item.get(fa).getS()); } } catch (Exception e) { throw new RuntimeException(e); } return pd; }
private void queryRectangle(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint minPoint = new GeoPoint(requestObject.getDouble("minLat"), requestObject.getDouble("minLng")); GeoPoint maxPoint = new GeoPoint(requestObject.getDouble("maxLat"), requestObject.getDouble("maxLng")); 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"); QueryRectangleRequest queryRectangleRequest = new QueryRectangleRequest(minPoint, maxPoint); queryRectangleRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRectangleResult queryRectangleResult = geoDataManager.queryRectangle(queryRectangleRequest); printGeoQueryResult(queryRectangleResult, out, filterUserId); }
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); }
public void putPoint(PointData pd) { GeoPoint geoPoint = new GeoPoint(pd.getLatitude(), pd.getLongitude()); AttributeValue rangeKeyValue = new AttributeValue().withS(pd.getRangeKey()); PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyValue); for (Entry<String, String> e : pd.getData().entrySet()) { AttributeValue value = new AttributeValue().withS(e.getValue()); putPointRequest.getPutItemRequest().getItem().put(e.getKey(), value); } geoDataManager.putPoint(putPointRequest); }
private void putPoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("s3-photo-url")); AttributeValue titleAttributeValue = new AttributeValue().withS(requestObject.getString("title")); AttributeValue userIdAttributeValue = new AttributeValue().withS(requestObject.getString("userId")); PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyAttributeValue); putPointRequest.getPutItemRequest().addItemEntry("title", titleAttributeValue); putPointRequest.getPutItemRequest().addItemEntry("userId", userIdAttributeValue); PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest); printPutPointResult(putPointResult, out); }
private void getPoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey")); GetPointRequest getPointRequest = new GetPointRequest(geoPoint, rangeKeyAttributeValue); GetPointResult getPointResult = geoDataManager.getPoint(getPointRequest); printGetPointRequest(getPointResult, out); }
private void deletePoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey")); DeletePointRequest deletePointRequest = new DeletePointRequest(geoPoint, rangeKeyAttributeValue); DeletePointResult deletePointResult = geoDataManager.deletePoint(deletePointRequest); printDeletePointResult(deletePointResult, out); }
/** * 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; }
public static long generateGeohash(GeoPoint geoPoint) { S2LatLng latLng = S2LatLng.fromDegrees(geoPoint.getLatitude(), geoPoint.getLongitude()); S2Cell cell = new S2Cell(latLng); S2CellId cellId = cell.id(); return cellId.id(); }
public static GeoPoint geoPointFromString(String jsonString) { try { return mapper.readValue(jsonString, GeoPoint.class); } catch (IOException e) { throw new RuntimeException(e); } }
private void putPoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(UUID.randomUUID().toString()); AttributeValue schoolNameKeyAttributeValue = new AttributeValue().withS(requestObject.getString("schoolName")); PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyAttributeValue); putPointRequest.getPutItemRequest().addItemEntry("schoolName", schoolNameKeyAttributeValue); PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest); printPutPointResult(putPointResult, out); }
private void updatePoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey")); String schoolName = requestObject.getString("schoolName"); AttributeValueUpdate schoolNameValueUpdate = null; String memo = requestObject.getString("memo"); AttributeValueUpdate memoValueUpdate = null; if (schoolName == null || schoolName.equalsIgnoreCase("")) { schoolNameValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.DELETE); } else { AttributeValue schoolNameAttributeValue = new AttributeValue().withS(schoolName); schoolNameValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue( schoolNameAttributeValue); } if (memo == null || memo.equalsIgnoreCase("")) { memoValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.DELETE); } else { AttributeValue memoAttributeValue = new AttributeValue().withS(memo); memoValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(memoAttributeValue); } UpdatePointRequest updatePointRequest = new UpdatePointRequest(geoPoint, rangeKeyAttributeValue); updatePointRequest.getUpdateItemRequest().addAttributeUpdatesEntry("schoolName", schoolNameValueUpdate); updatePointRequest.getUpdateItemRequest().addAttributeUpdatesEntry("memo", memoValueUpdate); UpdatePointResult updatePointResult = geoDataManager.updatePoint(updatePointRequest); printUpdatePointResult(updatePointResult, out); }
private void queryRectangle(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint minPoint = new GeoPoint(requestObject.getDouble("minLat"), requestObject.getDouble("minLng")); GeoPoint maxPoint = new GeoPoint(requestObject.getDouble("maxLat"), requestObject.getDouble("maxLng")); List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config.getRangeKeyAttributeName()); attributesToGet.add(config.getGeoJsonAttributeName()); attributesToGet.add("schoolName"); QueryRectangleRequest queryRectangleRequest = new QueryRectangleRequest(minPoint, maxPoint); queryRectangleRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRectangleResult queryRectangleResult = geoDataManager.queryRectangle(queryRectangleRequest); printGeoQueryResult(queryRectangleResult, out); }
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); }