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;
}
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;
}
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;
}
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());
}
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());
}
Aggregations