Search in sources :

Example 61 with Query

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

the class PostgresComplexTypeTest method testComplex2Insert.

/**
 * 3 level depths feature test.
 */
@Test
public void testComplex2Insert() throws DataStoreException, VersioningException {
    reload(false);
    store.createFeatureType(FTYPE_COMPLEX2);
    final FeatureType soundingType = store.getFeatureType(store.getNames().iterator().next().toString());
    final FeatureType recordType = ((FeatureAssociationRole) soundingType.getProperty("records")).getValueType();
    final FeatureType dataType = ((FeatureAssociationRole) recordType.getProperty("datas")).getValueType();
    final Feature sounding = soundingType.newInstance();
    sounding.setPropertyValue("identifier", 120l);
    final Feature record1 = recordType.newInstance();
    record1.setPropertyValue("time", new Date(5000000));
    final Feature data11 = dataType.newInstance();
    data11.setPropertyValue("values", new Float[] { 1f, 2f, 3f });
    final Feature data12 = dataType.newInstance();
    data12.setPropertyValue("values", new Float[] { 4f, 5f, 6f });
    record1.setPropertyValue("datas", Arrays.asList(data11, data12));
    final Feature record2 = recordType.newInstance();
    record2.setPropertyValue("time", new Date(6000000));
    final Feature data21 = dataType.newInstance();
    data21.setPropertyValue("values", new Float[] { 7f, 8f, 9f });
    record2.setPropertyValue("datas", Arrays.asList(data21));
    sounding.setPropertyValue("records", Arrays.asList(record1, record2));
    List<ResourceId> addedIds = store.addFeatures(soundingType.getName().toString(), Collections.singleton(sounding));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("Sounding.1"), addedIds.get(0));
    final Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(soundingType.getName()));
    assertEquals(1, col.size());
    final FeatureIterator ite = store.getFeatureReader(new Query(soundingType.getName()));
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertEquals(120l, resFeature.getPropertyValue("identifier"));
        final Collection<Feature> records = (Collection<Feature>) resFeature.getPropertyValue("records");
        assertEquals(2, records.size());
        final boolean[] found = new boolean[2];
        for (Feature record : records) {
            final Timestamp time = (Timestamp) record.getPropertyValue("time");
            if (time.getTime() == 5000000) {
                found[0] = true;
                final Collection<Feature> datas = (Collection<Feature>) record.getPropertyValue("datas");
                assertEquals(2, datas.size());
                final boolean[] dfound = new boolean[2];
                for (Feature data : datas) {
                    final Float[] values = (Float[]) data.getPropertyValue("values");
                    if (Arrays.equals(values, new Float[] { 1f, 2f, 3f })) {
                        dfound[0] = true;
                    } else if (Arrays.equals(values, new Float[] { 4f, 5f, 6f })) {
                        dfound[1] = true;
                    } else {
                        fail("Unexpected property \n" + data);
                    }
                }
                for (boolean b : dfound) assertTrue(b);
            } else if (time.getTime() == 6000000) {
                found[1] = true;
                final Collection<Feature> datas = (Collection<Feature>) record.getPropertyValue("datas");
                assertEquals(1, datas.size());
                final boolean[] dfound = new boolean[1];
                for (Feature data : datas) {
                    final Float[] values = (Float[]) data.getPropertyValue("values");
                    if (Arrays.equals(values, new Float[] { 7f, 8f, 9f })) {
                        dfound[0] = true;
                    } else {
                        fail("Unexpected property \n" + data);
                    }
                }
                for (boolean b : dfound) assertTrue(b);
            } else {
                fail("Unexpected property \n" + record);
            }
        }
        for (boolean b : found) assertTrue(b);
    } finally {
        ite.close();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) Feature(org.opengis.feature.Feature) Timestamp(java.sql.Timestamp) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) ResourceId(org.opengis.filter.ResourceId) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Collection(java.util.Collection) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 62 with Query

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

the class PostgresComplexTypeTest method testComplex3Insert.

/**
 * multiple complex properties of same type
 * @throws org.apache.sis.storage.DataStoreException
 * @throws org.geotoolkit.version.VersioningException
 */
@Test
public void testComplex3Insert() throws DataStoreException, VersioningException {
    reload(false);
    store.createFeatureType(FTYPE_COMPLEX3);
    final FeatureType recordType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature record = FTYPE_RECORD.newInstance();
    record.setPropertyValue("identifier", 120);
    final Feature data1 = FTYPE_DATA.newInstance();
    data1.setPropertyValue("value", 5f);
    final Feature data2 = FTYPE_DATA.newInstance();
    data2.setPropertyValue("value", 10f);
    final Feature data3 = FTYPE_DATA.newInstance();
    data3.setPropertyValue("value", 15f);
    record.setPropertyValue("datas", Arrays.asList(data1, data2, data3));
    List<ResourceId> addedIds = store.addFeatures(recordType.getName().toString(), Collections.singleton(record));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("Record.1"), addedIds.get(0));
    final Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(recordType.getName()));
    assertEquals(1, col.size());
    final FeatureIterator ite = store.getFeatureReader(new Query(recordType.getName()));
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertEquals(120l, resFeature.getPropertyValue("identifier"));
        assertNotNull(resFeature.getProperty("data1"));
        assertNotNull(resFeature.getProperty("data2"));
        assertNotNull(resFeature.getProperty("data3"));
        assertEquals(5f, ((Feature) resFeature.getPropertyValue("data1")).getPropertyValue("value"));
        assertEquals(10f, ((Feature) resFeature.getPropertyValue("data2")).getPropertyValue("value"));
        assertEquals(15f, ((Feature) resFeature.getPropertyValue("data3")).getPropertyValue("value"));
    } finally {
        ite.close();
    }
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) ResourceId(org.opengis.filter.ResourceId) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Feature(org.opengis.feature.Feature) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 63 with Query

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

the class PostgresSpatialQueryTest method noGeomEnvelopeQuery.

/**
 * Test reading envelope on a table with no geometry field.
 */
@Test
public void noGeomEnvelopeQuery() throws DataStoreException, VersioningException {
    reload(true);
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("nogeomtable");
    ftb.addAttribute(String.class).setName("field");
    FeatureType ft = ftb.build();
    store.createFeatureType(ft);
    // test env reading all fields
    Envelope env = store.getEnvelope(new Query(ft.getName()));
    assertNull(env);
    // test env reading no fields
    final Query qb = new Query(ft.getName().toString());
    qb.setProperties(new String[0]);
    env = store.getEnvelope(qb);
    assertNull(env);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) Envelope(org.opengis.geometry.Envelope) Test(org.junit.Test)

Example 64 with Query

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

the class PostgresSpatialQueryTest method noGeomNoIdEnvelopeQuery.

/**
 * Test reading envelope on a table with no geometry or id field.
 */
@Test
public void noGeomNoIdEnvelopeQuery() throws DataStoreException, VersioningException, SQLException {
    reload(true);
    final Connection cnx = store.getDataSource().getConnection();
    cnx.createStatement().executeUpdate("CREATE TABLE \"noGeomNoIdTable\" (field VARCHAR(255));");
    store.refreshMetaModel();
    final FeatureType ft = store.getFeatureType("noGeomNoIdTable");
    // test env reading all fields
    Envelope env = store.getEnvelope(new Query(ft.getName()));
    assertNull(env);
    // test env reading no fields
    final Query qb = new Query(ft.getName().toString());
    qb.setProperties(new String[0]);
    env = store.getEnvelope(qb);
    assertNull(env);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) Connection(java.sql.Connection) Envelope(org.opengis.geometry.Envelope) Test(org.junit.Test)

Example 65 with Query

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

the class PostgresVersioningTest method testVersioningSynchrone.

/**
 * Check versions are created on each call on the session.
 */
@Test
public void testVersioningSynchrone() 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(false);
    // //////////////////////////////////////////////////////////////////////
    // 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 one 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 an update 1 /////////////////////////////////////////////////////
    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 two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // //////////////////////////////////////////////////////////////////////
    // make an update 2 /////////////////////////////////////////////////////
    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 three versions
    versions = vc.list();
    assertEquals(3, 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 four versions
    versions = vc.list();
    assertEquals(4, versions.size());
    // //////////////////////////////////////////////////////////////////////
    // make an insert ///////////////////////////////////////////////////////
    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 five versions
    versions = vc.list();
    assertEquals(5, versions.size());
}
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)

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