Search in sources :

Example 26 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testTestTransaction.

@Test
public void testTestTransaction() throws Exception {
    final IndexedShapefileFeatureStore sds = createDataStore();
    final long idx = sds.getCount(new Query(sds.getName()));
    final Session session = sds.createSession(true);
    Feature[] newFeatures1 = new Feature[1];
    Feature[] newFeatures2 = new Feature[2];
    GeometryFactory fac = org.geotoolkit.geometry.jts.JTS.getFactory();
    newFeatures1[0] = sds.getFeatureType().newInstance();
    newFeatures1[0].setPropertyValue("a", fac.createPoint(new Coordinate(0, 0)));
    newFeatures2[0] = sds.getFeatureType().newInstance();
    newFeatures2[0].setPropertyValue("a", fac.createPoint(new Coordinate(0, 0)));
    newFeatures2[1] = sds.getFeatureType().newInstance();
    newFeatures2[1].setPropertyValue("a", fac.createPoint(new Coordinate(0, 0)));
    session.addFeatures(sds.getName().toString(), FeatureStoreUtilities.collection(newFeatures1));
    session.addFeatures(sds.getName().toString(), FeatureStoreUtilities.collection(newFeatures2));
    session.commit();
    assertEquals(idx + 3, sds.getCount(new Query(sds.getName())));
    sds.close();
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotoolkit.storage.feature.query.Query) Coordinate(org.locationtech.jts.geom.Coordinate) Feature(org.opengis.feature.Feature) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 27 with Query

use of org.geotoolkit.storage.feature.query.Query 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 28 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testRemoveFromBackAndClose.

/**
 * Create a test file, then continue removing the last entry until there are
 * no features left.
 */
@Test
public void testRemoveFromBackAndClose() throws Throwable {
    IndexedShapefileFeatureStore sds = createDataStore();
    int idx = loadFeatures(sds).size();
    while (idx > 0) {
        FeatureWriter writer = null;
        try {
            writer = sds.getFeatureWriter(new Query(sds.getName()));
            while (writer.hasNext()) {
                writer.next();
            }
            writer.remove();
        } finally {
            if (writer != null) {
                writer.close();
                writer = null;
            }
        }
        assertEquals(--idx, loadFeatures(sds).size());
    }
    sds.close();
}
Also used : FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) Point(org.locationtech.jts.geom.Point) Test(org.junit.Test)

Example 29 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class ShapefileQuadTreeReadWriteTest method testWriteTwice.

@Test
public void testWriteTwice() throws Exception {
    copyShapefiles("shapes/stream.shp");
    ShapefileProvider fac = new ShapefileProvider();
    FeatureStore s1 = createDataStore(fac, TestData.url(AbstractTestCaseSupport.class, "shapes/stream.shp"), true);
    GenericName typeName = s1.getNames().iterator().next();
    FeatureType type = s1.getFeatureType(typeName.toString());
    FeatureCollection one = s1.createSession(true).getFeatureCollection(new Query(typeName));
    ShapefileProvider maker = new ShapefileProvider();
    doubleWrite(type, one, getTempFile(), maker, false);
    doubleWrite(type, one, getTempFile(), maker, true);
}
Also used : FeatureType(org.opengis.feature.FeatureType) GenericName(org.opengis.util.GenericName) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) AbstractTestCaseSupport(org.geotoolkit.data.shapefile.AbstractTestCaseSupport) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) ShapefileProvider(org.geotoolkit.data.shapefile.ShapefileProvider) Test(org.junit.Test)

Example 30 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class ShapefileQuadTreeReadWriteTest method test.

private void test(final FeatureType type, final FeatureCollection one, final File tmp, final ShapefileProvider maker, final boolean memorymapped) throws IOException, MalformedURLException, Exception {
    FeatureStore s;
    s = createDataStore(maker, tmp.toURI().toURL(), memorymapped);
    s.createFeatureType(type);
    Session session = s.createSession(true);
    session.addFeatures(type.getName().toString(), one);
    session.commit();
    s = createDataStore(new ShapefileProvider(), tmp.toURI().toURL(), true);
    GenericName typeName = s.getNames().iterator().next();
    FeatureCollection two = s.createSession(true).getFeatureCollection(new Query(typeName));
    // copy values, order is not tested here.
    Collection<Feature> cone = new ArrayList<>();
    Collection<Feature> ctwo = new ArrayList<>();
    FeatureStoreUtilities.fill(one, cone);
    FeatureStoreUtilities.fill(two, ctwo);
// one.containsAll(two);
// two.containsAll(one);
}
Also used : GenericName(org.opengis.util.GenericName) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) Session(org.geotoolkit.storage.feature.session.Session) ShapefileProvider(org.geotoolkit.data.shapefile.ShapefileProvider)

Aggregations

Query (org.geotoolkit.storage.feature.query.Query)82 Test (org.junit.Test)50 Feature (org.opengis.feature.Feature)43 FeatureType (org.opengis.feature.FeatureType)36 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)31 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)30 Session (org.geotoolkit.storage.feature.session.Session)20 Point (org.locationtech.jts.geom.Point)18 ResourceId (org.opengis.filter.ResourceId)18 GenericName (org.opengis.util.GenericName)15 Coordinate (org.locationtech.jts.geom.Coordinate)14 FeatureWriter (org.geotoolkit.storage.feature.FeatureWriter)13 File (java.io.File)10 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)9 FeatureStore (org.geotoolkit.storage.feature.FeatureStore)9 Filter (org.opengis.filter.Filter)9 URL (java.net.URL)8 Date (java.util.Date)8 DataStoreException (org.apache.sis.storage.DataStoreException)8 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)8