Search in sources :

Example 11 with Event

use of org.apache.rya.indexing.geotemporal.model.Event in project incubator-rya by apache.

the class EventDocumentConverter method fromDocument.

@Override
public Event fromDocument(final Document document) throws DocumentConverterException {
    requireNonNull(document);
    final boolean isInstant;
    // Preconditions.
    if (!document.containsKey(SUBJECT)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + SUBJECT + "' field is missing.");
    }
    if (document.containsKey(INSTANT)) {
        isInstant = true;
    } else {
        isInstant = false;
    }
    final String subject = document.getString(SUBJECT);
    final Event.Builder builder = new Event.Builder().setSubject(new RyaURI(subject));
    if (document.containsKey(GEO_KEY)) {
        final Document geoObj = (Document) document.get(GEO_KEY);
        final GeometryFactory geoFact = new GeometryFactory();
        final String typeString = (String) geoObj.get("type");
        final CoordinateList coords = new CoordinateList();
        final Geometry geo;
        if (typeString.equals("Point")) {
            final List<Double> point = (List<Double>) geoObj.get("coordinates");
            final Coordinate coord = new Coordinate(point.get(0), point.get(1));
            geo = geoFact.createPoint(coord);
        } else if (typeString.equals("LineString")) {
            final List<List<Double>> pointsList = (List<List<Double>>) geoObj.get("coordinates");
            for (final List<Double> point : pointsList) {
                coords.add(new Coordinate(point.get(0), point.get(1)));
            }
            geo = geoFact.createLineString(coords.toCoordinateArray());
        } else {
            final List<List<List<Double>>> pointsList = (List<List<List<Double>>>) geoObj.get("coordinates");
            if (pointsList.size() == 1) {
                final List<List<Double>> poly = pointsList.get(0);
                for (final List<Double> point : poly) {
                    coords.add(new Coordinate(point.get(0), point.get(1)));
                }
                geo = geoFact.createPolygon(coords.toCoordinateArray());
            } else {
                final List<List<Double>> first = pointsList.get(0);
                final CoordinateList shellCoords = new CoordinateList();
                for (final List<Double> point : pointsList.get(0)) {
                    shellCoords.add(new Coordinate(point.get(0), point.get(1)));
                }
                final LinearRing shell = geoFact.createLinearRing(shellCoords.toCoordinateArray());
                final List<List<List<Double>>> holesPoints = pointsList.subList(1, pointsList.size() - 1);
                final LinearRing[] holes = new LinearRing[holesPoints.size()];
                for (int ii = 0; ii < holes.length; ii++) {
                    final List<List<Double>> holePoints = holesPoints.get(ii);
                    final CoordinateList shells = new CoordinateList();
                    for (final List<Double> point : pointsList.get(0)) {
                        shells.add(new Coordinate(point.get(0), point.get(1)));
                    }
                    holes[ii] = geoFact.createLinearRing(shells.toCoordinateArray());
                }
                geo = geoFact.createPolygon(shell, holes);
            }
        }
        builder.setGeometry(geo);
    }
    if (isInstant) {
        // we already know the key exists
        final Date date = (Date) document.get(INSTANT);
        final DateTime dt = new DateTime(date.getTime());
        final TemporalInstant instant = new TemporalInstantRfc3339(dt);
        builder.setTemporalInstant(instant);
    } else if (document.containsKey(INTERVAL_START)) {
        Date date = (Date) document.get(INTERVAL_START);
        DateTime dt = new DateTime(date.getTime());
        final TemporalInstant begining = new TemporalInstantRfc3339(dt);
        date = (Date) document.get(INTERVAL_END);
        dt = new DateTime(date.getTime());
        final TemporalInstant end = new TemporalInstantRfc3339(dt);
        final TemporalInterval interval = new TemporalInterval(begining, end);
        builder.setTemporalInterval(interval);
    }
    return builder.build();
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) CoordinateList(com.vividsolutions.jts.geom.CoordinateList) TemporalInstantRfc3339(org.apache.rya.indexing.TemporalInstantRfc3339) Document(org.bson.Document) TemporalInstant(org.apache.rya.indexing.TemporalInstant) Date(java.util.Date) DateTime(org.joda.time.DateTime) Geometry(com.vividsolutions.jts.geom.Geometry) RyaURI(org.apache.rya.api.domain.RyaURI) Coordinate(com.vividsolutions.jts.geom.Coordinate) Event(org.apache.rya.indexing.geotemporal.model.Event) CoordinateList(com.vividsolutions.jts.geom.CoordinateList) List(java.util.List) LinearRing(com.vividsolutions.jts.geom.LinearRing) TemporalInterval(org.apache.rya.indexing.TemporalInterval)

Example 12 with Event

use of org.apache.rya.indexing.geotemporal.model.Event in project incubator-rya by apache.

the class MongoGeoTemporalIndexer method deleteStatement.

@Override
public void deleteStatement(final RyaStatement statement) throws IOException {
    requireNonNull(statement);
    final RyaURI subject = statement.getSubject();
    try {
        final EventStorage eventStore = events.get();
        checkState(events != null, "Must set this indexers configuration before storing statements.");
        new EventUpdater(eventStore).update(subject, old -> {
            final Event.Builder updated;
            if (!old.isPresent()) {
                return Optional.empty();
            } else {
                updated = Event.builder(old.get());
            }
            final Event currentEvent = updated.build();
            final URI pred = statement.getObject().getDataType();
            if ((pred.equals(GeoConstants.GEO_AS_WKT) || pred.equals(GeoConstants.GEO_AS_GML) || pred.equals(GeoConstants.XMLSCHEMA_OGC_WKT) || pred.equals(GeoConstants.XMLSCHEMA_OGC_GML)) && currentEvent.getGeometry().isPresent()) {
                // is geo and needs to be removed.
                try {
                    if (currentEvent.getGeometry().get().equals(GeoParseUtils.getGeometry(RyaToRdfConversions.convertStatement(statement), new GmlParser()))) {
                        updated.setGeometry(null);
                    }
                } catch (final Exception e) {
                    LOG.debug("Unable to parse the stored geometry.");
                }
            } else {
                // is time
                final String dateTime = statement.getObject().getData();
                final Matcher matcher = TemporalInstantRfc3339.PATTERN.matcher(dateTime);
                if (matcher.find()) {
                    final TemporalInterval interval = TemporalInstantRfc3339.parseInterval(dateTime);
                    if (currentEvent.getInterval().get().equals(interval)) {
                        updated.setTemporalInterval(null);
                    }
                } else {
                    final TemporalInstant instant = new TemporalInstantRfc3339(DateTime.parse(dateTime));
                    if (currentEvent.getInstant().get().equals(instant)) {
                        updated.setTemporalInstant(null);
                    }
                }
            }
            return Optional.of(updated.build());
        });
    } catch (final IndexingException e) {
        throw new IOException("Failed to update the Entity index.", e);
    }
}
Also used : Matcher(java.util.regex.Matcher) TemporalInstantRfc3339(org.apache.rya.indexing.TemporalInstantRfc3339) GmlParser(org.apache.rya.indexing.mongodb.geo.GmlParser) IOException(java.io.IOException) TemporalInstant(org.apache.rya.indexing.TemporalInstant) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) IndexingException(org.apache.rya.indexing.mongodb.IndexingException) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) RyaURI(org.apache.rya.api.domain.RyaURI) IndexingException(org.apache.rya.indexing.mongodb.IndexingException) Event(org.apache.rya.indexing.geotemporal.model.Event) TemporalInterval(org.apache.rya.indexing.TemporalInterval) EventStorage(org.apache.rya.indexing.geotemporal.storage.EventStorage)

Aggregations

Event (org.apache.rya.indexing.geotemporal.model.Event)12 RyaURI (org.apache.rya.api.domain.RyaURI)10 EventStorage (org.apache.rya.indexing.geotemporal.storage.EventStorage)9 Test (org.junit.Test)9 TemporalInstant (org.apache.rya.indexing.TemporalInstant)8 TemporalInstantRfc3339 (org.apache.rya.indexing.TemporalInstantRfc3339)8 Coordinate (com.vividsolutions.jts.geom.Coordinate)7 Geometry (com.vividsolutions.jts.geom.Geometry)7 Document (org.bson.Document)3 TemporalInterval (org.apache.rya.indexing.TemporalInterval)2 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 DBObject (com.mongodb.DBObject)1 MongoException (com.mongodb.MongoException)1 CoordinateList (com.vividsolutions.jts.geom.CoordinateList)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 LinearRing (com.vividsolutions.jts.geom.LinearRing)1 ParseException (com.vividsolutions.jts.io.ParseException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1