Search in sources :

Example 1 with ReprojectMapper

use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.

the class MIFFeatureSet method add.

@Override
public void add(Iterator<? extends Feature> newFeatures) throws DataStoreException {
    final FeatureType ft = getType();
    final MIFReader reader = new MIFReader(store.manager, ft);
    try (final MIFWriter writer = new MIFWriter(store.manager, reader)) {
        // We remove the features as we get them. We don't need to write them as the default writing behaviour is append mode.
        while (writer.hasNext()) {
            writer.next();
            writer.remove();
        }
        if (store.manager.getWrittenCRS() != null) {
            ReprojectMapper mapper = null;
            while (newFeatures.hasNext()) {
                Feature f = newFeatures.next();
                if (mapper == null) {
                    final FeatureType type = f.getType();
                    mapper = new ReprojectMapper(type, store.manager.getWrittenCRS());
                }
                f = mapper.apply(f);
                final Feature candidate = writer.next();
                FeatureExt.copy(f, candidate, false);
                writer.write();
            }
        } else {
            while (newFeatures.hasNext()) {
                final Feature f = newFeatures.next();
                final Feature candidate = writer.next();
                FeatureExt.copy(f, candidate, false);
                writer.write();
            }
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) ReprojectMapper(org.geotoolkit.feature.ReprojectMapper) Feature(org.opengis.feature.Feature)

Example 2 with ReprojectMapper

use of org.geotoolkit.feature.ReprojectMapper 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 ReprojectMapper

use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.

the class ReprojectFeatureTypeTest method reprojectAttributeTest.

@Test
public void reprojectAttributeTest() {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Point.class).setName("attGeom").setCRS(CommonCRS.WGS84.geographic());
    final FeatureType baseType = ftb.build();
    // test view type
    final ReprojectMapper reprojType = new ReprojectMapper(baseType, CommonCRS.WGS84.normalizedGeographic());
    final Collection<? extends PropertyType> properties = reprojType.getMappedType().getProperties(true);
    assertEquals(1, properties.size());
    // test feature
    final Feature baseFeature = baseType.newInstance();
    baseFeature.setPropertyValue("attGeom", GF.createPoint(new Coordinate(10, 20)));
    final Feature reprojFeature = reprojType.apply(baseFeature);
    assertEquals(GF.createPoint(new Coordinate(20, 10)), reprojFeature.getPropertyValue("attGeom"));
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Coordinate(org.locationtech.jts.geom.Coordinate) ReprojectMapper(org.geotoolkit.feature.ReprojectMapper) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 4 with ReprojectMapper

use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.

the class NetcdfFeatureReader method getFeatureFromFOI.

protected final Feature getFeatureFromFOI(final AnyFeature foi) {
    if (foi instanceof SamplingFeature) {
        final SamplingFeature feature = (SamplingFeature) foi;
        final org.opengis.geometry.Geometry isoGeom = feature.getGeometry();
        try {
            final Geometry geom;
            if (isoGeom instanceof AbstractGeometry) {
                geom = GeometrytoJTS.toJTS((AbstractGeometry) isoGeom, AxisResolve.STRICT);
            } else {
                geom = null;
            }
            if (firstCRS && isoGeom != null) {
                // configure crs in the feature type
                final CoordinateReferenceSystem crs = ((AbstractGeometry) isoGeom).getCoordinateReferenceSystem(false);
                type = new ReprojectMapper(type, crs).getMappedType();
                firstCRS = false;
            }
            final Feature f = type.newInstance();
            f.setPropertyValue(AttributeConvention.IDENTIFIER, feature.getId());
            f.setPropertyValue(OMFeatureTypes.ATT_DESC.toString(), feature.getDescription());
            f.setPropertyValue(OMFeatureTypes.ATT_NAME.toString(), feature.getName());
            f.setPropertyValue(OMFeatureTypes.ATT_POSITION.toString(), geom);
            final List<String> sampleds = new ArrayList<>();
            for (FeatureProperty featProp : feature.getSampledFeatures()) {
                if (featProp.getHref() != null) {
                    sampleds.add(featProp.getHref());
                }
            }
            f.setPropertyValue(OMFeatureTypes.ATT_SAMPLED.toString(), sampleds);
            return f;
        } catch (FactoryException ex) {
            LOGGER.log(Level.WARNING, "error while transforming GML geometry to JTS", ex);
        }
    } else {
        LOGGER.warning("unable to find a valid feature of interest in the observation");
    }
    return null;
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) ReprojectMapper(org.geotoolkit.feature.ReprojectMapper) ArrayList(java.util.ArrayList) SamplingFeature(org.geotoolkit.sampling.xml.SamplingFeature) SamplingFeature(org.geotoolkit.sampling.xml.SamplingFeature) Feature(org.opengis.feature.Feature) AnyFeature(org.opengis.observation.AnyFeature) FeatureProperty(org.geotoolkit.gml.xml.FeatureProperty) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 5 with ReprojectMapper

use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.

the class ReprojectFeatureTypeTest method reprojectOperationTest.

@Test
public void reprojectOperationTest() {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Point.class).setName("attGeom").setCRS(CommonCRS.WGS84.geographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType baseType = ftb.build();
    // test view type
    final ReprojectMapper viewType = new ReprojectMapper(baseType, CommonCRS.WGS84.normalizedGeographic());
    final Collection<? extends PropertyType> properties = viewType.getMappedType().getProperties(true);
    assertEquals(3, properties.size());
    assertTrue(viewType.getMappedType().getProperty("attGeom") instanceof AttributeType);
    assertTrue(viewType.getMappedType().getProperty("sis:geometry") instanceof Operation);
    // test feature
    final Feature baseFeature = baseType.newInstance();
    baseFeature.setPropertyValue("attGeom", GF.createPoint(new Coordinate(10, 20)));
    final Feature viewFeature = viewType.apply(baseFeature);
    assertEquals(GF.createPoint(new Coordinate(20, 10)), viewFeature.getPropertyValue("attGeom"));
    assertEquals(GF.createPoint(new Coordinate(20, 10)), viewFeature.getPropertyValue("sis:geometry"));
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Coordinate(org.locationtech.jts.geom.Coordinate) ReprojectMapper(org.geotoolkit.feature.ReprojectMapper) AttributeType(org.opengis.feature.AttributeType) Point(org.locationtech.jts.geom.Point) Operation(org.opengis.feature.Operation) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Aggregations

ReprojectMapper (org.geotoolkit.feature.ReprojectMapper)6 Feature (org.opengis.feature.Feature)5 Test (org.junit.Test)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 FeatureType (org.opengis.feature.FeatureType)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)2 Geometry (org.locationtech.jts.geom.Geometry)2 Point (org.locationtech.jts.geom.Point)2 ArrayList (java.util.ArrayList)1 Hints (org.geotoolkit.factory.Hints)1 AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)1 FeatureProperty (org.geotoolkit.gml.xml.FeatureProperty)1 SamplingFeature (org.geotoolkit.sampling.xml.SamplingFeature)1 CheckCloseFeatureIterator (org.geotoolkit.storage.feature.CheckCloseFeatureIterator)1 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)1 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)1 Query (org.geotoolkit.storage.feature.query.Query)1 AttributeType (org.opengis.feature.AttributeType)1 Operation (org.opengis.feature.Operation)1