Write spatiotemporal objects

Updated at:

Use a GeoTools API writer to insert spatio-temporal SimpleFeature objects into Lindorm . You can write one object at a time using SimpleFeatureWriter, or write a batch of objects in a single call using SimpleFeatureStore.

Prerequisites

Before you begin, make sure that you have:

  • A collection in Lindorm GanosBase

  • Spatio-temporal objects created within that collection

  • A DataStore instance (ds) connected to Lindorm GanosBase

  • A SimpleFeatureType (sft) and one or more SimpleFeature objects to write — see the Quick start topic for details

Write a single SimpleFeature object

Use SimpleFeatureWriter from the GeoTools API to insert one SimpleFeature at a time. SimpleFeatureWriter supports transactions; the example below uses Transaction.AUTO_COMMIT to commit each write immediately.

Things to note:

  • Call getFeatureWriterAppend on DataStore to get a writer positioned at the end of the collection.

  • Copy both the feature's attributes and its user data to the writer's staging object before calling write().

  • Always close the writer after writing.

  • The method returns 1 on success. Any other return value throws an exception.

/**
 * Inserts a single SimpleFeature into Lindorm GanosBase.
 *
 * @param sft     the SimpleFeatureType that defines the schema
 * @param feature the SimpleFeature object to insert
 * @return 1 on success; throws an exception otherwise
 * @throws Exception if the write fails
 */
public int upsert(SimpleFeatureType sft, SimpleFeature feature) throws Exception {
    try {
        SimpleFeatureWriter writer =
            (SimpleFeatureWriter) ds.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT);
        SimpleFeature toWrite = writer.next();
        toWrite.setAttributes(feature.getAttributes());
        toWrite.getUserData().putAll(feature.getUserData());
        writer.write();
        writer.close();
        return 1;
    } catch (Exception e) {
        throw new Exception("Data write error" + e.getMessage());
    }
}

Write multiple SimpleFeature objects

Use SimpleFeatureStore from the GeoTools API to insert a batch of SimpleFeature objects in a single call. SimpleFeatureStore is retrieved via getFeatureSource on DataStore.

Things to note:

  • addFeatures expects a FeatureCollection, not a raw List. Wrap your list with new ListFeatureCollection(sft, features) to adapt it.

  • The method returns the count of features actually written, derived from the size of the returned FeatureId list.

/**
 * Inserts multiple SimpleFeature objects into Lindorm GanosBase.
 *
 * @param sft      the SimpleFeatureType that defines the schema
 * @param features the list of SimpleFeature objects to insert
 * @return the number of features written
 * @throws Exception if the write fails
 */
public int upsert(SimpleFeatureType sft, List<SimpleFeature> features) throws Exception {
    try {
        SimpleFeatureStore featureStore =
            (SimpleFeatureStore) ds.getFeatureSource(sft.getTypeName());
        List<FeatureId> featureIds =
            featureStore.addFeatures(new ListFeatureCollection(sft, features));
        return featureIds.size();
    } catch (Exception e) {
        throw new Exception("Data write error" + e.getMessage());
    }
}

What's next

  • To build a SimpleFeatureType or SimpleFeature object, see the Quick start topic.