Search in sources :

Example 6 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class PostgresSimpleTypeTest method testUglyTableName.

/**
 * Test ugly named table
 */
@Test
public void testUglyTableName() throws Exception {
    reload(true);
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test'te#st$'test\"'test");
    ftb.addAttribute(String.class).setName("text");
    final FeatureType ft = ftb.build();
    store.createFeatureType(ft);
    final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature record = resType.newInstance();
    record.setPropertyValue("text", "un'deux'trois'quatre");
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(record));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("1"), addedIds.get(0));
    final Query query = new Query(resType.getName());
    final FeatureReader ite = store.getFeatureReader(query);
    boolean found = false;
    try {
        while (ite.hasNext()) {
            final Feature feature = ite.next();
            Object val = feature.getPropertyValue("text");
            assertEquals("un'deux'trois'quatre", val);
            found = true;
        }
    } finally {
        ite.close();
    }
    assertTrue(found);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) ResourceId(org.opengis.filter.ResourceId) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 7 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class PostgresSimpleTypeTest method testArrayInsert.

@Test
public void testArrayInsert() throws DataStoreException, VersioningException {
    reload(true);
    store.createFeatureType(FTYPE_ARRAY);
    final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature feature = resType.newInstance();
    feature.setPropertyValue("fid", 0);
    feature.setPropertyValue("boolean", new Boolean[] { true, false, true });
    feature.setPropertyValue("byte", new Short[] { 3, 6, 9 });
    feature.setPropertyValue("short", new Short[] { -5, 12, -50 });
    feature.setPropertyValue("integer", new Integer[] { 123, 456, 789 });
    feature.setPropertyValue("long", new Long[] { 111l, 222l, 333l });
    feature.setPropertyValue("float", new Float[] { 1.2f, -5.9f, 8.1f });
    feature.setPropertyValue("double", new Double[] { 78.3d, 41.23d, -99.66d });
    feature.setPropertyValue("string", new String[] { "marc", "hubert", "samy" });
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("1"), addedIds.get(0));
    final Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
    assertEquals(1, col.size());
    // Postgis allow NULL in arrays, so returned array are not primitive types
    final FeatureIterator ite = col.iterator();
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertArrayEquals(new Boolean[] { true, false, true }, (Boolean[]) resFeature.getPropertyValue("boolean"));
        assertArrayEquals(new Short[] { 3, 6, 9 }, (Short[]) resFeature.getPropertyValue("byte"));
        assertArrayEquals(new Short[] { -5, 12, -50 }, (Short[]) resFeature.getPropertyValue("short"));
        assertArrayEquals(new Integer[] { 123, 456, 789 }, (Integer[]) resFeature.getPropertyValue("integer"));
        assertArrayEquals(new Long[] { 111l, 222l, 333l }, (Long[]) resFeature.getPropertyValue("long"));
        assertArrayEquals(new Float[] { 1.2f, -5.9f, 8.1f }, (Float[]) resFeature.getPropertyValue("float"));
        assertArrayEquals(new Double[] { 78.3d, 41.23d, -99.66d }, (Double[]) resFeature.getPropertyValue("double"));
        assertArrayEquals(new String[] { "marc", "hubert", "samy" }, (String[]) resFeature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) ResourceId(org.opengis.filter.ResourceId) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 8 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class PostgresSimpleTypeTest method testGeometryInsert.

@Test
public void testGeometryInsert() throws DataStoreException, NoSuchAuthorityCodeException, FactoryException, VersioningException {
    reload(true);
    // //////////////////////////////////////////////////////////////////////
    final GeometryFactory gf = JTS.getFactory();
    // creating a point -----------------------------------------------
    final Point point = gf.createPoint(new Coordinate(56, 45));
    // creating a multipoint ------------------------------------------
    final MultiPoint mp = gf.createMultiPoint(new Coordinate[] { new Coordinate(23, 78), new Coordinate(-10, 43), new Coordinate(12, 94) });
    // creating a linestring ------------------------------------------
    final LineString ls = gf.createLineString(new Coordinate[] { new Coordinate(23, 78), new Coordinate(-10, 43), new Coordinate(12, 94) });
    // creating a multilinestring -------------------------------------
    final LineString ls1 = gf.createLineString(new Coordinate[] { new Coordinate(30, 45), new Coordinate(56, 29) });
    final LineString ls2 = gf.createLineString(new Coordinate[] { new Coordinate(98, 12), new Coordinate(19, 87) });
    final MultiLineString mls = gf.createMultiLineString(new LineString[] { ls1, ls2 });
    // creating a polygon ---------------------------------------------
    final LinearRing ring = gf.createLinearRing(new Coordinate[] { new Coordinate(23, 78), new Coordinate(-10, 43), new Coordinate(12, 94), new Coordinate(23, 78) });
    final Polygon polygon = gf.createPolygon(ring, new LinearRing[0]);
    // creating a multipolygon ----------------------------------------
    final MultiPolygon mpolygon = gf.createMultiPolygon(new Polygon[] { polygon });
    // creating a geometry collection ----------------------------------------
    final GeometryCollection gc = gf.createGeometryCollection(new Geometry[] { point, ls, polygon });
    // //////////////////////////////////////////////////////////////////////
    store.createFeatureType(FTYPE_GEOMETRY);
    final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature feature = resType.newInstance();
    feature.setPropertyValue("fid", 0);
    feature.setPropertyValue("geometry", point);
    feature.setPropertyValue("point", point);
    feature.setPropertyValue("multipoint", mp);
    feature.setPropertyValue("linestring", ls);
    feature.setPropertyValue("multilinestring", mls);
    feature.setPropertyValue("polygon", polygon);
    feature.setPropertyValue("multipolygon", mpolygon);
    feature.setPropertyValue("geometrycollection", gc);
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("1"), addedIds.get(0));
    final Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
    assertEquals(1, col.size());
    // Postgis allow NULL in arrays, so returned array are not primitive types
    final FeatureIterator ite = col.iterator();
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        Geometry geom;
        geom = (Geometry) resFeature.getPropertyValue("geometry");
        assertEquals(point, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
        geom = (Geometry) resFeature.getPropertyValue("point");
        assertEquals(point, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
        geom = (Geometry) resFeature.getPropertyValue("multipoint");
        assertEquals(mp, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
        geom = (Geometry) resFeature.getPropertyValue("linestring");
        assertEquals(ls, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
        geom = (Geometry) resFeature.getPropertyValue("multilinestring");
        assertEquals(mls, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
        geom = (Geometry) resFeature.getPropertyValue("polygon");
        assertEquals(polygon, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
        geom = (Geometry) resFeature.getPropertyValue("multipolygon");
        assertEquals(mpolygon, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
        geom = (Geometry) resFeature.getPropertyValue("geometrycollection");
        assertEquals(gc, geom);
        assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
    } finally {
        ite.close();
    }
}
Also used : MultiPoint(org.locationtech.jts.geom.MultiPoint) MultiLineString(org.locationtech.jts.geom.MultiLineString) FeatureType(org.opengis.feature.FeatureType) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotoolkit.storage.feature.query.Query) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) Feature(org.opengis.feature.Feature) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) ResourceId(org.opengis.filter.ResourceId) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 9 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class PostgresSimpleTypeTest method testArray2Insert.

@Test
public void testArray2Insert() throws DataStoreException, VersioningException {
    reload(true);
    store.createFeatureType(FTYPE_ARRAY2);
    final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature feature = resType.newInstance();
    feature.setPropertyValue("fid", 0);
    feature.setPropertyValue("boolean", new Boolean[][] { { true, false, true }, { false, true, false }, { false, false, false } });
    feature.setPropertyValue("byte", new Short[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
    feature.setPropertyValue("short", new Short[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
    feature.setPropertyValue("integer", new Integer[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
    feature.setPropertyValue("long", new Long[][] { { 1l, 2l, 3l }, { 4l, 5l, 6l }, { 7l, 8l, 9l } });
    feature.setPropertyValue("float", new Float[][] { { 1f, 2f, 3f }, { 4f, 5f, 6f }, { 7f, 8f, 9f } });
    feature.setPropertyValue("double", new Double[][] { { 1d, 2d, 3d }, { 4d, 5d, 6d }, { 7d, 8d, 9d } });
    feature.setPropertyValue("string", new String[][] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } });
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("1"), addedIds.get(0));
    final Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
    assertEquals(1, col.size());
    // Postgis allow NULL in arrays, so returned array are not primitive types
    final FeatureIterator ite = col.iterator();
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertArrayEquals((Boolean[][]) feature.getPropertyValue("boolean"), (Boolean[][]) resFeature.getPropertyValue("boolean"));
        assertArrayEquals((Short[][]) feature.getPropertyValue("byte"), (Short[][]) resFeature.getPropertyValue("byte"));
        assertArrayEquals((Short[][]) feature.getPropertyValue("short"), (Short[][]) resFeature.getPropertyValue("short"));
        assertArrayEquals((Integer[][]) feature.getPropertyValue("integer"), (Integer[][]) resFeature.getPropertyValue("integer"));
        assertArrayEquals((Long[][]) feature.getPropertyValue("long"), (Long[][]) resFeature.getPropertyValue("long"));
        assertArrayEquals((Float[][]) feature.getPropertyValue("float"), (Float[][]) resFeature.getPropertyValue("float"));
        assertArrayEquals((Double[][]) feature.getPropertyValue("double"), (Double[][]) resFeature.getPropertyValue("double"));
        assertArrayEquals((String[][]) feature.getPropertyValue("string"), (String[][]) resFeature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) 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 10 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testTrimVersioning.

@Test
public void testTrimVersioning() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Feature feature;
    ResourceId fid;
    Version v0;
    Version v1;
    Version v2;
    FeatureIterator ite;
    Query qb = new Query();
    // create table
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    // get version control
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    assertNotNull(vc);
    assertTrue(vc.isEditable());
    assertFalse(vc.isVersioned());
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    assertTrue(vc.isVersioned());
    versions = vc.list();
    assertTrue(versions.isEmpty());
    // 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));
    // we should have one version
    versions = vc.list();
    assertEquals(1, versions.size());
    // get identifier
    // ensure normal reading is correct without version----------------------
    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());
    }
    // make an update ///////////////////////////////////////////////////////
    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);
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // make a 2nd update ///////////////////////////////////////////////////////
    final Point thirdPoint = GF.createPoint(new Coordinate(145, -221));
    final Map<String, Object> updates2 = new HashMap<>();
    updates2.put("boolean", Boolean.FALSE);
    updates2.put("integer", 150);
    updates2.put("point", thirdPoint);
    updates2.put("string", "secondtextupdated");
    store.updateFeatures(refType.getName().toString(), fid, updates2);
    // get all versions organized in increase dates order.
    versions = vc.list();
    assertEquals(3, versions.size());
    v0 = versions.get(0);
    v1 = versions.get(1);
    v2 = versions.get(2);
    /* first trim between v1 date and v2 date (named middle date) to verify
         * deletion of first version and update v1 date at trim date.*/
    final Date middle = new Date((v1.getDate().getTime() + v2.getDate().getTime()) >> 1);
    vc.trim(middle);
    versions = vc.list();
    assertEquals(2, versions.size());
    // ensure version 0 does not exist
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v0.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure version v1 begin at middle date.
    assertEquals(vc.list().get(0).getDate().getTime(), middle.getTime());
    /* second trim at exactely the begining of the third version to verify,
         * deletion of second version and third version existence.*/
    vc.trim(v2);
    versions = vc.list();
    assertEquals(1, versions.size());
    // ensure version 1 does not exist
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure version v2 begin time doesn't change.
    assertEquals(vc.list().get(0).getDate().getTime(), v2.getDate().getTime());
    /* third trim just after v3 version date, to verify that v3
         * version date become trim date */
    final long lastDate = v2.getDate().getTime() + 400;
    vc.trim(new Date(lastDate));
    versions = vc.list();
    assertEquals(1, versions.size());
    // ensure version v2 begin time become lastDate.
    assertEquals(vc.list().get(0).getDate().getTime(), lastDate);
}
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) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) Test(org.junit.Test)

Aggregations

ResourceId (org.opengis.filter.ResourceId)45 Feature (org.opengis.feature.Feature)26 Test (org.junit.Test)23 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)20 FeatureType (org.opengis.feature.FeatureType)20 Query (org.geotoolkit.storage.feature.query.Query)18 HashMap (java.util.HashMap)11 Coordinate (org.locationtech.jts.geom.Coordinate)11 Point (org.locationtech.jts.geom.Point)10 Filter (org.opengis.filter.Filter)10 DataStoreException (org.apache.sis.storage.DataStoreException)9 Session (org.geotoolkit.storage.feature.session.Session)9 Geometry (org.locationtech.jts.geom.Geometry)9 Date (java.util.Date)8 PropertyType (org.opengis.feature.PropertyType)8 ArrayList (java.util.ArrayList)7 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)7 Version (org.geotoolkit.version.Version)6 VersionControl (org.geotoolkit.version.VersionControl)6 IOException (java.io.IOException)5