GeoTools

OSGeo

Table Of Contents

Previous topic

FeatureSource

Next topic

Exporting

This Page

MemoryDataStore

We do have one MemoryDataStore suitable for storing temporary information in memory prior to saving it out to disk. Please be advised that it is set up to accurately mirror information being located on disk and is not performant in any way. That said it works; and is pretty easy to stuff your data into.

This implementation is actually offered by the gt-main module, it is being documented here for consistency.

References:

Create

MemoryDataStore is not fast - it is for testing. Why is it not fast do you ask? Because we use it to be strict and model what working with an external service is like (as such it is going to copy your data again and again and again).

Unlike most DataStores we will be creating this one by hand, rather than using a factory.:

MemoryDataStore memory = new MemoryDataStore();

// you are on the honour system to only add features of the same type
memory.addFeature( feature );
...
  • Q: Why is this so slow?

    It is slow for two reasons:

    • It is not indexed, every access involves looking at every feature and applying your filter to it
    • It duplicates each feature (so you don’t accidentally modify something outside of a transaction)
    • It probably duplicates each feature in order to apply the feature for extra slowness.
  • Q: Given me something faster

    The gt-main DataUtilities offers several high performance alternatives to MemoryDataStore.

Examples

  • Using a Memory DataStore to alter content.

    Thanks to Mau Macros for the following example:

    SimpleFeatureSource alter(SimpleFeatureCollection collection, String typename,
            SimpleFeatureType featureType, final List<AttributeDescriptor> newTypes) {
        
        try {
            
            // Create target schema
            SimpleFeatureTypeBuilder buildType = new SimpleFeatureTypeBuilder();
            buildType.init(featureType);
            buildType.setName(typename);
            buildType.addAll(newTypes);
            
            final SimpleFeatureType schema = buildType.buildFeatureType();
            // Configure memory datastore
            final MemoryDataStore memory = new MemoryDataStore();
            memory.createSchema(schema);
            
            collection.accepts(new FeatureVisitor() {
                public void visit(Feature feature) {
                    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema);
                    
                    builder.init((SimpleFeature) feature);
                    for (AttributeDescriptor descriptor : newTypes) {
                        builder.add(DataUtilities.defaultValue(descriptor));
                    }
                    
                    SimpleFeature newFeature = builder.buildFeature(feature.getIdentifier().getID());
                    memory.addFeature(newFeature);
                }
            }, null);
            
            return memory.getFeatureSource(typename);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }