Search in sources :

Example 11 with FeatureWriter

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

the class DefaultJDBCFeatureStore method handleRemoveWithFeatureWriter.

/**
 * Convenient method to handle adding features operation by using the
 * FeatureWriter.
 */
protected void handleRemoveWithFeatureWriter(final String groupName, final Filter filter, Connection cnx) throws DataStoreException {
    try (FeatureWriter writer = getFeatureWriterInternal(groupName, filter, EditMode.UPDATE, cnx, null)) {
        while (writer.hasNext()) {
            writer.next();
            writer.remove();
        }
    } catch (FeatureStoreRuntimeException | IOException ex) {
        throw new DataStoreException(ex);
    }
}
Also used : FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IOException(java.io.IOException)

Example 12 with FeatureWriter

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

the class ShapefileDataStoreTest method testWriteShapefileWithNoRecords.

@Test
public void testWriteShapefileWithNoRecords() throws Exception {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder().setName("whatever");
    ftb.addAttribute(Polygon.class).setName("a").addRole(AttributeRole.DEFAULT_GEOMETRY);
    ftb.addAttribute(String.class).setName("b");
    FeatureType featureType = ftb.build();
    File tempFile = getTempFile();
    ShapefileFeatureStore shapefileFeatureStore = new ShapefileFeatureStore(tempFile.toURI());
    shapefileFeatureStore.createFeatureType(featureType);
    FeatureWriter featureWriter = shapefileFeatureStore.getFeatureWriter(new Query(shapefileFeatureStore.getName()));
    // don't add any features to the data store....
    // this should open a shapefile with no records. Not sure about the
    // semantics of this,
    // but it's meant to be used in the context of a FeatureCollection
    // iteration,
    // where the FeatureCollection<SimpleFeatureType, SimpleFeature> has nothing in it.
    featureWriter.close();
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) File(java.io.File) Test(org.junit.Test)

Example 13 with FeatureWriter

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

the class ShapefileDataStoreTest method writeFeatures.

private void writeFeatures(final ShapefileFeatureStore s, final Collection<Feature> fc) throws Exception {
    final FeatureType sft = fc.iterator().next().getType();
    s.createFeatureType(sft);
    try (FeatureWriter fw = s.getFeatureWriter(new Query(sft.getName()))) {
        Iterator<Feature> it = fc.iterator();
        while (it.hasNext()) {
            Feature feature = it.next();
            Feature newFeature = fw.next();
            FeatureExt.copy(feature, newFeature, false);
            fw.write();
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) Feature(org.opengis.feature.Feature)

Example 14 with FeatureWriter

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

the class ShapefileDataStoreTest method testRemoveFromBackAndClose.

/**
 * Create a test file, then continue removing the last entry until there are
 * no features left.
 */
@Test
public void testRemoveFromBackAndClose() throws Throwable {
    ShapefileFeatureStore sds = createDataStore();
    int idx = loadFeatures(sds).size();
    while (idx > 0) {
        FeatureWriter writer = null;
        try {
            writer = sds.getFeatureWriter(new Query(sds.getName()));
            while (writer.hasNext()) {
                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 15 with FeatureWriter

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

the class IndexedShapefileDataStoreTest method testRemoveFromFrontAndClose.

/**
 * Create a test file, then continue removing the first entry until there
 * are no features left.
 */
@Test
public void testRemoveFromFrontAndClose() 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());
    }
    sds.close();
}
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)

Aggregations

FeatureWriter (org.geotoolkit.storage.feature.FeatureWriter)18 Query (org.geotoolkit.storage.feature.query.Query)13 Test (org.junit.Test)11 Feature (org.opengis.feature.Feature)7 FeatureType (org.opengis.feature.FeatureType)7 DataStoreException (org.apache.sis.storage.DataStoreException)6 Point (org.locationtech.jts.geom.Point)6 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)4 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)4 IOException (java.io.IOException)3 File (java.io.File)2 Map (java.util.Map)2 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)2 FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)2 Filter (org.opengis.filter.Filter)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1 InputStream (java.io.InputStream)1