Java Code Examples for org.geotools.feature.simple.SimpleFeatureBuilder#build()

The following examples show how to use org.geotools.feature.simple.SimpleFeatureBuilder#build() . 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: SimpleFeatureCentroidExractorTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws SchemaException {
  final SimpleFeatureType schema =
      DataUtilities.createType("testGeo", "location:Point:srid=4326,name:String");
  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature feature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());
  final GeometryFactory geoFactory = new GeometryFactory();

  feature.setAttribute("location", geoFactory.createPoint(new Coordinate(-45, 45)));

  final Point point = extractor.getCentroid(feature);
  assertEquals(4326, point.getSRID());
}
 
Example 2
Source File: FeatureDataUtils.java    From geowave with Apache License 2.0 6 votes vote down vote up
public static SimpleFeature buildFeature(
    final SimpleFeatureType featureType,
    final Pair<String, Object>[] entries) {

  final List<AttributeDescriptor> descriptors = featureType.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }
  final SimpleFeature newFeature =
      SimpleFeatureBuilder.build(featureType, defaults, UUID.randomUUID().toString());
  for (final Pair<String, Object> entry : entries) {
    newFeature.setAttribute(entry.getKey(), entry.getValue());
  }
  return newFeature;
}
 
Example 3
Source File: JsonDefinitionColumnVisibilityManagementTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws SchemaException, CQLException {
  type =
      DataUtilities.createType(
          "geostuff",
          "geometry:Geometry:srid=4326,vis:java.lang.String,pop:java.lang.Long,pid:String");
  descriptors = type.getAttributeDescriptors();
  defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  newFeature = SimpleFeatureBuilder.build(type, defaults, UUID.randomUUID().toString());
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("vis", "{\"pid\":\"TS\", \"geo.*\":\"S\"}");
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 128.232)));
}
 
Example 4
Source File: CentroidManagerGeoWave.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Override
public SimpleFeature toSimpleFeature(final AnalyticItemWrapper<T> item) {
  final SimpleFeature newFeature = SimpleFeatureBuilder.build(type, defaults, item.getID());
  int i = 0;
  for (final Object value : ((SimpleFeature) item.getWrappedItem()).getAttributes()) {
    if (value instanceof Geometry) {
      final Geometry newValue = convert((Geometry) value, shapeClass);
      if (newValue == null) {
        return null;
      }
      newFeature.setAttribute(i++, newValue);
    } else {
      newFeature.setAttribute(i++, value);
    }
  }
  return newFeature;
}
 
Example 5
Source File: FeatureFixedBinNumericStaticticsTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
private SimpleFeature create(final Double val) {
  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature newFeature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());

  newFeature.setAttribute("pop", val);
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", new Date());
  newFeature.setAttribute("whennot", new Date());
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  return newFeature;
}
 
Example 6
Source File: FeatureHyperLogLogStaticticsTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
private SimpleFeature create(final String pid, final Set<String> set) {
  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature newFeature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());

  newFeature.setAttribute("pid", pid);

  set.add(pid);

  return newFeature;
}
 
Example 7
Source File: FeatureNumericHistogramStaticticsTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
private SimpleFeature create(final Double val) {
  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature newFeature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());

  newFeature.setAttribute("pop", val);
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", new Date());
  newFeature.setAttribute("whennot", new Date());
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  return newFeature;
}
 
Example 8
Source File: GeoMesaGeoIndexer.java    From rya with Apache License 2.0 5 votes vote down vote up
private static SimpleFeature createFeature(final SimpleFeatureType featureType, final Statement statement) throws ParseException {
    final String subject = StatementSerializer.writeSubject(statement);
    final String predicate = StatementSerializer.writePredicate(statement);
    final String object = StatementSerializer.writeObject(statement);
    final String context = StatementSerializer.writeContext(statement);

    // create the feature
    final Object[] noValues = {};

    // create the hash
    final String statementId = Md5Hash.md5Base64(StatementSerializer.writeStatement(statement));
    final SimpleFeature newFeature = SimpleFeatureBuilder.build(featureType, noValues, statementId);

    // write the statement data to the fields
    final Geometry geom = GeoParseUtils.getGeometry(statement, new GmlParser());
    if(geom == null || geom.isEmpty() || !geom.isValid()) {
        throw new ParseException("Could not create geometry for statement " + statement);
    }
    newFeature.setDefaultGeometry(geom);

    newFeature.setAttribute(SUBJECT_ATTRIBUTE, subject);
    newFeature.setAttribute(PREDICATE_ATTRIBUTE, predicate);
    newFeature.setAttribute(OBJECT_ATTRIBUTE, object);
    newFeature.setAttribute(CONTEXT_ATTRIBUTE, context);

    // preserve the ID that we created for this feature
    // (set the hint to FALSE to have GeoTools generate IDs)
    newFeature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);

    return newFeature;
}
 
Example 9
Source File: FeatureSerializationTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws SchemaException {
  final Kryo kryo = new Kryo();

  kryo.register(SimpleFeatureImpl.class, new FeatureSerializer());

  final SimpleFeatureType schema =
      DataUtilities.createType("testGeo", "location:Point:srid=4326,name:String");
  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature feature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());
  final GeometryFactory geoFactory = new GeometryFactory();

  feature.setAttribute("location", geoFactory.createPoint(new Coordinate(-45, 45)));
  final Output output = new OutputChunked();
  kryo.getSerializer(SimpleFeatureImpl.class).write(kryo, output, feature);
  final Input input = new InputChunked();
  input.setBuffer(output.getBuffer());
  final SimpleFeature f2 =
      (SimpleFeature) kryo.getSerializer(SimpleFeatureImpl.class).read(
          kryo,
          input,
          SimpleFeatureImpl.class);
  assertEquals(feature, f2);
}
 
Example 10
Source File: QueryIndexHelperTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
private SimpleFeature createFeature(final Date sTime, final Date eTime) {
  final SimpleFeature instance =
      SimpleFeatureBuilder.build(rangeType, rangeDefaults, UUID.randomUUID().toString());
  instance.setAttribute("pop", Long.valueOf(100));
  instance.setAttribute("pid", UUID.randomUUID().toString());
  instance.setAttribute("start", sTime);
  instance.setAttribute("end", eTime);
  instance.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  return instance;
}
 
Example 11
Source File: QueryIndexHelperTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
private SimpleFeature createSingleTimeFeature(final Date time) {
  final SimpleFeature instance =
      SimpleFeatureBuilder.build(singleType, singleDefaults, UUID.randomUUID().toString());
  instance.setAttribute("pop", Long.valueOf(100));
  instance.setAttribute("pid", UUID.randomUUID().toString());
  instance.setAttribute("when", time);
  instance.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  return instance;
}
 
Example 12
Source File: QueryIndexHelperTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
private SimpleFeature createGeoMercFeature(final Geometry geo) {
  final SimpleFeature instance =
      SimpleFeatureBuilder.build(geoMercType, geoDefaults, UUID.randomUUID().toString());
  instance.setAttribute("pop", Long.valueOf(100));
  instance.setAttribute("pid", UUID.randomUUID().toString());
  instance.setAttribute("geometry", geo);
  return instance;
}
 
Example 13
Source File: QueryIndexHelperTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
private SimpleFeature createGeoFeature(final Geometry geo) {
  final SimpleFeature instance =
      SimpleFeatureBuilder.build(geoType, geoDefaults, UUID.randomUUID().toString());
  instance.setAttribute("pop", Long.valueOf(100));
  instance.setAttribute("pid", UUID.randomUUID().toString());
  instance.setAttribute("geometry", geo);
  return instance;
}
 
Example 14
Source File: FeatureCountMinSketchStaticticsTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
private SimpleFeature create(final String pid) {
  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature newFeature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());

  newFeature.setAttribute("pid", pid);

  return newFeature;
}
 
Example 15
Source File: CQLQueryFilterTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
private SimpleFeature createFeature() {
  final SimpleFeature instance =
      SimpleFeatureBuilder.build(type, defaults, UUID.randomUUID().toString());
  instance.setAttribute("pop", Long.valueOf(100));
  instance.setAttribute("pid", "a89dhd-123-abc");
  instance.setAttribute("geom", factory.createPoint(new Coordinate(27.25, 41.25)));
  return instance;
}
 
Example 16
Source File: ExportGeometryAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private static Map<Class<?>, List<SimpleFeature>> createGeometryToFeaturesListMap(VectorDataNode vectorNode) throws TransformException,
                                                                                                                    SchemaException {
    FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = vectorNode.getFeatureCollection();
    CoordinateReferenceSystem crs = vectorNode.getFeatureType().getCoordinateReferenceSystem();
    if (crs == null) {   // for pins and GCPs crs is null
        crs = vectorNode.getProduct().getSceneCRS();
    }
    final CoordinateReferenceSystem modelCrs;
    if (vectorNode.getProduct().getSceneGeoCoding() instanceof CrsGeoCoding) {
        modelCrs = vectorNode.getProduct().getSceneCRS();
    } else {
        modelCrs = DefaultGeographicCRS.WGS84;
    }

    // Not using ReprojectingFeatureCollection - it is reprojecting all geometries of a feature
    // but we want to reproject the default geometry only
    GeometryCoordinateSequenceTransformer transformer = createTransformer(crs, modelCrs);

    Map<Class<?>, List<SimpleFeature>> featureListMap = new HashMap<>();
    final FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
    // The schema needs to be reprojected. We need to build a new feature be cause we can't change the schema.
    // It is necessary to have this reprojected schema, because otherwise the shapefile is not correctly georeferenced.
    SimpleFeatureType schema = featureCollection.getSchema();
    SimpleFeatureType transformedSchema = FeatureTypes.transform(schema, modelCrs);
    while (featureIterator.hasNext()) {
        SimpleFeature feature = featureIterator.next();
        Object defaultGeometry = feature.getDefaultGeometry();
        feature.setDefaultGeometry(transformer.transform((Geometry) defaultGeometry));

        Class<?> geometryType = defaultGeometry.getClass();
        List<SimpleFeature> featureList = featureListMap.computeIfAbsent(geometryType, k -> new ArrayList<>());
        SimpleFeature exportFeature = SimpleFeatureBuilder.build(transformedSchema, feature.getAttributes(), feature.getID());
        featureList.add(exportFeature);
    }
    return featureListMap;
}
 
Example 17
Source File: GeoWaveGeoIndexer.java    From rya with Apache License 2.0 5 votes vote down vote up
private static SimpleFeature createFeature(final SimpleFeatureType featureType, final Statement statement) throws ParseException {
    final String subject = StatementSerializer.writeSubject(statement);
    final String predicate = StatementSerializer.writePredicate(statement);
    final String object = StatementSerializer.writeObject(statement);
    final String context = StatementSerializer.writeContext(statement);

    // create the feature
    final Object[] noValues = {};

    // create the hash
    final String statementId = Md5Hash.md5Base64(StatementSerializer.writeStatement(statement));
    final SimpleFeature newFeature = SimpleFeatureBuilder.build(featureType, noValues, statementId);

    // write the statement data to the fields
    final Geometry geom = GeoParseUtils.getGeometry(statement, new GmlParser());
    if(geom == null || geom.isEmpty() || !geom.isValid()) {
        throw new ParseException("Could not create geometry for statement " + statement);
    }
    newFeature.setDefaultGeometry(geom);

    newFeature.setAttribute(SUBJECT_ATTRIBUTE, subject);
    newFeature.setAttribute(PREDICATE_ATTRIBUTE, predicate);
    newFeature.setAttribute(OBJECT_ATTRIBUTE, object);
    newFeature.setAttribute(CONTEXT_ATTRIBUTE, context);
    // GeoWave does not support querying based on a user generated feature ID
    // So, we create a separate ID attribute that it can query on.
    newFeature.setAttribute(GEO_ID_ATTRIBUTE, statementId);

    // preserve the ID that we created for this feature
    // (set the hint to FALSE to have GeoTools generate IDs)
    newFeature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);

    return newFeature;
}
 
Example 18
Source File: GeoWaveAvroFeatureDataAdapterTest.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Test
public void testInferredRange() throws SchemaException {

  final SimpleFeatureType schema =
      DataUtilities.createType(
          "sp.geostuff",
          "geometry:Geometry:srid=4326,pop:java.lang.Long,start:Date,end:Date,pid:String");

  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature newFeature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());

  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("start", time1);
  newFeature.setAttribute("end", time2);
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));

  final GeoWaveAvroFeatureDataAdapter dataAdapter =
      new GeoWaveAvroFeatureDataAdapter(
          schema,
          new GlobalVisibilityHandler<SimpleFeature, Object>("default"));
  final Index index = new SpatialIndexBuilder().createIndex();
  dataAdapter.init(index);
  final byte[] binary = dataAdapter.toBinary();

  final GeoWaveAvroFeatureDataAdapter dataAdapterCopy = new GeoWaveAvroFeatureDataAdapter();
  dataAdapterCopy.fromBinary(binary);

  assertEquals(dataAdapterCopy.getTypeName(), dataAdapter.getTypeName());
  assertEquals(dataAdapterCopy.getFeatureType(), dataAdapter.getFeatureType());
  assertEquals(
      Boolean.TRUE,
      dataAdapterCopy.getFeatureType().getDescriptor("end").getUserData().get("end"));
  assertEquals(
      Boolean.TRUE,
      dataAdapterCopy.getFeatureType().getDescriptor("start").getUserData().get("start"));

  final List<IndexFieldHandler<SimpleFeature, ? extends CommonIndexValue, Object>> handlers =
      dataAdapterCopy.getDefaultTypeMatchingHandlers(schema);
  boolean found = false;
  for (final IndexFieldHandler<SimpleFeature, ? extends CommonIndexValue, Object> handler : handlers) {
    found |=
        ((handler instanceof FeatureTimeRangeHandler)
            && ((((FeatureTimeRangeHandler) handler).toIndexValue(
                newFeature).toNumericData().getMin() - time1.getTime()) < 0.001)
            && ((((FeatureTimeRangeHandler) handler).toIndexValue(
                newFeature).toNumericData().getMax() - time2.getTime()) < 0.001));
  }

  assertTrue(found);
}
 
Example 19
Source File: FeatureDataAdapterTest.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Test
public void testInferredRange() throws SchemaException {

  final SimpleFeatureType schema =
      DataUtilities.createType(
          "http://foo",
          "sp.geostuff",
          "geometry:Geometry:srid=4326,pop:java.lang.Long,start:Date,end:Date,pid:String");

  final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature newFeature =
      SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());

  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("start", time1);
  newFeature.setAttribute("end", time2);
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));

  final FeatureDataAdapter dataAdapter =
      new FeatureDataAdapter(
          schema,
          new GlobalVisibilityHandler<SimpleFeature, Object>("default"));
  final Index spatialIndex =
      new SpatialDimensionalityTypeProvider().createIndex(new SpatialOptions());
  dataAdapter.init(spatialIndex);
  final byte[] binary = dataAdapter.toBinary();

  final FeatureDataAdapter dataAdapterCopy = new FeatureDataAdapter();
  dataAdapterCopy.fromBinary(binary);

  assertEquals("http://foo", dataAdapterCopy.getFeatureType().getName().getNamespaceURI());

  assertEquals(dataAdapterCopy.getTypeName(), dataAdapter.getTypeName());
  assertEquals(dataAdapterCopy.getFeatureType(), dataAdapter.getFeatureType());
  assertEquals(
      Boolean.TRUE,
      dataAdapterCopy.getFeatureType().getDescriptor("end").getUserData().get("end"));
  assertEquals(
      Boolean.TRUE,
      dataAdapterCopy.getFeatureType().getDescriptor("start").getUserData().get("start"));

  final List<IndexFieldHandler<SimpleFeature, ? extends CommonIndexValue, Object>> handlers =
      dataAdapterCopy.getDefaultTypeMatchingHandlers(schema);
  boolean found = false;
  for (final IndexFieldHandler<SimpleFeature, ? extends CommonIndexValue, Object> handler : handlers) {
    found |=
        ((handler instanceof FeatureTimeRangeHandler)
            && ((((FeatureTimeRangeHandler) handler).toIndexValue(
                newFeature).toNumericData().getMin() - time1.getTime()) < 0.001)
            && ((((FeatureTimeRangeHandler) handler).toIndexValue(
                newFeature).toNumericData().getMax() - time2.getTime()) < 0.001));
  }

  assertTrue(found);
}
 
Example 20
Source File: AnalyticFeature.java    From geowave with Apache License 2.0 4 votes vote down vote up
public static SimpleFeature createGeometryFeature(
    final SimpleFeatureType featureType,
    final String batchId,
    final String dataId,
    final String name,
    final String groupID,
    final double weight,
    final Geometry geometry,
    final String[] extraDimensionNames,
    final double[] extraDimensions,
    final int zoomLevel,
    final int iteration,
    final long count) {
  if (extraDimensionNames.length != extraDimensions.length) {
    LOGGER.error(
        "The number of extraDimension names does not equal the number of extraDimensions");
    throw new IllegalArgumentException(
        "The number of extraDimension names does not equal the number of extraDimensions");
  }
  final List<AttributeDescriptor> descriptors = featureType.getAttributeDescriptors();
  final Object[] defaults = new Object[descriptors.size()];
  int p = 0;
  for (final AttributeDescriptor descriptor : descriptors) {
    defaults[p++] = descriptor.getDefaultValue();
  }

  final SimpleFeature newFeature = SimpleFeatureBuilder.build(featureType, defaults, dataId);
  newFeature.setAttribute(ClusterFeatureAttribute.NAME.attrName(), name);
  newFeature.setAttribute(ClusterFeatureAttribute.GROUP_ID.attrName(), groupID);
  newFeature.setAttribute(ClusterFeatureAttribute.ITERATION.attrName(), iteration);
  newFeature.setAttribute(ClusterFeatureAttribute.WEIGHT.attrName(), weight);
  newFeature.setAttribute(ClusterFeatureAttribute.BATCH_ID.attrName(), batchId);
  newFeature.setAttribute(ClusterFeatureAttribute.COUNT.attrName(), count);
  newFeature.setAttribute(ClusterFeatureAttribute.GEOMETRY.attrName(), geometry);
  newFeature.setAttribute(ClusterFeatureAttribute.ZOOM_LEVEL.attrName(), zoomLevel);
  int i = 0;
  for (final String dimName : extraDimensionNames) {
    newFeature.setAttribute(dimName, new Double(extraDimensions[i++]));
  }
  return newFeature;
}