Search in sources :

Example 11 with FeatureCollection

use of org.geotoolkit.storage.feature.FeatureCollection 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 12 with FeatureCollection

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

the class ReferenceToFeatureCollectionConverterTest method testJSONConversion.

@Test
public void testJSONConversion() throws IOException, DataStoreException {
    final FeatureCollection featureCollection = ConvertersTestUtils.initAndRunInputConversion(Reference.class, FeatureCollection.class, "/inputs/featurecollection.json", WPSMimeType.APP_GEOJSON.val(), WPSEncoding.UTF8.getValue(), null);
    // Test the feature collection
    ConvertersTestUtils.assertFeatureCollectionIsValid(featureCollection);
}
Also used : FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Test(org.junit.Test)

Example 13 with FeatureCollection

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

the class FeatureCollectionToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final FeatureCollection source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (params.get(TMP_DIR_PATH) == null) {
        throw new UnconvertibleObjectException("The output directory should be defined.");
    }
    if (params.get(TMP_DIR_URL) == null) {
        throw new UnconvertibleObjectException("The output directory URL should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    // TODO : useless test, null test above is all we need, fix this and other converters
    if (!(source instanceof FeatureCollection)) {
        throw new UnconvertibleObjectException("The requested output data is not an instance of FeatureCollection.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    final FeatureType ft = source.getType();
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    final String randomFileName = UUID.randomUUID().toString();
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
        // create file
        final Path dataFile = buildPath(params, randomFileName + ".json");
        try (OutputStream fos = Files.newOutputStream(dataFile, CREATE, TRUNCATE_EXISTING, WRITE);
            GeoJSONStreamWriter writer = new GeoJSONStreamWriter(fos, ft, WPSConvertersUtils.FRACTION_DIGITS);
            Stream<Feature> st = source.features(false)) {
            Iterator<Feature> iterator = st.iterator();
            while (iterator.hasNext()) {
                Feature next = iterator.next();
                Feature neww = writer.next();
                FeatureExt.copy(next, neww, false);
                writer.write();
            }
        } catch (DataStoreException e) {
            throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
        } catch (IOException e) {
            throw new UnconvertibleObjectException(e);
        }
        final String relLoc = getRelativeLocation(dataFile, params);
        reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
        reference.setSchema(null);
    } else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType())) {
        try {
            reference.setSchema(WPSConvertersUtils.writeSchema(ft, params));
            schemaLocation.put(namespace, reference.getSchema());
        } catch (JAXBException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
        }
        // Write Feature
        final JAXPStreamFeatureWriter featureWriter = new JAXPStreamFeatureWriter(schemaLocation);
        // create file
        final Path dataFile = buildPath(params, randomFileName + ".xml");
        try (final OutputStream dataStream = Files.newOutputStream(dataFile);
            final AutoCloseable xmlCloser = () -> featureWriter.dispose()) {
            // Write feature in file
            featureWriter.write(source, dataStream);
            final String relLoc = getRelativeLocation(dataFile, params);
            reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
        } catch (XMLStreamException ex) {
            throw new UnconvertibleObjectException("Stax exception while writing the feature collection", ex);
        } catch (DataStoreException ex) {
            throw new UnconvertibleObjectException("FeatureStore exception while writing the feature collection", ex);
        } catch (FeatureStoreRuntimeException ex) {
            throw new UnconvertibleObjectException("FeatureStoreRuntimeException exception while writing the feature collection", ex);
        } catch (Exception ex) {
            throw new UnconvertibleObjectException(ex);
        }
    } else {
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
    }
    return reference;
}
Also used : Path(java.nio.file.Path) FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) HashMap(java.util.HashMap) Reference(org.geotoolkit.wps.xml.v200.Reference) JAXBException(javax.xml.bind.JAXBException) JAXPStreamFeatureWriter(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter) Feature(org.opengis.feature.Feature) XMLStreamException(javax.xml.stream.XMLStreamException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) JAXBException(javax.xml.bind.JAXBException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) XMLStreamException(javax.xml.stream.XMLStreamException) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException)

Example 14 with FeatureCollection

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

the class PostgresComplexTypeTest method testComplexInsert.

/**
 * 2 level depths feature test.
 */
@Test
public void testComplexInsert() throws DataStoreException, VersioningException {
    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 Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
    assertEquals(1, col.size());
    final FeatureIterator ite = col.iterator();
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertEquals(120l, resFeature.getPropertyValue("identifier"));
        final Feature resDriver = (Feature) resFeature.getProperty("driver");
        assertEquals("jean-michel", resDriver.getPropertyValue("name"));
        assertEquals("BHF:123456", resDriver.getPropertyValue("code"));
        final Collection<Feature> stops = (Collection<Feature>) resFeature.getPropertyValue("stops");
        assertEquals(3, stops.size());
        final boolean[] found = new boolean[3];
        for (Feature stop : stops) {
            final Timestamp time = (Timestamp) stop.getPropertyValue("time");
            final Point location = (Point) stop.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" + stop);
            }
        }
        for (boolean b : found) assertTrue(b);
    } finally {
        ite.close();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotoolkit.storage.feature.query.Query) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) Feature(org.opengis.feature.Feature) Timestamp(java.sql.Timestamp) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Coordinate(org.locationtech.jts.geom.Coordinate) 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) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 15 with FeatureCollection

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

the class PostgresSimpleTypeTest method testSimpleInsert.

@Test
public void testSimpleInsert() throws DataStoreException, VersioningException {
    reload(true);
    store.createFeatureType(FTYPE_SIMPLE);
    FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    Feature feature = resType.newInstance();
    feature.setPropertyValue("boolean", true);
    // byte type do not exist, it's converted to smallint/int2
    feature.setPropertyValue("byte", (short) 45);
    feature.setPropertyValue("short", (short) 963);
    feature.setPropertyValue("integer", 123456);
    feature.setPropertyValue("long", 456789l);
    feature.setPropertyValue("float", 7.3f);
    feature.setPropertyValue("double", 14.5);
    feature.setPropertyValue("string", "a string");
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("1"), addedIds.get(0));
    Session session = store.createSession(false);
    FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
    assertEquals(1, col.size());
    FeatureIterator ite = col.iterator();
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertEquals(true, resFeature.getPropertyValue("boolean"));
        assertEquals((short) 45, resFeature.getPropertyValue("byte"));
        assertEquals((short) 963, resFeature.getPropertyValue("short"));
        assertEquals(123456, resFeature.getPropertyValue("integer"));
        assertEquals(456789l, resFeature.getPropertyValue("long"));
        assertEquals(7.3f, resFeature.getPropertyValue("float"));
        assertEquals(14.5d, resFeature.getPropertyValue("double"));
        assertEquals("a string", resFeature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
    // SECOND TEST for NAN values ------------------------------------------
    reload(true);
    store.createFeatureType(FTYPE_SIMPLE);
    resType = store.getFeatureType(store.getNames().iterator().next().toString());
    feature = resType.newInstance();
    feature.setPropertyValue("fid", 0);
    feature.setPropertyValue("boolean", true);
    feature.setPropertyValue("byte", (short) 45);
    feature.setPropertyValue("short", (short) 963);
    feature.setPropertyValue("integer", 123456);
    feature.setPropertyValue("long", 456789l);
    feature.setPropertyValue("float", Float.NaN);
    feature.setPropertyValue("double", Double.NaN);
    feature.setPropertyValue("string", "a string");
    addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("1"), addedIds.get(0));
    session = store.createSession(false);
    col = session.getFeatureCollection(new Query(resType.getName()));
    assertEquals(1, col.size());
    ite = col.iterator();
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertEquals(true, resFeature.getPropertyValue("boolean"));
        assertEquals((short) 45, resFeature.getPropertyValue("byte"));
        assertEquals((short) 963, resFeature.getPropertyValue("short"));
        assertEquals(123456, resFeature.getPropertyValue("integer"));
        assertEquals(456789l, resFeature.getPropertyValue("long"));
        assertEquals(Float.NaN, resFeature.getPropertyValue("float"));
        assertEquals(Double.NaN, resFeature.getPropertyValue("double"));
        assertEquals("a 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)

Aggregations

FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)131 Feature (org.opengis.feature.Feature)75 Test (org.junit.Test)49 Coordinate (org.locationtech.jts.geom.Coordinate)49 LinearRing (org.locationtech.jts.geom.LinearRing)39 Query (org.geotoolkit.storage.feature.query.Query)31 FeatureType (org.opengis.feature.FeatureType)27 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)24 LineString (org.locationtech.jts.geom.LineString)23 MultiPoint (org.locationtech.jts.geom.MultiPoint)16 ProcessDescriptor (org.geotoolkit.process.ProcessDescriptor)15 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)15 Geometry (org.locationtech.jts.geom.Geometry)14 AbstractProcessTest (org.geotoolkit.processing.vector.AbstractProcessTest)13 Session (org.geotoolkit.storage.feature.session.Session)12 CheckCloseFeatureIterator (org.geotoolkit.storage.feature.CheckCloseFeatureIterator)10 Point (org.locationtech.jts.geom.Point)10 GenericName (org.opengis.util.GenericName)10 NoSuchElementException (java.util.NoSuchElementException)8 File (java.io.File)7