Search in sources :

Example 16 with FeatureIterator

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;
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Feature(org.opengis.feature.Feature)

Example 17 with FeatureIterator

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();
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 18 with FeatureIterator

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);
}
Also used : Query(org.geotoolkit.storage.feature.query.Query) Feature(org.opengis.feature.Feature) URL(java.net.URL) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Filter(org.opengis.filter.Filter) TreeSet(java.util.TreeSet) File(java.io.File) FilterFactory2(org.geotoolkit.filter.FilterFactory2) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with FeatureIterator

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Query(org.geotoolkit.storage.feature.query.Query) IndexedShapefileFeatureStore(org.geotoolkit.data.shapefile.indexed.IndexedShapefileFeatureStore) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 20 with FeatureIterator

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);
    }
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Query(org.geotoolkit.storage.feature.query.Query) TreeSet(java.util.TreeSet) IndexedShapefileFeatureStore(org.geotoolkit.data.shapefile.indexed.IndexedShapefileFeatureStore) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Aggregations

FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)53 Feature (org.opengis.feature.Feature)41 Test (org.junit.Test)34 Query (org.geotoolkit.storage.feature.query.Query)30 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)24 FeatureType (org.opengis.feature.FeatureType)20 ResourceId (org.opengis.filter.ResourceId)20 Coordinate (org.locationtech.jts.geom.Coordinate)11 Point (org.locationtech.jts.geom.Point)11 Session (org.geotoolkit.storage.feature.session.Session)10 Geometry (org.locationtech.jts.geom.Geometry)10 HashMap (java.util.HashMap)9 CheckCloseFeatureIterator (org.geotoolkit.storage.feature.CheckCloseFeatureIterator)9 ArrayList (java.util.ArrayList)8 Date (java.util.Date)7 NoSuchElementException (java.util.NoSuchElementException)7 Filter (org.opengis.filter.Filter)7 File (java.io.File)6 Version (org.geotoolkit.version.Version)6 VersionControl (org.geotoolkit.version.VersionControl)6