Search in sources :

Example 21 with FeatureIterator

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

the class FidQueryTest method testDeleteFeature.

@Test
public void testDeleteFeature() throws Exception {
    FeatureIterator features = ds.getFeatureReader(new Query(name));
    Feature feature;
    try {
        feature = features.next();
    } finally {
        if (features != null)
            features.close();
    }
    final ResourceId createFidFilter = FeatureExt.getId(feature);
    session.removeFeatures(name.toString(), createFidFilter);
    session.commit();
    fids.remove(FeatureExt.getId(feature).getIdentifier());
    assertEquals(fids.size(), ds.getCount(new Query(name)));
    features = ds.getFeatureReader(Query.filtered(name.toString(), createFidFilter));
    try {
        assertFalse(features.hasNext());
    } finally {
        if (features != null)
            features.close();
    }
    this.assertFidsMatch();
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Query(org.geotoolkit.storage.feature.query.Query) ResourceId(org.opengis.filter.ResourceId) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 22 with FeatureIterator

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

the class FidQueryTest method testAddFeature.

@Test
@Ignore
public void testAddFeature() throws Exception {
    final FeatureType schema = ds.getFeatureType(ds.getName().toString());
    final Feature newFeature = schema.newInstance();
    final GeometryFactory gf = org.geotoolkit.geometry.jts.JTS.getFactory();
    newFeature.setPropertyValue("a", gf.createPoint((new Coordinate(0, 0))));
    newFeature.setPropertyValue("b", new Long(0));
    newFeature.setPropertyValue("c", new Long(0));
    newFeature.setPropertyValue("d", "Hey");
    final Collection<Feature> collection = new ArrayList<>();
    collection.add(newFeature);
    final List<ResourceId> newFids;
    try (FeatureWriter writer = ds.getFeatureWriter(Query.filtered(name.toString(), Filter.exclude()))) {
        newFids = FeatureStoreUtilities.write(writer, collection);
    }
    assertEquals(1, newFids.size());
    // this.assertFidsMatch();
    final Filter filter = newFids.iterator().next();
    final Query query = new Query();
    query.setTypeName(schema.getName());
    query.setSelection(filter);
    final FeatureIterator features = ds.getFeatureReader(query);
    try {
        final Feature feature = features.next();
        for (PropertyType desc : schema.getProperties(true)) {
            final Object value = feature.getPropertyValue(desc.getName().toString());
            final Object newValue = newFeature.getPropertyValue(desc.getName().toString());
            if (value instanceof Geometry) {
                assertTrue(((Geometry) newValue).equals((Geometry) value));
            } else {
                assertEquals(newValue, value);
            }
        }
        assertFalse(features.hasNext());
    } finally {
        if (features != null)
            features.close();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotoolkit.storage.feature.query.Query) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) ResourceId(org.opengis.filter.ResourceId) Filter(org.opengis.filter.Filter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with FeatureIterator

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

the class JDBCAddDelta method commit.

@Override
public Map<String, String> commit(FeatureStore store) throws DataStoreException {
    final DefaultJDBCFeatureStore jdbcstore = (DefaultJDBCFeatureStore) store;
    final Connection cnx = ((JDBCSession) session).getTransaction();
    final List<ResourceId> createdIds = jdbcstore.addFeatures(type, features, cnx, null);
    // iterator and list should have the same size
    final Map<String, String> updates = new HashMap<>();
    final FeatureIterator ite = features.iterator();
    int i = 0;
    try {
        if (createdIds != null && !createdIds.isEmpty()) {
            while (ite.hasNext()) {
                final Feature f = ite.next();
                final String id = (String) f.getPropertyValue(AttributeConvention.IDENTIFIER);
                updates.put(id, createdIds.get(i).getIdentifier());
                i++;
            }
        }
    } finally {
        ite.close();
    }
    features.clear();
    return updates;
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) DefaultJDBCFeatureStore(org.geotoolkit.db.DefaultJDBCFeatureStore) ResourceId(org.opengis.filter.ResourceId) HashMap(java.util.HashMap) Connection(java.sql.Connection) Feature(org.opengis.feature.Feature)

Example 24 with FeatureIterator

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

the class JDBCSession method removeFeatures.

/**
 * {@inheritDoc }
 * Override here split remove query in order to avoid
 * "stack depth limit exceeded" exception.
 */
@Override
public void removeFeatures(final String groupName, final Filter filter) throws DataStoreException {
    checkVersion();
    // will raise an error if the name doesn't exist
    store.getFeatureType(groupName);
    if (isAsynchrone()) {
        List<Filter<Object>> removeIdFilters = new ArrayList<>();
        // split Id filter
        if (filter instanceof ResourceId) {
            final ResourceId removed = (ResourceId) filter;
            removeIdFilters.add(removed);
        } else {
            Set<ResourceId<Object>> identifiers = new HashSet<>();
            final Query qb = new Query(groupName);
            qb.setSelection(filter);
            final FeatureIterator ite = getFeatureIterator(qb);
            try {
                while (ite.hasNext()) {
                    identifiers.add(FeatureExt.getId(ite.next()));
                    // flush in list of filters
                    if (identifiers.size() == MAX_ID_IN_REQUEST) {
                        removeIdFilters.addAll(identifiers);
                        identifiers.clear();
                    }
                }
                if (!identifiers.isEmpty()) {
                    removeIdFilters.addAll(identifiers);
                }
            } finally {
                ite.close();
            }
            if (removeIdFilters.isEmpty()) {
                // no feature match this filter, no need to create to remove delta
                return;
            }
        }
        for (final Filter removeIdFilter : removeIdFilters) {
            getDiff().add(createRemoveDelta(this, groupName, removeIdFilter));
        }
        fireSessionChanged();
    } else {
        store.removeFeatures(groupName, filter);
    }
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Query(org.geotoolkit.storage.feature.query.Query) Filter(org.opengis.filter.Filter) ResourceId(org.opengis.filter.ResourceId)

Example 25 with FeatureIterator

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

the class UnionProcess method unionFeatureToFC.

/**
 * Generate an union FeatureCollection comparing a Feature to a FeatureCollection.
 * During the second pass, we remove duplicates Features
 *
 * @param newFeatureType the new FeatureType
 * @param unionFC union FeatureCollection
 * @param inputGeomName attribute name of the used Geometry from inputFeature
 * @param unionGeomName attribute name of the used Geometry from unionFC
 * @param featureList Set of already created Features (it's used in order to remove duplicate Features)
 * @return the result FeatureCollection of an union between a Feature and a FeatureCollection
 */
static FeatureCollection unionFeatureToFC(final Feature inputFeature, final FeatureType newFeatureType, final FeatureCollection unionFC, final String inputGeomName, final String unionGeomName, final boolean firstPass, final Set<String> featureList) throws TransformException, FactoryException {
    final FeatureCollection resultFeatureList = FeatureStoreUtilities.collection(FeatureExt.getId(inputFeature).getIdentifier(), newFeatureType);
    /*
         * In order to get all part of Feature, add a second pass with the diffenrence between the FeatureGeometry
         * and united intersections. if return nothing we have all the geometry feature, else we add the difference
         */
    Geometry inputGeometry = org.geotoolkit.geometry.jts.JTS.getFactory().buildGeometry(Collections.EMPTY_LIST);
    for (final PropertyType inputProperty : inputFeature.getType().getProperties(true)) {
        if (AttributeConvention.isGeometryAttribute(inputProperty)) {
            final String name = inputProperty.getName().toString();
            if (name.equals(inputGeomName)) {
                inputGeometry = (Geometry) inputFeature.getPropertyValue(name);
            }
        }
    }
    Geometry remainingGeometry = inputGeometry;
    boolean isIntersected = false;
    // Check if each union Features intersect inputFeature. if yes, create a new Feature which is union of both
    try (final FeatureIterator unionIter = unionFC.iterator()) {
        while (unionIter.hasNext()) {
            final Feature unionFeature = unionIter.next();
            final String featureID;
            // Invert ID order for the second pass (firstpass "inputID U unionID", second pass "unionID U inputID")
            if (firstPass) {
                featureID = FeatureExt.getId(inputFeature).getIdentifier() + "-" + FeatureExt.getId(unionFeature).getIdentifier();
            } else {
                featureID = FeatureExt.getId(unionFeature).getIdentifier() + "-" + FeatureExt.getId(inputFeature).getIdentifier();
            }
            final Feature resultFeature = unionFeatureToFeature(inputFeature, unionFeature, newFeatureType, inputGeomName, unionGeomName, featureID, firstPass);
            // Else we add the resutl Feature to resultFeatureList
            if (resultFeature != null) {
                isIntersected = true;
                resultFeatureList.add(resultFeature);
                Geometry intersectGeom = (Geometry) resultFeature.getPropertyValue(AttributeConvention.GEOMETRY);
                remainingGeometry = remainingGeometry.difference(intersectGeom);
            }
        }
    }
    // If remaining Geometry is empty and isIntersecting boolean is false, mean the geometry
    if (remainingGeometry.isEmpty() && !isIntersected) {
        final Feature remainingFeature = newFeatureType.newInstance();
        FeatureExt.setId(remainingFeature, FeatureExt.getId(inputFeature));
        // Copy none Geometry attributes
        for (final PropertyType inputProperty : inputFeature.getType().getProperties(true)) {
            if (!(inputProperty instanceof AttributeType) || AttributeConvention.contains(inputProperty.getName()))
                continue;
            if (!AttributeConvention.isGeometryAttribute(inputProperty)) {
                final String name = inputProperty.getName().toString();
                remainingFeature.setPropertyValue(name, inputFeature.getPropertyValue(name));
            }
        }
        if (firstPass) {
            remainingFeature.setPropertyValue(inputGeomName, inputGeometry);
        } else {
            remainingFeature.setPropertyValue(unionGeomName, inputGeometry);
        }
        resultFeatureList.add(remainingFeature);
    }
    // Create a remaining Feature with the inputGeometry
    if (!(remainingGeometry.isEmpty())) {
        if (remainingGeometry.equalsTopo(inputGeometry)) {
            remainingGeometry = inputGeometry;
        }
        final Feature remainingFeature = newFeatureType.newInstance();
        FeatureExt.setId(remainingFeature, FeatureExt.getId(inputFeature));
        // Copy none Geometry attributes
        for (final PropertyType inputProperty : inputFeature.getType().getProperties(true)) {
            if (!(inputProperty instanceof AttributeType) || AttributeConvention.contains(inputProperty.getName()))
                continue;
            if (!AttributeConvention.isGeometryAttribute(inputProperty)) {
                final String name = inputProperty.getName().toString();
                remainingFeature.setPropertyValue(name, inputFeature.getPropertyValue(name));
            }
        }
        // System.out.println("LOG Empty remaining Geometry");
        if (firstPass) {
            remainingFeature.setPropertyValue(inputGeomName, remainingGeometry);
        } else {
            remainingFeature.setPropertyValue(unionGeomName, remainingGeometry);
        }
        resultFeatureList.add(remainingFeature);
    }
    final Collection<Feature> featureToRemove = new ArrayList<>();
    /* Check if created features are already present in featureList.
         * If yes, we delete them from the returning FeatureCollection
         * else we add them into the featureList
         */
    for (final Feature createdFeature : resultFeatureList) {
        final String createdFeatureID = FeatureExt.getId(createdFeature).getIdentifier();
        if (featureList.contains(createdFeatureID)) {
            featureToRemove.add(createdFeature);
        } else {
            featureList.add(createdFeatureID);
        }
    }
    // remove existing feature
    resultFeatureList.removeAll(featureToRemove);
    return resultFeatureList;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature)

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