Search in sources :

Example 66 with Query

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

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

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

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

the class IndexedShapefileDataStoreTest method testRemoveFromFrontAndClose.

/**
 * Create a test file, then continue removing the first entry until there
 * are no features left.
 */
@Test
public void testRemoveFromFrontAndClose() throws Throwable {
    IndexedShapefileFeatureStore sds = createDataStore();
    int idx = loadFeatures(sds).size();
    while (idx > 0) {
        FeatureWriter writer = null;
        try {
            writer = sds.getFeatureWriter(new Query(sds.getName()));
            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 70 with Query

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

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