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

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

Introduction

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

Prototype

public Point(final Position coordinate) 

Source Link

Document

Construct an instance with the given coordinate.

Usage

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//from  w  ww. j a  va  2  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.");
    }
}