use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class AbstractTestCaseSupport method firstFeature.
/**
* Returns the first feature in the given feature collection.
*/
protected Feature firstFeature(final FeatureCollection fc) {
FeatureIterator features = fc.iterator();
Feature next = features.next();
features.close();
return next;
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method testUpdating.
/**
* Create a set of features, then remove every other one, updating the
* remaining. Test for removal and proper update after reloading...
*/
@Test
public void testUpdating() throws Throwable {
ShapefileFeatureStore sds = createDataStore();
loadFeatures(sds);
FeatureWriter writer = null;
try {
writer = sds.getFeatureWriter(new Query(sds.getNames().iterator().next()));
while (writer.hasNext()) {
Feature feat = writer.next();
Byte b = (Byte) feat.getPropertyValue("b");
if (b.byteValue() % 2 == 0) {
writer.remove();
} else {
feat.setPropertyValue("b", new Byte((byte) -1));
}
}
} finally {
if (writer != null) {
writer.close();
}
}
FeatureCollection fc = loadFeatures(sds);
assertEquals(10, fc.size());
FeatureIterator i = fc.iterator();
for (; i.hasNext(); ) {
assertEquals(-1, ((Byte) (i.next()).getPropertyValue("b")).byteValue());
}
i.close();
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class IndexedShapefileDataStoreTest method testFidFilter.
@Test
public void testFidFilter() throws Exception {
File shpFile = copyShapefiles(STATE_POP);
URL url = shpFile.toURI().toURL();
IndexedShapefileFeatureStore ds = new IndexedShapefileFeatureStore(url.toURI(), true, true, IndexType.NONE, null);
FeatureCollection features = ds.createSession(true).getFeatureCollection(new Query(ds.getName()));
FeatureIterator indexIter = features.iterator();
Set<String> expectedFids = new HashSet<>();
final Filter fidFilter;
try {
FilterFactory2 ff = FilterUtilities.FF;
Set<Filter<Object>> fids = new HashSet<>();
while (indexIter.hasNext()) {
Feature newFeature = indexIter.next();
String id = FeatureExt.getId(newFeature).getIdentifier();
expectedFids.add(id);
fids.add(ff.resourceId(id));
}
switch(fids.size()) {
case 0:
fidFilter = Filter.exclude();
break;
case 1:
fidFilter = fids.iterator().next();
break;
default:
fidFilter = ff.or(fids);
break;
}
} finally {
indexIter.close();
}
Set<String> actualFids = new HashSet<>();
{
indexIter = ds.getFeatureReader(Query.filtered(ds.getName().toString(), fidFilter));
while (indexIter.hasNext()) {
Feature next = indexIter.next();
String id = FeatureExt.getId(next).getIdentifier();
actualFids.add(id);
}
indexIter.close();
}
TreeSet<String> lackingFids = new TreeSet<>(expectedFids);
lackingFids.removeAll(actualFids);
TreeSet<String> unexpectedFids = new TreeSet<>(actualFids);
unexpectedFids.removeAll(expectedFids);
String lacking = String.valueOf(lackingFids);
String unexpected = String.valueOf(unexpectedFids);
String failureMsg = "lacking fids: " + lacking + ". Unexpected ones: " + unexpected;
assertEquals(failureMsg, expectedFids.size(), actualFids.size());
assertEquals(failureMsg, expectedFids, actualFids);
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class IndexedFidReaderTest method testFindAllFids.
@Test
public void testFindAllFids() throws Exception {
int expectedCount = 0;
final Set<String> expectedFids = new LinkedHashSet<>();
final IndexedShapefileFeatureStore ds = new IndexedShapefileFeatureStore(backshp.toURI(), true, true, IndexType.NONE, null);
final FeatureIterator features = ds.getFeatureReader(new Query(ds.getNames().iterator().next()));
while (features.hasNext()) {
final Feature next = features.next();
expectedCount++;
expectedFids.add(FeatureExt.getId(next).getIdentifier());
}
features.close();
assertTrue(expectedCount > 0);
assertEquals(expectedCount, reader.getCount());
for (String fid : expectedFids) {
final long offset = reader.findFid(fid);
assertFalse(-1 == offset);
}
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class IndexedFidReaderTest method testFindAllFidsReverseOrder.
@Test
public void testFindAllFidsReverseOrder() throws Exception {
int expectedCount = 0;
final Set<String> expectedFids = new TreeSet<>(Collections.reverseOrder());
final IndexedShapefileFeatureStore ds = new IndexedShapefileFeatureStore(backshp.toURI(), true, true, IndexType.NONE, null);
final FeatureIterator features = ds.getFeatureReader(new Query(ds.getNames().iterator().next()));
while (features.hasNext()) {
final Feature next = features.next();
expectedCount++;
expectedFids.add(FeatureExt.getId(next).getIdentifier());
}
features.close();
assertTrue(expectedCount > 0);
assertEquals(expectedCount, reader.getCount());
assertFalse("findFid for archsites.5 returned -1", -1 == reader.findFid("archsites.5"));
assertFalse("findFid for archsites.25 returned -1", -1 == reader.findFid("archsites.25"));
for (String fid : expectedFids) {
final long offset = reader.findFid(fid);
assertNotNull(offset);
// System.out.println(fid + "=" + offset + ", ");
assertFalse("findFid for " + fid + " returned -1", -1 == offset);
}
}
Aggregations