Search in sources :

Example 26 with FeatureIterator

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

the class ClipProcess method clipFeature.

/**
 * Clip a feature with the FeatureCollection's geometries
 *
 * @param newType the new FeatureType for the Feature
 * @param featureClippingList FeatureCollection used to clip
 */
public static Feature clipFeature(final Feature oldFeature, final FeatureType newType, final FeatureCollection featureClippingList) throws FactoryException, MismatchedDimensionException, TransformException {
    final Feature resultFeature = newType.newInstance();
    FeatureExt.setId(resultFeature, FeatureExt.getId(oldFeature));
    for (final PropertyType property : oldFeature.getType().getProperties(true)) {
        final String name = property.getName().toString();
        final Object value = oldFeature.getPropertyValue(name);
        // for each Geometry in the oldFeature
        if (AttributeConvention.isGeometryAttribute(property)) {
            final Geometry inputGeom = (Geometry) value;
            final CoordinateReferenceSystem inputGeomCRS = FeatureExt.getCRS(property);
            // loop and test intersection between each geometry of each clipping feature from
            // clipping FeatureCollection
            final List<Geometry> bufferInterGeometries = new ArrayList<>();
            try (final FeatureIterator clipIterator = featureClippingList.iterator()) {
                while (clipIterator.hasNext()) {
                    final Feature clipFeature = clipIterator.next();
                    for (PropertyType clipFeatureProperty : clipFeature.getType().getProperties(true)) {
                        if (AttributeConvention.isGeometryAttribute(clipFeatureProperty)) {
                            Geometry clipGeom = (Geometry) clipFeature.getPropertyValue(clipFeatureProperty.getName().toString());
                            final CoordinateReferenceSystem clipGeomCRS = FeatureExt.getCRS(clipFeatureProperty);
                            // re-project clipping geometry into input Feature geometry CRS
                            clipGeom = VectorProcessUtils.repojectGeometry(inputGeomCRS, clipGeomCRS, clipGeom);
                            final Geometry interGeometry = VectorProcessUtils.geometryIntersection(inputGeom, clipGeom);
                            // if an intersection geometry exist, store it into a buffer Collection
                            if (interGeometry != null) {
                                bufferInterGeometries.add(interGeometry);
                            }
                        }
                    }
                }
            }
            // if the feature intersect one of the feature clipping list
            final int size = bufferInterGeometries.size();
            if (size == 1) {
                resultFeature.setPropertyValue(name, bufferInterGeometries.get(0));
            } else if (size > 1) {
                final Geometry[] bufferArray = bufferInterGeometries.toArray(new Geometry[bufferInterGeometries.size()]);
                // create a GeometryCollection with all the intersections
                final GeometryCollection resultGeometry = GF.createGeometryCollection(bufferArray);
                resultFeature.setPropertyValue(name, resultGeometry);
            } else {
                return null;
            }
        } else if (property instanceof AttributeType && !(AttributeConvention.contains(property.getName()))) {
            // others properties (no geometry)
            resultFeature.setPropertyValue(name, value);
        }
    }
    return resultFeature;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Feature(org.opengis.feature.Feature)

Example 27 with FeatureIterator

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

the class RegroupProcess method regroupFeature.

/**
 * Create a Feature with one of attribute values and an union of all features geometry with the
 * same attribute value.
 *
 * @param regroupAttribute attribute specified in process input
 * @param attrubuteValue one value of the specified attribute
 * @param newFeatureType the new FeatureTYpe
 * @param geometryName if null we use the default Feature geometry
 * @param filtredList the input FeatureCollection filtered on attribute value
 */
static Feature regroupFeature(final String regroupAttribute, final Object attributeValue, final FeatureType newFeatureType, String geometryName, final FeatureCollection filtredList) {
    final List<Geometry> geoms = new ArrayList<>();
    try (final FeatureIterator featureIter = filtredList.iterator()) {
        while (featureIter.hasNext()) {
            final Feature feature = featureIter.next();
            if (geometryName == null) {
                geometryName = AttributeConvention.GEOMETRY;
            }
            for (final PropertyType property : feature.getType().getProperties(true)) {
                // if property is a geometry
                if (AttributeConvention.isGeometryAttribute(property)) {
                    // if it's the property we needed
                    final String name = property.getName().toString();
                    if (name.equals(geometryName)) {
                        Geometry candidate = (Geometry) feature.getPropertyValue(name);
                        geoms.add(candidate);
                    }
                }
            }
        }
    }
    Geometry regroupGeometry = org.geotoolkit.geometry.jts.JTS.getFactory().buildGeometry(geoms);
    Feature resultFeature;
    // In case
    if (regroupAttribute == null && attributeValue == null) {
        resultFeature = newFeatureType.newInstance();
        resultFeature.setPropertyValue(AttributeConvention.IDENTIFIER, "groupedGeometryFeature");
        resultFeature.setPropertyValue(geometryName, regroupGeometry);
    } else {
        // result feature
        resultFeature = newFeatureType.newInstance();
        resultFeature.setPropertyValue(AttributeConvention.IDENTIFIER, regroupAttribute + "-" + attributeValue);
        resultFeature.setPropertyValue(regroupAttribute, attributeValue);
        resultFeature.setPropertyValue(geometryName, regroupGeometry);
    }
    return resultFeature;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature)

Example 28 with FeatureIterator

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

the class DifferenceProcess method clipFeature.

/**
 * Compute difference between a feature and FeatureCollection's geometries
 *
 * @param newType the new FeatureType for the Feature
 * @param featureClippingList FeatureCollection used to compute the difference
 */
public static Feature clipFeature(final Feature oldFeature, final FeatureType newType, final FeatureCollection featureClippingList) throws MismatchedDimensionException, TransformException, FactoryException {
    final Feature resultFeature = newType.newInstance();
    FeatureExt.setId(resultFeature, FeatureExt.getId(oldFeature));
    for (final PropertyType property : oldFeature.getType().getProperties(true)) {
        final String name = property.getName().toString();
        final Object value = oldFeature.getPropertyValue(name);
        // for each Geometry in the oldFeature
        if (AttributeConvention.isGeometryAttribute(property)) {
            final CoordinateReferenceSystem inputGeomCRS = FeatureExt.getCRS(property);
            // loop and test intersection between each geometry of each clipping feature from
            // clipping FeatureCollection
            Geometry resultGeometry = (Geometry) value;
            try (final FeatureIterator clipIterator = featureClippingList.iterator()) {
                while (clipIterator.hasNext()) {
                    final Feature clipFeature = clipIterator.next();
                    for (PropertyType clipFeatureProperty : clipFeature.getType().getProperties(true)) {
                        if (AttributeConvention.isGeometryAttribute(clipFeatureProperty)) {
                            Geometry diffGeom = (Geometry) clipFeature.getPropertyValue(clipFeatureProperty.getName().toString());
                            final CoordinateReferenceSystem diffGeomCRS = FeatureExt.getCRS(clipFeatureProperty);
                            // re-project clipping geometry into input Feature geometry CRS
                            diffGeom = VectorProcessUtils.repojectGeometry(inputGeomCRS, diffGeomCRS, diffGeom);
                            final Geometry diffGeometry = VectorProcessUtils.geometryDifference(resultGeometry, diffGeom);
                            /*
                                 * If diffGeometry return null, it's because the result geomerty
                                 * is contained into another Geometry. So we stop the loop and return null.
                                 */
                            if (diffGeometry != null) {
                                resultGeometry = diffGeometry;
                            } else {
                                return null;
                            }
                        }
                    }
                }
            }
            resultFeature.setPropertyValue(name, resultGeometry);
        } else if (property instanceof AttributeType && !(AttributeConvention.contains(property.getName()))) {
            // others properties (no geometry)
            resultFeature.setPropertyValue(name, value);
        }
    }
    return resultFeature;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Feature(org.opengis.feature.Feature)

Example 29 with FeatureIterator

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

the class FeatureStreamsTest method testWrapIterator.

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

Example 30 with FeatureIterator

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

the class FeatureStreamsTest method testMaxIterator.

@Test
public void testMaxIterator() {
    FeatureCollection collection = buildSimpleFeatureCollection();
    FeatureIterator ite = FeatureStreams.limit(collection.iterator(), 10);
    assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
    ite = FeatureStreams.limit(collection.iterator(), 2);
    assertEquals(2, FeatureStoreUtilities.calculateCount(ite));
    ite = FeatureStreams.limit(collection.iterator(), 1);
    assertEquals(id1, ite.next().getPropertyValue(AttributeConvention.IDENTIFIER));
    try {
        ite.next();
        fail("Should have raise a no such element exception.");
    } catch (NoSuchElementException ex) {
    // ok
    }
    // check has next do not iterate
    ite = FeatureStreams.limit(collection.iterator(), 10);
    testIterationOnNext(ite, 3);
    // check sub iterator is properly closed
    CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(collection.iterator());
    assertFalse(checkIte.isClosed());
    ite = FeatureStreams.limit(checkIte, 10);
    while (ite.hasNext()) ite.next();
    ite.close();
    assertTrue(checkIte.isClosed());
}
Also used : CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) NoSuchElementException(java.util.NoSuchElementException) CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) Test(org.junit.Test)

Aggregations

FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)53 Feature (org.opengis.feature.Feature)41 Test (org.junit.Test)34 Query (org.geotoolkit.storage.feature.query.Query)30 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)24 FeatureType (org.opengis.feature.FeatureType)20 ResourceId (org.opengis.filter.ResourceId)20 Coordinate (org.locationtech.jts.geom.Coordinate)11 Point (org.locationtech.jts.geom.Point)11 Session (org.geotoolkit.storage.feature.session.Session)10 Geometry (org.locationtech.jts.geom.Geometry)10 HashMap (java.util.HashMap)9 CheckCloseFeatureIterator (org.geotoolkit.storage.feature.CheckCloseFeatureIterator)9 ArrayList (java.util.ArrayList)8 Date (java.util.Date)7 NoSuchElementException (java.util.NoSuchElementException)7 Filter (org.opengis.filter.Filter)7 File (java.io.File)6 Version (org.geotoolkit.version.Version)6 VersionControl (org.geotoolkit.version.VersionControl)6