Example usage for com.mongodb.client.model.geojson Polygon Polygon

List of usage examples for com.mongodb.client.model.geojson Polygon Polygon

Introduction

In this page you can find the example usage for com.mongodb.client.model.geojson Polygon Polygon.

Prototype

public Polygon(final PolygonCoordinates coordinates) 

Source Link

Document

Construct an instance with the given coordinates.

Usage

From source file:com.waves_rsp.ikb4stream.consumer.database.DatabaseReader.java

License:Open Source License

/**
 * This method requests events from mongodb database and filters from data coming to the request object in parameter
 *
 * @param request  Request to apply to Mongo
 * @param callback Callback method call after select operation
 * @see DatabaseReader#limit/*w  w  w.  ja  va  2s .c  om*/
 * @see DatabaseReader#mongoCollection
 */
@Override
public void getEvent(Request request, DatabaseReaderCallback callback) {
    List<Position> polygon = Arrays.stream(request.getBoundingBox().getLatLongs())
            .map(l -> new Position(l.getLongitude(), l.getLatitude())).collect(Collectors.toList());
    final long start = System.currentTimeMillis();
    this.mongoCollection
            .find(and(geoIntersects("location", new Polygon(polygon)), lte("start", request.getEnd().getTime()),
                    gte("end", request.getStart().getTime())))
            .limit(limit).into(new ArrayList<Document>(), (result, t) -> {
                long time = System.currentTimeMillis() - start;
                METRICS_LOGGER.log("time_dbreader", time);
                callback.onResult(t,
                        "[" + result.stream().map(Document::toJson).collect(Collectors.joining(", ")) + "]");
            });
}

From source file:com.waves_rsp.ikb4stream.producer.DatabaseWriter.java

License:Open Source License

/**
 * This method inserts an {@link Event} in the database
 *
 * @param event    {@link Event} to insert into database
 * @param callback {@link DatabaseWriterCallback} called after inserting
 * @throws NullPointerException    if event or callback is null
 * @see DatabaseWriter#mongoCollection// w  w w.j  a  va2  s  . co m
 * @see DatabaseWriter#LOCATION_FIELD
 * @see DatabaseWriter#METRICS_LOGGER
 * @see DatabaseWriter#mapper
 */
public void insertEvent(Event event, DatabaseWriterCallback callback) {
    Objects.requireNonNull(event);
    Objects.requireNonNull(callback);
    try {
        long start = System.currentTimeMillis();
        Document document = Document.parse(mapper.writeValueAsString(event));
        document.remove("start");
        document.remove("end");
        document.remove(LOCATION_FIELD);
        document.append("start", event.getStart().getTime());
        document.append("end", event.getEnd().getTime());
        if (event.getLocation().length == 1) {
            document.append(LOCATION_FIELD, new Point(
                    new Position(event.getLocation()[0].getLongitude(), event.getLocation()[0].getLatitude())));
        } else {
            List<Position> positions = Arrays.stream(event.getLocation())
                    .map(p -> new Position(p.getLongitude(), p.getLatitude())).collect(Collectors.toList());
            document.append(LOCATION_FIELD, new Polygon(positions));
        }
        this.mongoCollection.insertOne(document, (result, t) -> callback.onResult(t));
        long time = System.currentTimeMillis() - start;
        METRICS_LOGGER.log("time_dbwriter_" + event.getSource(), time);
    } catch (JsonProcessingException e) {
        LOGGER.error("Invalid event format: event not inserted in database.");
    }
}