Skip to content

Commit

Permalink
GEOT-5283 fixed addFeatures() behavior for Reader and Iterator
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Gasdorf <fgdrf@users.sourceforge.net>
  • Loading branch information
fgdrf committed Nov 4, 2015
1 parent 74c1383 commit 9c1d519
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 44 deletions.
Expand Up @@ -24,36 +24,24 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Level;

import org.geotools.data.AbstractDataStore;
import org.geotools.data.DataSourceException;
import org.geotools.data.FeatureReader;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Query;
import org.geotools.data.SchemaNotFoundException;
import org.geotools.data.Transaction;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.store.ContentDataStore;
import org.geotools.data.store.ContentEntry;
import org.geotools.data.store.ContentFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.NameImpl;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.Feature;
import org.opengis.feature.FeatureVisitor;
import org.opengis.feature.IllegalAttributeException;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.Name;
import org.opengis.filter.Filter;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;

/**
* This is an example implementation of a DataStore used for testing.
Expand Down Expand Up @@ -126,32 +114,19 @@ public MemoryDataStore(SimpleFeatureIterator reader) throws IOException {
*/
public void addFeatures(FeatureReader <SimpleFeatureType, SimpleFeature> reader) throws IOException {
try {
SimpleFeatureType featureType;
// use an order preserving map, so that features are returned in the same
// order as they were inserted. This is important for repeatable rendering
// of overlapping features.
Map<String,SimpleFeature> featureMap = new LinkedHashMap<String,SimpleFeature>();
String typeName;
SimpleFeature feature;

feature = reader.next();
SimpleFeature feature = reader.next();

if (feature == null) {
throw new IllegalArgumentException("Provided FeatureReader<SimpleFeatureType, SimpleFeature> is closed");
}

featureType = feature.getFeatureType();
typeName = featureType.getTypeName();

featureMap.put(feature.getID(), feature);

addFeatureInternal(feature);

while (reader.hasNext()) {
feature = reader.next();
featureMap.put(feature.getID(), feature);
addFeatureInternal(feature);
}

schema.put(typeName, featureType);
memory.put(typeName, featureMap);
} catch (IllegalAttributeException e) {
throw new DataSourceException("Problem using reader", e);
}
Expand All @@ -170,29 +145,18 @@ public void addFeatures(FeatureReader <SimpleFeatureType, SimpleFeature> reader)
*/
public void addFeatures(SimpleFeatureIterator reader) throws IOException {
try {
SimpleFeatureType featureType;
Map<String,SimpleFeature> featureMap = new LinkedHashMap<String,SimpleFeature>();
String typeName;
SimpleFeature feature;

feature = reader.next();
SimpleFeature feature = reader.next();

if (feature == null) {
throw new IllegalArgumentException("Provided FeatureReader<SimpleFeatureType, SimpleFeature> is closed");
}

featureType = feature.getFeatureType();
typeName = featureType.getTypeName();

featureMap.put(feature.getID(), feature);
addFeatureInternal(feature);

while (reader.hasNext()) {
feature = reader.next();
featureMap.put(feature.getID(), feature);
addFeatureInternal(feature);
}

schema.put(typeName, featureType);
memory.put(typeName, featureMap);
}
finally {
reader.close();
Expand Down
Expand Up @@ -18,8 +18,10 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import org.geotools.data.DataStore;
Expand Down Expand Up @@ -1448,4 +1450,75 @@ public void testRemoveTypeThatDoesntExistsGracefulWithoutIOException()
assertNotNull(namesAfterRemove);
assertEquals(2, namesAfterRemove.size());
}
}

public void testAddingTwoFeaturesWithSameType() throws IOException {
MemoryDataStore mds = new MemoryDataStore();
mds.addFeature(roadFeatures[0]);
mds.addFeature(roadFeatures[1]);

Map<String, SimpleFeature> features = mds.features("road");
assertEquals(2, features.size());
}

public void testCallingAddFeaturesWithArrayTwiceAndExtentInitialCollection() throws IOException {
MemoryDataStore mds = new MemoryDataStore();
mds.addFeatures(roadFeatures);

SimpleFeature road1 = SimpleFeatureBuilder.template(roadType, null);
mds.addFeatures(new SimpleFeature[] {road1});

Map<String, SimpleFeature> features = mds.features("road");
assertEquals(roadFeatures.length + 1, features.size());
}

public void testCallingAddFeaturesWithCollectionTwiceAndExtentInitialCollection() throws IOException {
MemoryDataStore mds = new MemoryDataStore();
mds.addFeatures(Arrays.asList(roadFeatures));

SimpleFeature road1 = SimpleFeatureBuilder.template(roadType, null);

mds.addFeatures(Collections.singletonList(road1));

Map<String, SimpleFeature> features = mds.features("road");
assertEquals(roadFeatures.length + 1, features.size());
}

public void testCallingAddFeaturesWithReaderTwiceAndExtentInitialCollection()
throws IOException {
FeatureReader<SimpleFeatureType, SimpleFeature> reader = DataUtilities
.reader(roadFeatures);
MemoryDataStore mds = new MemoryDataStore(reader);

assertEquals(roadFeatures.length, mds.features(roadType.getTypeName())
.size());

FeatureReader<SimpleFeatureType, SimpleFeature> secondReader = DataUtilities
.reader(new SimpleFeature[] { SimpleFeatureBuilder.template(
roadType, null) });

mds.addFeatures(secondReader);

Map<String, SimpleFeature> features = mds.features("road");
assertEquals(roadFeatures.length + 1, features.size());

}

public void testCallingAddFeaturesWithIteratorTwiceAndExtentInitialCollection()
throws IOException {
FeatureReader<SimpleFeatureType, SimpleFeature> reader = DataUtilities
.reader(roadFeatures);
MemoryDataStore mds = new MemoryDataStore(reader);

assertEquals(roadFeatures.length, mds.features(roadType.getTypeName())
.size());

SimpleFeatureIterator featureIterator = DataUtilities
.collection(new SimpleFeature[] { SimpleFeatureBuilder.template(
roadType, null) }).features();

mds.addFeatures(featureIterator);

Map<String, SimpleFeature> features = mds.features("road");
assertEquals(roadFeatures.length + 1, features.size());

}}

0 comments on commit 9c1d519

Please sign in to comment.