Search in sources :

Example 41 with FeatureIterator

use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testVersioningASynchrone.

/**
 * Check versions are created only on session commit calls.
 */
@Test
public void testVersioningASynchrone() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Version version;
    Feature feature;
    ResourceId fid;
    FeatureIterator ite;
    Query qb = new Query();
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    // //////////////////////////////////////////////////////////////////////
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    versions = vc.list();
    assertTrue(versions.isEmpty());
    final Session session = store.createSession(true);
    // //////////////////////////////////////////////////////////////////////
    // make an insert ///////////////////////////////////////////////////////
    final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.TRUE);
    feature.setPropertyValue("integer", 14);
    feature.setPropertyValue("point", firstPoint);
    feature.setPropertyValue("string", "someteststring");
    session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have 0 version
    versions = vc.list();
    assertEquals(0, versions.size());
    // <-- creates a version
    session.commit();
    // we should have 1 version
    versions = vc.list();
    assertEquals(1, versions.size());
    version = versions.get(0);
    Date date = version.getDate();
    // ensure normal reading is correct without version----------------------
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    ite = session.getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // //////////////////////////////////////////////////////////////////////
    // make 2 updates at the time ///////////////////////////////////////////
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    session.updateFeatures(refType.getName().toString(), fid, updates);
    // we should have 1 version
    versions = vc.list();
    assertEquals(1, versions.size());
    final Point thirdPoint = GF.createPoint(new Coordinate(48, -51));
    updates = new HashMap<>();
    updates.put("boolean", Boolean.TRUE);
    updates.put("integer", -89);
    updates.put("point", thirdPoint);
    updates.put("string", "thridupdatetext");
    session.updateFeatures(refType.getName().toString(), fid, updates);
    // we should have 1 version
    versions = vc.list();
    assertEquals(1, versions.size());
    // <-- creates a version
    session.commit();
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // ensure we read the latest --------------------------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.TRUE, feature.getProperty("boolean").getValue());
        assertEquals(-89, feature.getProperty("integer").getValue());
        assertEquals(thirdPoint, feature.getProperty("point").getValue());
        assertEquals("thridupdatetext", feature.getProperty("string").getValue());
    } finally {
        ite.close();
    }
    // //////////////////////////////////////////////////////////////////////
    // make delete + insert at the same time ///////////////////////////////
    session.removeFeatures(refType.getName().toString(), fid);
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    assertEquals(0, session.getCount(qb));
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // //////////////////////////////////////////////////////////////////////
    // delete record ////////////////////////////////////////////////////////
    session.removeFeatures(refType.getName().toString(), fid);
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    assertEquals(0, session.getCount(qb));
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    Point fourthPoint = GF.createPoint(new Coordinate(66, 11));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.FALSE);
    feature.setPropertyValue("integer", 22);
    feature.setPropertyValue("point", fourthPoint);
    feature.setPropertyValue("string", "fourthupdateString");
    session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // <-- creates a version
    session.commit();
    // we should have three versions
    versions = vc.list();
    assertEquals(3, versions.size());
    // ensure we read the latest --------------------------------------------
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.FALSE, feature.getProperty("boolean").getValue());
        assertEquals(22, feature.getProperty("integer").getValue());
        assertEquals(fourthPoint, feature.getProperty("point").getValue());
        assertEquals("fourthupdateString", feature.getProperty("string").getValue());
    } finally {
        ite.close();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 42 with FeatureIterator

use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testDistinctSchema.

@Test
public void testDistinctSchema() throws DataStoreException, VersioningException, FileNotFoundException, IOException {
    reload(true);
    List<Version> versions;
    Feature feature;
    ResourceId fid;
    Version v0;
    Version v1;
    Version v2;
    FeatureIterator ite;
    Query qb = new Query();
    final FeatureType refType = FTYPE_SIMPLE;
    // ------------------- initialize public2 schema --------------------
    // / creation 2eme table
    PostgresStore store2;
    final ParameterValueGroup params2 = params.clone();
    params2.parameter("schema").setValue("public2");
    store2 = (PostgresStore) DataStores.open(params2);
    // -------------- create schema in public2 schema --------------------
    try {
        ((DataStoreFactory) store2.getProvider()).create(params2);
    } catch (Exception ex) {
    // schema public2 already exist
    }
    for (GenericName n : store2.getNames()) {
        VersionControl vc = store2.getVersioning(n.toString());
        vc.dropVersioning();
        store2.deleteFeatureType(n.toString());
    }
    assertTrue(store2.getNames().isEmpty());
    // delete historisation functions, he must create them himself
    store2.dropHSFunctions();
    // -------------- create table in public schema --------------------
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    assertTrue(store2.getNames().isEmpty());
    // get version control
    final VersionControl vcP1 = store.getVersioning(refType.getName().toString());
    assertNotNull(vcP1);
    assertTrue(vcP1.isEditable());
    assertFalse(vcP1.isVersioned());
    // -------------------- start versioning in public schema ---------------
    vcP1.startVersioning();
    assertTrue(vcP1.isVersioned());
    versions = vcP1.list();
    assertTrue(versions.isEmpty());
    // --------------------- table creation in public2 schema ---------------
    store2.createFeatureType(refType);
    assertEquals(1, store2.getNames().size());
    // get version control
    final VersionControl vcP2 = store2.getVersioning(refType.getName().toString());
    assertNotNull(vcP2);
    assertTrue(vcP2.isEditable());
    assertFalse(vcP2.isVersioned());
    // -------------------- start versioning in public schema ---------------
    vcP2.startVersioning();
    assertTrue(vcP2.isVersioned());
    versions = vcP2.list();
    assertTrue(versions.isEmpty());
    /* insert, update and delete some elements in public schema and verify
         * public2 schema stay empty, to verify the 2th schema are actions distincts.*/
    // make an insert ///////////////////////////////////////////////////////
    final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.TRUE);
    feature.setPropertyValue("integer", 14);
    feature.setPropertyValue("point", firstPoint);
    feature.setPropertyValue("string", "someteststring");
    store.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // make an update ///////////////////////////////////////////////////////
    // get feature to update
    // get identifier
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    final Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    store.updateFeatures(refType.getName().toString(), fid, updates);
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // make a remove ///////////////////////////////////////////////////////
    store.removeFeatures(refType.getName().toString(), fid);
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // get all versions organized in increase dates order.
    versions = vcP1.list();
    assertEquals(3, versions.size());
    v0 = versions.get(0);
    v1 = versions.get(1);
    v2 = versions.get(2);
    vcP1.revert(v1.getDate());
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    vcP1.trim(v1.getDate());
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) VersioningException(org.geotoolkit.version.VersioningException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) GenericName(org.opengis.util.GenericName) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) DataStoreFactory(org.geotoolkit.storage.DataStoreFactory) Test(org.junit.Test)

Example 43 with FeatureIterator

use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testWipesOutInvalidFidsFromFilters.

/**
 * Issueing a request, whether its a query, update or delete, with a fid filter where feature
 * ids match the {@code <typeName>.<number>} structure but the {@code <typeName>} part does not
 * match the actual typeName, shoud ensure the invalid fids are ignored
 *
 * @throws java.lang.Exception
 */
@Test
public void testWipesOutInvalidFidsFromFilters() throws Exception {
    final IndexedShapefileFeatureStore ds = createDataStore();
    final Session session = ds.createSession(true);
    final String validFid1, validFid2, invalidFid1, invalidFid2;
    try (FeatureIterator features = ds.getFeatureReader(new Query(ds.getName()))) {
        validFid1 = FeatureExt.getId(features.next()).getIdentifier();
        validFid2 = FeatureExt.getId(features.next()).getIdentifier();
        invalidFid1 = "_" + FeatureExt.getId(features.next()).getIdentifier();
        invalidFid2 = FeatureExt.getId(features.next()).getIdentifier() + "abc";
    }
    FilterFactory2 ff = FilterUtilities.FF;
    Set<Filter<Object>> ids = new HashSet<>();
    ids.add(ff.resourceId(validFid1));
    ids.add(ff.resourceId(validFid2));
    ids.add(ff.resourceId(invalidFid1));
    ids.add(ff.resourceId(invalidFid2));
    Filter fidFilter = ff.or(ids);
    final FeatureType schema = ds.getFeatureType();
    final String typeName = schema.getName().tip().toString();
    // get a property of type String to update its value by the given filter
    assertEquals(2, count(ds, typeName, fidFilter));
    session.updateFeatures(ds.getName().toString(), fidFilter, Collections.singletonMap("f", "modified"));
    session.commit();
    Filter modifiedFilter = ff.equal(ff.property("f"), ff.literal("modified"));
    assertEquals(2, count(ds, typeName, modifiedFilter));
    final long initialCount = ds.getCount(new Query(ds.getName()));
    session.removeFeatures(ds.getName().toString(), fidFilter);
    session.commit();
    final long afterCount = ds.getCount(new Query(ds.getName()));
    assertEquals(initialCount - 2, afterCount);
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) Filter(org.opengis.filter.Filter) FilterFactory2(org.geotoolkit.filter.FilterFactory2) Session(org.geotoolkit.storage.feature.session.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 44 with FeatureIterator

use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method performQueryComparison.

private ArrayList performQueryComparison(final IndexedShapefileFeatureStore indexedDS, final IndexedShapefileFeatureStore baselineDS, final JTSEnvelope2D newBounds) throws FactoryRegistryException, IOException, DataStoreException {
    FeatureCollection features;
    FeatureIterator indexIter;
    FilterFactory2 fac = FilterUtilities.FF;
    String geometryName = FeatureExt.getDefaultGeometry(indexedDS.getFeatureType()).getName().tip().toString();
    Filter filter = fac.bbox(fac.property(geometryName), newBounds);
    features = indexedDS.createSession(true).getFeatureCollection(Query.filtered(indexedDS.getName().toString(), filter));
    FeatureCollection features2 = baselineDS.createSession(true).getFeatureCollection(Query.filtered(baselineDS.getName().toString(), filter));
    FeatureIterator baselineIter = features2.iterator();
    indexIter = features.iterator();
    ArrayList baselineFeatures = new ArrayList();
    ArrayList indexedFeatures = new ArrayList();
    try {
        while (baselineIter.hasNext()) {
            baselineFeatures.add(baselineIter.next());
        }
        while (indexIter.hasNext()) {
            indexedFeatures.add(indexIter.next());
        }
        assertFalse(indexIter.hasNext());
        assertFalse(baselineIter.hasNext());
        assertTrue(baselineFeatures.containsAll(indexedFeatures));
        assertTrue(indexedFeatures.containsAll(baselineFeatures));
    } finally {
        indexIter.close();
        baselineIter.close();
    }
    return indexedFeatures;
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Filter(org.opengis.filter.Filter) ArrayList(java.util.ArrayList) FilterFactory2(org.geotoolkit.filter.FilterFactory2)

Example 45 with FeatureIterator

use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testCreateAndReadQIX.

@Test
public void testCreateAndReadQIX() throws Exception {
    File shpFile = copyShapefiles(STATE_POP);
    URL url = shpFile.toURI().toURL();
    String filename = url.getFile();
    filename = filename.substring(0, filename.lastIndexOf("."));
    File file = new File(filename + ".qix");
    if (file.exists()) {
        file.delete();
    }
    file.deleteOnExit();
    IndexedShapefileFeatureStore ds = new IndexedShapefileFeatureStore(url.toURI(), true, true, IndexType.QIX, null);
    FeatureIterator indexIter = ds.getFeatureReader(new Query(ds.getName()));
    GeometryFactory factory = org.geotoolkit.geometry.jts.JTS.getFactory();
    double area = Double.MAX_VALUE;
    Feature smallestFeature = null;
    while (indexIter.hasNext()) {
        Feature newFeature = indexIter.next();
        Geometry geometry = factory.toGeometry(new JTSEnvelope2D(FeatureExt.getEnvelope(newFeature)));
        double newArea = geometry.getArea();
        if (smallestFeature == null || newArea < area) {
            smallestFeature = newFeature;
            area = newArea;
        }
    }
    indexIter.close();
    IndexedShapefileFeatureStore ds2 = new IndexedShapefileFeatureStore(url.toURI(), false, false, IndexType.NONE, null);
    Envelope newBounds = new JTSEnvelope2D(ds.getEnvelope(new Query(ds2.getNames().iterator().next())));
    double dx = newBounds.getWidth() / 4;
    double dy = newBounds.getHeight() / 4;
    newBounds = new Envelope(newBounds.getMinX() + dx, newBounds.getMaxX() - dx, newBounds.getMinY() + dy, newBounds.getMaxY() - dy);
    CoordinateReferenceSystem crs = FeatureExt.getCRS(ds.getFeatureType());
    performQueryComparison(ds, ds2, new JTSEnvelope2D(newBounds, crs));
    performQueryComparison(ds, ds2, new JTSEnvelope2D(FeatureExt.getEnvelope(smallestFeature)));
    assertTrue(file.exists());
    ds.close();
    ds2.close();
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotoolkit.storage.feature.query.Query) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Envelope(org.locationtech.jts.geom.Envelope) Feature(org.opengis.feature.Feature) URL(java.net.URL) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Geometry(org.locationtech.jts.geom.Geometry) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) File(java.io.File) 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