Write spatiotemporal objects
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
DataStoreinstance (ds) connected to Lindorm GanosBaseA
SimpleFeatureType(sft) and one or moreSimpleFeatureobjects 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
getFeatureWriterAppendonDataStoreto 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
1on 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:
addFeaturesexpects aFeatureCollection, not a rawList. Wrap your list withnew ListFeatureCollection(sft, features)to adapt it.The method returns the count of features actually written, derived from the size of the returned
FeatureIdlist.
/**
* 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
SimpleFeatureTypeorSimpleFeatureobject, see the Quick start topic.