Search in sources :

Example 1 with FeatureReader

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

the class FeatureStreamsTest method testEmptyReader.

@Test
public void testEmptyReader() {
    FeatureCollection collection = buildSimpleFeatureCollection();
    final FeatureReader iterator = FeatureStreams.emptyReader(collection.getType());
    assertEquals(iterator.getFeatureType(), collection.getType());
    assertFalse(iterator.hasNext());
    try {
        iterator.next();
        fail("Next on empty iterator should have raised a no such element exception.");
    } catch (NoSuchElementException ex) {
    // ok
    }
    try {
        iterator.remove();
        fail("Remove should have raise an error.");
    } catch (Exception ex) {
    // ok
    }
    iterator.close();
}
Also used : FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) NoSuchElementException(java.util.NoSuchElementException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) DataStoreException(org.apache.sis.storage.DataStoreException) NoSuchElementException(java.util.NoSuchElementException) FactoryException(org.opengis.util.FactoryException) Test(org.junit.Test)

Example 2 with FeatureReader

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

the class FeatureStreamsTest method testReprojectFeatureIterator.

@Test
public void testReprojectFeatureIterator() throws DataStoreException, FactoryException {
    Query query = new Query(collection.getType().getName());
    FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    final CoordinateReferenceSystem targetCRS = CommonCRS.WGS84.geographic();
    FeatureReader retyped = FeatureStreams.decorate(reader, new ReprojectMapper(reader.getFeatureType(), targetCRS), new Hints());
    int mask = 0;
    Feature f;
    while (retyped.hasNext()) {
        f = retyped.next();
        final Object id = f.getPropertyValue(AttributeConvention.IDENTIFIER);
        assertEquals(4, f.getType().getProperties(true).size());
        assertEquals(targetCRS, JTS.findCoordinateReferenceSystem((Geometry) f.getProperty("att_geom").getValue()));
        if (id1.equals(id)) {
            mask |= 1 << 0;
            assertEquals(GF.createPoint(new Coordinate(0, 3)).toString(), f.getProperty("att_geom").getValue().toString());
        } else if (id2.equals(id)) {
            mask |= 1 << 1;
            assertEquals(GF.createPoint(new Coordinate(0, 1)).toString(), f.getProperty("att_geom").getValue().toString());
        } else if (id3.equals(id)) {
            mask |= 1 << 2;
            assertEquals(GF.createPoint(new Coordinate(0, 2)).toString(), f.getProperty("att_geom").getValue().toString());
        }
    }
    if (mask != 7) {
        fail("missing features in iterations");
    }
    // check has next do not iterate
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    retyped = FeatureStreams.decorate(reader, new ReprojectMapper(reader.getFeatureType(), CommonCRS.WGS84.geographic()), new Hints());
    testIterationOnNext(retyped, 3);
    // check sub iterator is properly closed
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(reader);
    assertFalse(checkIte.isClosed());
    retyped = FeatureStreams.decorate(checkIte, new ReprojectMapper(checkIte.getFeatureType(), CommonCRS.WGS84.geographic()), new Hints());
    while (retyped.hasNext()) retyped.next();
    retyped.close();
    assertTrue(checkIte.isClosed());
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Query(org.geotoolkit.storage.feature.query.Query) Hints(org.geotoolkit.factory.Hints) Coordinate(org.locationtech.jts.geom.Coordinate) ReprojectMapper(org.geotoolkit.feature.ReprojectMapper) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) Point(org.locationtech.jts.geom.Point) CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) Test(org.junit.Test)

Example 3 with FeatureReader

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

the class FeatureStreamsTest method testWrapReader.

@Test
public void testWrapReader() {
    FeatureCollection collection = buildSimpleFeatureCollection();
    // check has next do not iterate
    FeatureReader reader = FeatureStreams.asReader(collection.iterator(), collection.getType());
    testIterationOnNext(reader, 3);
    // check sub iterator is properly closed
    CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(collection.iterator());
    assertFalse(checkIte.isClosed());
    reader = FeatureStreams.asReader(checkIte, collection.getType());
    while (reader.hasNext()) reader.next();
    reader.close();
    assertTrue(checkIte.isClosed());
}
Also used : FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) Test(org.junit.Test)

Example 4 with FeatureReader

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

the class FeatureStreamsTest method testTransformFeatureIterator.

@Test
public void testTransformFeatureIterator() throws DataStoreException {
    FeatureType originalType = buildOriginalFT();
    final FeatureTypeBuilder builder = new FeatureTypeBuilder();
    final GenericName name = NamesExt.create("http://test.com", "TestSchema");
    builder.setName(name);
    builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY);
    builder.addAttribute(LineString.class).setName("att_geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = builder.build();
    final LineString geom = GF.createLineString(new Coordinate[] { new Coordinate(0, 0), // dx 15 , dy 12
    new Coordinate(15, 12), // dx 7 , dy 16
    new Coordinate(8, 28), // dx 1 , dy 3
    new Coordinate(9, 31), // dx 14 , dy 20
    new Coordinate(-5, 11), // dx 4 , dy 2
    new Coordinate(-1, 9) });
    final FeatureCollection collection = FeatureStoreUtilities.collection("id", type);
    Feature sf = type.newInstance();
    sf.setPropertyValue("att_geom", geom);
    collection.add(sf);
    // get the reader -------------------------------------------------------
    Query query = new Query(originalType.getName());
    FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    // create the decimate reader -------------------------------------------
    final Hints hints = new Hints();
    GeometryTransformer decim = new GeometryScaleTransformer(10, 10);
    final TransformMapper ttype = new TransformMapper(reader.getFeatureType(), decim);
    FeatureReader retyped = FeatureStreams.decorate(reader, ttype, hints);
    assertTrue(retyped.hasNext());
    LineString decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertFalse(retyped.hasNext());
    retyped.close();
    assertEquals(4, decimated.getNumPoints());
    assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
    assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
    assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
    assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
    // check the original geometry has not been modified
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    assertTrue(reader.hasNext());
    LineString notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertEquals(6, notDecimated.getNumPoints());
    assertFalse(reader.hasNext());
    reader.close();
    // same test but with reuse hint ---------------------------------------
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    decim = new GeometryScaleTransformer(10, 10);
    retyped = FeatureStreams.decorate(reader, new TransformMapper(reader.getFeatureType(), decim), hints);
    assertTrue(retyped.hasNext());
    decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertFalse(retyped.hasNext());
    retyped.close();
    assertEquals(4, decimated.getNumPoints());
    assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
    assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
    assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
    assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
    // check the original geometry has not been modified
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    assertTrue(reader.hasNext());
    notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertEquals(6, notDecimated.getNumPoints());
    assertFalse(reader.hasNext());
    reader.close();
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) Query(org.geotoolkit.storage.feature.query.Query) Hints(org.geotoolkit.factory.Hints) GeometryTransformer(org.geotoolkit.geometry.jts.transform.GeometryTransformer) LineString(org.locationtech.jts.geom.LineString) TransformMapper(org.geotoolkit.feature.TransformMapper) Feature(org.opengis.feature.Feature) GeometryScaleTransformer(org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer) GenericName(org.opengis.util.GenericName) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Test(org.junit.Test)

Example 5 with FeatureReader

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

the class PostgresComplexTypeTest method testHandMadeSQLQuery.

/**
 * Test hand made query.
 */
@Test
public void testHandMadeSQLQuery() throws Exception {
    reload(false);
    final GeometryFactory gf = JTS.getFactory();
    store.createFeatureType(FTYPE_COMPLEX);
    final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature voyage = resType.newInstance();
    voyage.setPropertyValue("identifier", 120l);
    final Feature driver = FTYPE_DRIVER.newInstance();
    driver.setPropertyValue("name", "jean-michel");
    driver.setPropertyValue("code", "BHF:123456");
    voyage.setPropertyValue("driver", driver);
    final Feature stop1 = FTYPE_STOP.newInstance();
    stop1.setPropertyValue("location", gf.createPoint(new Coordinate(-10, 60)));
    stop1.setPropertyValue("time", new Date(5000000));
    final Feature stop2 = FTYPE_STOP.newInstance();
    stop2.setPropertyValue("location", gf.createPoint(new Coordinate(30, 15)));
    stop2.setPropertyValue("time", new Date(6000000));
    final Feature stop3 = FTYPE_STOP.newInstance();
    stop3.setPropertyValue("location", gf.createPoint(new Coordinate(40, -70)));
    stop3.setPropertyValue("time", new Date(7000000));
    voyage.setPropertyValue("stops", Arrays.asList(stop1, stop2, stop3));
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(voyage));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("Voyage.1"), addedIds.get(0));
    final org.apache.sis.storage.Query query = new SQLQuery("SELECT * FROM \"Stop\"", "s1");
    final FeatureReader ite = store.getFeatureReader(query);
    final boolean[] found = new boolean[3];
    try {
        while (ite.hasNext()) {
            final Feature feature = ite.next();
            final Timestamp time = (Timestamp) feature.getPropertyValue("time");
            final Point location = (Point) feature.getPropertyValue("location");
            if (time.getTime() == 5000000) {
                assertEquals(stop1.getPropertyValue("location"), location);
                found[0] = true;
            } else if (time.getTime() == 6000000) {
                assertEquals(stop2.getPropertyValue("location"), location);
                found[1] = true;
            } else if (time.getTime() == 7000000) {
                assertEquals(stop3.getPropertyValue("location"), location);
                found[2] = true;
            } else {
                fail("Unexpected property \n" + feature);
            }
            final Optional<Geometry> geom = FeatureExt.getDefaultGeometryValue(feature).filter(Geometry.class::isInstance).map(Geometry.class::cast);
            Assert.assertTrue(geom.isPresent());
            assertNotNull(JTS.findCoordinateReferenceSystem(geom.get()));
        }
    } finally {
        ite.close();
    }
    for (boolean b : found) assertTrue(b);
}
Also used : FeatureType(org.opengis.feature.FeatureType) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) Feature(org.opengis.feature.Feature) Timestamp(java.sql.Timestamp) Date(java.util.Date) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) ResourceId(org.opengis.filter.ResourceId) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Test(org.junit.Test)

Aggregations

FeatureReader (org.geotoolkit.storage.feature.FeatureReader)24 Test (org.junit.Test)11 FeatureType (org.opengis.feature.FeatureType)11 DataStoreException (org.apache.sis.storage.DataStoreException)10 Query (org.apache.sis.storage.Query)10 Feature (org.opengis.feature.Feature)9 Query (org.geotoolkit.storage.feature.query.Query)8 IOException (java.io.IOException)7 UnsupportedQueryException (org.apache.sis.storage.UnsupportedQueryException)7 Filter (org.opengis.filter.Filter)6 URL (java.net.URL)5 Hints (org.geotoolkit.factory.Hints)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 Point (org.locationtech.jts.geom.Point)5 ResourceId (org.opengis.filter.ResourceId)5 GenericName (org.opengis.util.GenericName)5 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)4 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)4 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3