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();
}
}
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) {
}
}
Aggregations