Java Code Examples for org.opengis.feature.simple.SimpleFeature#getType()

The following examples show how to use org.opengis.feature.simple.SimpleFeature#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeoWaveAvroFeatureWriter.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a SimpleFeature to an avroSimpleFeature and then serializes it.
 *
 * @param sf Simple Feature to be serialized
 * @param avroObjectToReuse null or AvroSimpleFeature instance to be re-used. If null a new
 *        instance will be allocated
 * @param defaultClassifications null map of attribute names vs. classification. if null all
 *        values will be set to the default classification
 * @param defaultClassification null or default classification. if null and defaultClassifications
 *        are not provided an exception will be thrown
 * @return the serialized feature
 * @throws IOException
 */
public byte[] serializeAvroSimpleFeature(
    final SimpleFeature sf,
    AvroSimpleFeature avroObjectToReuse,
    final Map<String, String> defaultClassifications,
    final String defaultClassification) throws IOException {
  if (sf == null) {
    throw new IOException("Feature cannot be null");
  }

  if ((defaultClassification == null) && (defaultClassifications == null)) {
    throw new IOException(
        "if per attribute classifications aren't provided then a default classification must be provided");
  }

  final SimpleFeatureType sft = sf.getType();
  if (avroObjectToReuse == null) {
    avroObjectToReuse = new AvroSimpleFeature();
  }

  final AvroFeatureDefinition fd =
      GeoWaveAvroFeatureUtils.buildFeatureDefinition(
          avroObjectToReuse.getFeatureType(),
          sft,
          defaultClassifications,
          defaultClassification);
  avroObjectToReuse.setFeatureType(fd);

  final AvroAttributeValues av = GeoWaveAvroFeatureUtils.buildAttributeValue(sf, sft);
  avroObjectToReuse.setValue(av);

  return serializeAvroSimpleFeature(avroObjectToReuse);
}
 
Example 2
Source File: GeoWaveGrpcVectorService.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void cqlQuery(
    final CQLQueryParametersProtos request,
    final StreamObserver<FeatureProtos> responseObserver) {

  final String cql = request.getCql();
  final String storeName = request.getBaseParams().getStoreName();
  final StoreLoader storeLoader = new StoreLoader(storeName);

  String typeName = request.getBaseParams().getTypeName();
  String indexName = request.getBaseParams().getIndexName();

  if (typeName.equalsIgnoreCase("")) {
    typeName = null;
  }
  if (indexName.equalsIgnoreCase("")) {
    indexName = null;
  }

  // first check to make sure the data store exists
  if (!storeLoader.loadFromConfig(GeoWaveGrpcServiceOptions.geowaveConfigFile)) {
    throw new ParameterException("Cannot find store name: " + storeLoader.getStoreName());
  }

  // get a handle to the relevant stores
  final DataStore dataStore = storeLoader.createDataStore();

  VectorQueryBuilder bldr = VectorQueryBuilder.newBuilder();
  if (typeName != null) {
    bldr = bldr.addTypeName(typeName);
  }

  if (indexName != null) {
    bldr = bldr.indexName(indexName);
  }
  try (final CloseableIterator<SimpleFeature> iterator =
      dataStore.query(bldr.constraints(bldr.constraintsFactory().cqlConstraints(cql)).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();
  }
}
 
Example 3
Source File: GeoWaveGrpcVectorService.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void spatialQuery(
    final SpatialQueryParametersProtos request,
    final StreamObserver<FeatureProtos> responseObserver) {

  final String storeName = request.getBaseParams().getStoreName();
  final StoreLoader storeLoader = new StoreLoader(storeName);

  String typeName = request.getBaseParams().getTypeName();
  String indexName = request.getBaseParams().getIndexName();
  VectorQueryBuilder bldr = VectorQueryBuilder.newBuilder();
  if (typeName.equalsIgnoreCase("")) {
    typeName = null;
  } else {
    bldr = bldr.addTypeName(typeName);
  }
  if (indexName.equalsIgnoreCase("")) {
    indexName = null;
  } else {
    bldr = bldr.indexName(indexName);
  }

  // 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();

  Geometry queryGeom = null;

  try {
    queryGeom =
        new WKBReader(JTSFactoryFinder.getGeometryFactory()).read(
            request.getGeometry().toByteArray());
  } 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(
              bldr.constraintsFactory().spatialTemporalConstraints().spatialConstraints(
                  queryGeom).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();
  }
}
 
Example 4
Source File: GeoWaveGrpcVectorService.java    From geowave with Apache License 2.0 4 votes vote down vote up
@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();
  }
}