Search in sources :

Example 26 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class FeatureStoreUtilitiesTest method testGetEnvelope.

@Test
public void testGetEnvelope() throws DataStoreException {
    final GeometryFactory gf = org.geotoolkit.geometry.jts.JTS.getFactory();
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("Candidate");
    ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = ftb.build();
    final Feature f1 = type.newInstance();
    f1.setPropertyValue("geom", gf.createPoint(new Coordinate(10, 20)));
    final Feature f2 = type.newInstance();
    f2.setPropertyValue("geom", gf.createPoint(new Coordinate(-30, -40)));
    final FeatureSet fs = new InMemoryFeatureSet(type, Arrays.asList(f1, f2));
    final Envelope envelope = FeatureStoreUtilities.getEnvelope(fs);
    final GeneralEnvelope expected = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    expected.setRange(0, -30, 10);
    expected.setRange(1, -40, 20);
    Assert.assertEquals(expected, envelope);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 27 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class FeatureStreamsTest method testModifyIterator.

@Test
public void testModifyIterator() {
    FeatureCollection collection = buildSimpleFeatureCollection();
    FeatureType originalType = collection.getType();
    Filter filter = Filter.include();
    Map<String, Object> values = new HashMap<>();
    values.put("att_string", "toto");
    FeatureIterator ite = FeatureStreams.update(collection.iterator(), filter, values);
    assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
    ite = FeatureStreams.update(collection.iterator(), filter, values);
    while (ite.hasNext()) {
        assertTrue(ite.next().getProperty("att_string").getValue().equals("toto"));
    }
    filter = FF.equal(FF.literal("aaa"), FF.property("att_string"));
    ite = FeatureStreams.update(collection.iterator(), filter, values);
    assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
    ite = FeatureStreams.update(collection.iterator(), filter, values);
    while (ite.hasNext()) {
        Feature f = ite.next();
        if (f.getPropertyValue(AttributeConvention.IDENTIFIER).equals(id3)) {
            assertTrue(f.getProperty("att_string").getValue().equals("toto"));
        } else {
            assertFalse(f.getProperty("att_string").getValue().equals("toto"));
        }
    }
    ite = FeatureStreams.update(collection.iterator(), filter, values);
    ite.next();
    ite.next();
    ite.next();
    try {
        ite.next();
        fail("Should have raise a no such element exception.");
    } catch (NoSuchElementException ex) {
    // ok
    }
    // check has next do not iterate
    ite = FeatureStreams.update(collection.iterator(), filter, values);
    testIterationOnNext(ite, 3);
    // check sub iterator is properly closed
    CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(collection.iterator());
    assertFalse(checkIte.isClosed());
    ite = FeatureStreams.update(checkIte, filter, values);
    while (ite.hasNext()) ite.next();
    ite.close();
    assertTrue(checkIte.isClosed());
}
Also used : CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Filter(org.opengis.filter.Filter) HashMap(java.util.HashMap) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature) NoSuchElementException(java.util.NoSuchElementException) CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) Test(org.junit.Test)

Example 28 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class FeatureStreamsTest method buildSimpleFeatureCollection.

private FeatureCollection buildSimpleFeatureCollection() {
    FeatureType originalType = buildOriginalFT();
    FeatureCollection collection = FeatureStoreUtilities.collection("id", originalType);
    Feature sf1 = originalType.newInstance();
    sf1.setPropertyValue("id", 0);
    sf1.setPropertyValue("att_geom", GF.createPoint(new Coordinate(3, 0)));
    sf1.setPropertyValue("att_string", "bbb");
    sf1.setPropertyValue("att_double", 3d);
    collection.add(sf1);
    Feature sf2 = originalType.newInstance();
    sf2.setPropertyValue("id", 1);
    sf2.setPropertyValue("att_geom", GF.createPoint(new Coordinate(1, 0)));
    sf2.setPropertyValue("att_string", "ccc");
    sf2.setPropertyValue("att_double", 1d);
    collection.add(sf2);
    Feature sf3 = originalType.newInstance();
    sf3.setPropertyValue("id", 2);
    sf3.setPropertyValue("att_geom", GF.createPoint(new Coordinate(2, 0)));
    sf3.setPropertyValue("att_string", "aaa");
    sf3.setPropertyValue("att_double", 2d);
    collection.add(sf3);
    return collection;
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Coordinate(org.locationtech.jts.geom.Coordinate) Feature(org.opengis.feature.Feature)

Example 29 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class FeatureStreamsTest method buildComplexFeatureCollection.

private FeatureCollection buildComplexFeatureCollection() {
    FeatureTypeBuilder builder = new FeatureTypeBuilder();
    builder.setName(NAME);
    builder.addAttribute(Integer.class).setName("id").addRole(AttributeRole.IDENTIFIER_COMPONENT);
    builder.addAttribute(String.class).setName("att_string");
    builder.addAttribute(Double.class).setName("att_double");
    FeatureType ct = builder.build();
    FeatureCollection collectionComplex = FeatureStoreUtilities.collection("cid", ct);
    Feature f1 = ct.newInstance();
    f1.setPropertyValue("id", COMPLEX_ID_1);
    f1.setPropertyValue("att_string", "aaaa");
    f1.setPropertyValue("att_double", 12.0);
    collectionComplex.add(f1);
    Feature f2 = ct.newInstance();
    f2.setPropertyValue("id", COMPLEX_ID_2);
    f2.setPropertyValue("att_string", "bbbb");
    f2.setPropertyValue("att_double", 7.0);
    collectionComplex.add(f2);
    return collectionComplex;
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature)

Example 30 with FeatureType

use of org.opengis.feature.FeatureType 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)

Aggregations

FeatureType (org.opengis.feature.FeatureType)303 Feature (org.opengis.feature.Feature)146 Test (org.junit.Test)122 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)100 GenericName (org.opengis.util.GenericName)70 ArrayList (java.util.ArrayList)65 DataStoreException (org.apache.sis.storage.DataStoreException)56 PropertyType (org.opengis.feature.PropertyType)51 Coordinate (org.locationtech.jts.geom.Coordinate)50 Point (org.locationtech.jts.geom.Point)41 FeatureSet (org.apache.sis.storage.FeatureSet)37 Query (org.geotoolkit.storage.feature.query.Query)36 AttributeType (org.opengis.feature.AttributeType)36 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)33 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)32 WritableFeatureSet (org.apache.sis.storage.WritableFeatureSet)31 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)27 Geometry (org.locationtech.jts.geom.Geometry)27 URL (java.net.URL)26 HashMap (java.util.HashMap)26