Search in sources :

Example 81 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testRemoveFromFrontAndCloseTransaction.

/**
 * Create a test file, then continue removing the first entry until there
 * are no features left.
 */
@Test
public void testRemoveFromFrontAndCloseTransaction() throws Throwable {
    IndexedShapefileFeatureStore sds = createDataStore();
    int idx = loadFeatures(sds).size();
    while (idx > 0) {
        FeatureWriter writer = null;
        try {
            writer = sds.getFeatureWriter(new Query(sds.getName()));
            writer.next();
            writer.remove();
        } finally {
            if (writer != null) {
                writer.close();
                writer = null;
            }
        }
        assertEquals(--idx, loadFeatures(sds).size());
    }
}
Also used : FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) Point(org.locationtech.jts.geom.Point) Test(org.junit.Test)

Example 82 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class Copy method insert.

private void insert(GenericName name, final Session sourceSS, final Session targetSS, Query query, final boolean erase, final boolean newVersion) throws DataStoreException {
    FeatureType type = sourceSS.getFeatureStore().getFeatureType(name.toString());
    // Change * to featureType default geometry name
    if (query != null && query.getSelection() != null) {
        final Filter newFilter = (Filter) new BBOXFilterVisitor(type).visit((Filter) query.getSelection());
        final Query builder = new Query();
        builder.copy(query);
        builder.setSelection(newFilter);
        query = builder;
    }
    final FeatureCollection collection = sourceSS.getFeatureCollection(query);
    // get the real FeatureType of collection (in case of reprojection, CRS is different).
    type = collection.getType();
    if (targetSS.getFeatureStore().getNames().contains(name)) {
        // ERASE
        if (erase) {
            targetSS.getFeatureStore().deleteFeatureType(name.toString());
            targetSS.getFeatureStore().createFeatureType(type);
        }
    } else {
        targetSS.getFeatureStore().createFeatureType(type);
    }
    // get the created name, namespace might change
    name = targetSS.getFeatureStore().getFeatureType(type.getName().tip().toString()).getName();
    if (targetSS.getFeatureStore().getQueryCapabilities().handleVersioning()) {
        try {
            targetSS.getFeatureStore().getVersioning(name.toString()).startVersioning();
        } catch (VersioningException ex) {
            throw new DataStoreException(ex.getLocalizedMessage(), ex);
        }
    }
    // NEW VERSION (remove old features)
    if (newVersion) {
        targetSS.removeFeatures(name.toString(), Filter.include());
    }
    // Logger
    final StringBuilder logMsg = new StringBuilder("Insert ");
    logMsg.append(collection.size()).append(" features ");
    logMsg.append("in type ").append(name.tip().toString());
    logMsg.append(" [");
    if (erase) {
        logMsg.append("ERASE");
        if (newVersion)
            logMsg.append(", NEWVERSION");
    }
    if (newVersion)
        logMsg.append("NEWVERSION");
    logMsg.append("]");
    LOGGER.log(Level.INFO, logMsg.toString());
    // APPEND
    targetSS.addFeatures(name.toString(), collection);
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.geotoolkit.storage.feature.query.Query) Filter(org.opengis.filter.Filter) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) VersioningException(org.geotoolkit.version.VersioningException)

Aggregations

Query (org.geotoolkit.storage.feature.query.Query)82 Test (org.junit.Test)50 Feature (org.opengis.feature.Feature)43 FeatureType (org.opengis.feature.FeatureType)36 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)31 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)30 Session (org.geotoolkit.storage.feature.session.Session)20 Point (org.locationtech.jts.geom.Point)18 ResourceId (org.opengis.filter.ResourceId)18 GenericName (org.opengis.util.GenericName)15 Coordinate (org.locationtech.jts.geom.Coordinate)14 FeatureWriter (org.geotoolkit.storage.feature.FeatureWriter)13 File (java.io.File)10 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)9 FeatureStore (org.geotoolkit.storage.feature.FeatureStore)9 Filter (org.opengis.filter.Filter)9 URL (java.net.URL)8 Date (java.util.Date)8 DataStoreException (org.apache.sis.storage.DataStoreException)8 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)8