Search in sources :

Example 1 with SpatialTemporalConstraintsBuilder

use of org.locationtech.geowave.core.geotime.store.query.api.SpatialTemporalConstraintsBuilder in project geowave by locationtech.

the class GeoWaveGrpcVectorService method spatialTemporalQuery.

@Override
public void spatialTemporalQuery(final SpatialTemporalQueryParametersProtos request, final StreamObserver<FeatureProtos> responseObserver) {
    final String storeName = request.getSpatialParams().getBaseParams().getStoreName();
    final StoreLoader storeLoader = new StoreLoader(storeName);
    // first check to make sure the data store exists
    if (!storeLoader.loadFromConfig(GeoWaveGrpcServiceOptions.geowaveConfigFile)) {
        throw new ParameterException("Cannot find store name: " + storeLoader.getStoreName());
    }
    final DataStore dataStore = storeLoader.createDataStore();
    VectorQueryBuilder bldr = VectorQueryBuilder.newBuilder();
    String typeName = request.getSpatialParams().getBaseParams().getTypeName();
    String indexName = request.getSpatialParams().getBaseParams().getIndexName();
    if (typeName.equalsIgnoreCase("")) {
        typeName = null;
    } else {
        bldr = bldr.addTypeName(typeName);
    }
    if (indexName.equalsIgnoreCase("")) {
        indexName = null;
    } else {
        bldr = bldr.indexName(indexName);
    }
    final int constraintCount = request.getTemporalConstraintsCount();
    SpatialTemporalConstraintsBuilder stBldr = bldr.constraintsFactory().spatialTemporalConstraints();
    for (int i = 0; i < constraintCount; i++) {
        final TemporalConstraintsProtos t = request.getTemporalConstraints(i);
        stBldr.addTimeRange(Interval.of(Instant.ofEpochMilli(Timestamps.toMillis(t.getStartTime())), Instant.ofEpochMilli(Timestamps.toMillis(t.getEndTime()))));
    }
    Geometry queryGeom = null;
    try {
        queryGeom = new WKBReader(JTSFactoryFinder.getGeometryFactory()).read(request.getSpatialParams().getGeometry().toByteArray());
        stBldr = stBldr.spatialConstraints(queryGeom);
        stBldr = stBldr.spatialConstraintsCompareOperation(CompareOperation.valueOf(request.getCompareOperation()));
    } catch (final FactoryRegistryException | org.locationtech.jts.io.ParseException e) {
        LOGGER.error("Exception encountered creating query geometry", e);
    }
    try (final CloseableIterator<SimpleFeature> iterator = dataStore.query(bldr.constraints(stBldr.build()).build())) {
        while (iterator.hasNext()) {
            final SimpleFeature simpleFeature = iterator.next();
            final SimpleFeatureType type = simpleFeature.getType();
            final FeatureProtos.Builder b = FeatureProtos.newBuilder();
            final FeatureAttributeProtos.Builder attBuilder = FeatureAttributeProtos.newBuilder();
            for (int i = 0; i < type.getAttributeDescriptors().size(); i++) {
                setAttributeBuilderValue(simpleFeature.getAttribute(i), attBuilder);
                b.putAttributes(type.getAttributeDescriptors().get(i).getLocalName(), attBuilder.build());
            }
            final FeatureProtos f = b.build();
            responseObserver.onNext(f);
        }
        responseObserver.onCompleted();
    }
}
Also used : VectorQueryBuilder(org.locationtech.geowave.core.geotime.store.query.api.VectorQueryBuilder) SpatialTemporalConstraintsBuilder(org.locationtech.geowave.core.geotime.store.query.api.SpatialTemporalConstraintsBuilder) ByteString(com.google.protobuf.ByteString) WKBReader(org.locationtech.jts.io.WKBReader) SimpleFeature(org.opengis.feature.simple.SimpleFeature) TemporalConstraintsProtos(org.locationtech.geowave.service.grpc.protobuf.TemporalConstraintsProtos) Geometry(org.locationtech.jts.geom.Geometry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FactoryRegistryException(org.geotools.util.factory.FactoryRegistryException) GeoWaveGTDataStore(org.locationtech.geowave.adapter.vector.plugin.GeoWaveGTDataStore) DataStore(org.locationtech.geowave.core.store.api.DataStore) FeatureProtos(org.locationtech.geowave.service.grpc.protobuf.FeatureProtos) StoreLoader(org.locationtech.geowave.core.store.cli.store.StoreLoader) ParameterException(com.beust.jcommander.ParameterException) FeatureAttributeProtos(org.locationtech.geowave.service.grpc.protobuf.FeatureAttributeProtos)

Example 2 with SpatialTemporalConstraintsBuilder

use of org.locationtech.geowave.core.geotime.store.query.api.SpatialTemporalConstraintsBuilder in project geowave by locationtech.

the class CustomAdapterExample method querySpatial.

private void querySpatial() {
    // constraints when querying
    try {
        // This bounding box represents approximately Europe, so only the European POIs will be
        // queried
        final String queryPolygonDefinition = "POLYGON (( " + "-10.55 35.96, " + "-10.55 71.30, " + "56.16 71.30, " + "56.16 35.96, " + "-10.55 35.96" + "))";
        final Geometry queryPolygon = new WKTReader(JTSFactoryFinder.getGeometryFactory()).read(queryPolygonDefinition);
        final SpatialTemporalConstraintsBuilder spatialConstraintsBuilder = new SpatialTemporalConstraintsBuilderImpl();
        final Query<POI> query = QueryBuilder.newBuilder(POI.class).addTypeName(adapter.getTypeName()).indexName(spatialIndex.getName()).constraints(spatialConstraintsBuilder.spatialConstraints(queryPolygon).build()).build();
        System.out.println("Executing query, expecting to match Roman Colosseum and Eiffel Tower...");
        try (final CloseableIterator<POI> iterator = dataStore.query(query)) {
            while (iterator.hasNext()) {
                System.out.println("Query match: " + iterator.next().getName());
            }
        }
    } catch (FactoryRegistryException | ParseException e) {
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) SpatialTemporalConstraintsBuilder(org.locationtech.geowave.core.geotime.store.query.api.SpatialTemporalConstraintsBuilder) FactoryRegistryException(org.geotools.util.factory.FactoryRegistryException) SpatialTemporalConstraintsBuilderImpl(org.locationtech.geowave.core.geotime.store.query.SpatialTemporalConstraintsBuilderImpl) ParseException(org.locationtech.jts.io.ParseException) WKTReader(org.locationtech.jts.io.WKTReader)

Aggregations

FactoryRegistryException (org.geotools.util.factory.FactoryRegistryException)2 SpatialTemporalConstraintsBuilder (org.locationtech.geowave.core.geotime.store.query.api.SpatialTemporalConstraintsBuilder)2 Geometry (org.locationtech.jts.geom.Geometry)2 ParameterException (com.beust.jcommander.ParameterException)1 ByteString (com.google.protobuf.ByteString)1 GeoWaveGTDataStore (org.locationtech.geowave.adapter.vector.plugin.GeoWaveGTDataStore)1 SpatialTemporalConstraintsBuilderImpl (org.locationtech.geowave.core.geotime.store.query.SpatialTemporalConstraintsBuilderImpl)1 VectorQueryBuilder (org.locationtech.geowave.core.geotime.store.query.api.VectorQueryBuilder)1 DataStore (org.locationtech.geowave.core.store.api.DataStore)1 StoreLoader (org.locationtech.geowave.core.store.cli.store.StoreLoader)1 FeatureAttributeProtos (org.locationtech.geowave.service.grpc.protobuf.FeatureAttributeProtos)1 FeatureProtos (org.locationtech.geowave.service.grpc.protobuf.FeatureProtos)1 TemporalConstraintsProtos (org.locationtech.geowave.service.grpc.protobuf.TemporalConstraintsProtos)1 ParseException (org.locationtech.jts.io.ParseException)1 WKBReader (org.locationtech.jts.io.WKBReader)1 WKTReader (org.locationtech.jts.io.WKTReader)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1