Search in sources :

Example 1 with GeometryScaleTransformer

use of org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer 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 2 with GeometryScaleTransformer

use of org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer in project geotoolkit by Geomatys.

the class GenericQueryFeatureIterator method wrap.

public static FeatureReader wrap(FeatureReader reader, final Query remainingParameters) throws DataStoreException {
    final long start = remainingParameters.getOffset();
    final long max = remainingParameters.getLimit().orElse(-1);
    Filter filter = remainingParameters.getSelection();
    if (filter == null)
        filter = Filter.include();
    final String[] properties = remainingParameters.getPropertyNames();
    final SortProperty[] sorts = QueryUtilities.getSortProperties(remainingParameters.getSortBy());
    final double[] resampling = remainingParameters.getResolution();
    final Hints hints = remainingParameters.getHints();
    // that may cause out of memory errors.
    if (sorts != null && sorts.length != 0) {
        reader = FeatureStreams.sort(reader, sorts);
    }
    // we must keep the filter first since it impacts the start index and max feature
    if (filter != null && filter != Filter.include()) {
        if (filter == Filter.exclude()) {
            // filter that exclude everything, use optimzed reader
            reader = FeatureStreams.emptyReader(reader.getFeatureType());
            // close original reader
            reader.close();
        } else {
            reader = FeatureStreams.filter(reader, filter);
        }
    }
    // wrap start index -----------------------------------------------------
    if (start > 0) {
        reader = FeatureStreams.skip(reader, (int) start);
    }
    // wrap max -------------------------------------------------------------
    if (max != -1) {
        if (max == 0) {
            // use an optimized reader
            reader = FeatureStreams.emptyReader(reader.getFeatureType());
            // close original reader
            reader.close();
        } else {
            reader = FeatureStreams.limit(reader, (int) max);
        }
    }
    // wrap properties  -----------------------------------------------------
    final FeatureType original = reader.getFeatureType();
    if (properties != null && !FeatureTypeExt.isAllProperties(original, properties)) {
        try {
            reader = FeatureStreams.decorate(reader, new ViewMapper(original, properties), hints);
        } catch (MismatchedFeatureException | IllegalStateException ex) {
            throw new DataStoreException(ex);
        }
    }
    // wrap resampling ------------------------------------------------------
    if (resampling != null) {
        final GeometryScaleTransformer trs = new GeometryScaleTransformer(resampling[0], resampling[1]);
        final TransformMapper ttype = new TransformMapper(reader.getFeatureType(), trs);
        reader = FeatureStreams.decorate(reader, ttype, hints);
    }
    return reader;
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) Hints(org.geotoolkit.factory.Hints) TransformMapper(org.geotoolkit.feature.TransformMapper) ViewMapper(org.geotoolkit.feature.ViewMapper) GeometryScaleTransformer(org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer) SortProperty(org.opengis.filter.SortProperty) Filter(org.opengis.filter.Filter) MismatchedFeatureException(org.opengis.feature.MismatchedFeatureException)

Example 3 with GeometryScaleTransformer

use of org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer in project geotoolkit by Geomatys.

the class AbstractFeatureCollection method subset.

@Override
public FeatureCollection subset(final Query remainingParameters) throws DataStoreException {
    FeatureCollection result = this;
    final long start = remainingParameters.getOffset();
    final long max = remainingParameters.getLimit().orElse(-1);
    Filter filter = remainingParameters.getSelection();
    if (filter == null)
        filter = Filter.include();
    final String[] properties = remainingParameters.getPropertyNames();
    final SortProperty[] sorts = QueryUtilities.getSortProperties(remainingParameters.getSortBy());
    final double[] resampling = remainingParameters.getResolution();
    final Hints hints = remainingParameters.getHints();
    // that may cause out of memory errors.
    if (sorts != null && sorts.length != 0) {
        result = FeatureStreams.sort(result, sorts);
    }
    // we must keep the filter first since it impacts the start index and max feature
    if (filter != null && filter != Filter.include()) {
        if (filter == Filter.exclude()) {
            // filter that exclude everything, use optimzed reader
            result = FeatureStreams.emptyCollection(result);
        } else {
            result = FeatureStreams.filter(result, filter);
        }
    }
    // wrap start index -----------------------------------------------------
    if (start > 0) {
        result = FeatureStreams.skip(result, (int) start);
    }
    // wrap max -------------------------------------------------------------
    if (max != -1) {
        if (max == 0) {
            // use an optimized reader
            result = FeatureStreams.emptyCollection(result);
        } else {
            result = FeatureStreams.limit(result, (int) max);
        }
    }
    // wrap properties --------------------
    final FeatureType original = result.getType();
    FeatureType mask = original;
    if (properties != null && FeatureTypeExt.isAllProperties(original, properties)) {
        try {
            result = FeatureStreams.decorate(result, new ViewMapper(mask, properties));
        } catch (MismatchedFeatureException | IllegalStateException ex) {
            throw new DataStoreException(ex);
        }
    }
    // wrap resampling ------------------------------------------------------
    if (resampling != null) {
        final GeometryScaleTransformer trs = new GeometryScaleTransformer(resampling[0], resampling[1]);
        final TransformMapper ttype = new TransformMapper(result.getType(), trs);
        result = FeatureStreams.decorate(result, ttype);
    }
    return result;
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) Hints(org.geotoolkit.factory.Hints) TransformMapper(org.geotoolkit.feature.TransformMapper) ViewMapper(org.geotoolkit.feature.ViewMapper) GeometryScaleTransformer(org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer) SortProperty(org.opengis.filter.SortProperty) Filter(org.opengis.filter.Filter) MismatchedFeatureException(org.opengis.feature.MismatchedFeatureException)

Aggregations

Hints (org.geotoolkit.factory.Hints)3 TransformMapper (org.geotoolkit.feature.TransformMapper)3 GeometryScaleTransformer (org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer)3 FeatureType (org.opengis.feature.FeatureType)3 DataStoreException (org.apache.sis.storage.DataStoreException)2 ViewMapper (org.geotoolkit.feature.ViewMapper)2 MismatchedFeatureException (org.opengis.feature.MismatchedFeatureException)2 Filter (org.opengis.filter.Filter)2 SortProperty (org.opengis.filter.SortProperty)2 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)1 GeometryTransformer (org.geotoolkit.geometry.jts.transform.GeometryTransformer)1 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)1 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)1 Query (org.geotoolkit.storage.feature.query.Query)1 Test (org.junit.Test)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 LineString (org.locationtech.jts.geom.LineString)1 Feature (org.opengis.feature.Feature)1 GenericName (org.opengis.util.GenericName)1