Search in sources :

Example 16 with FeatureWriter

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

the class IndexedShapefileDataStoreTest method writeFeatures.

private void writeFeatures(final IndexedShapefileFeatureStore s, final Collection<Feature> fc) throws Exception {
    final FeatureType type = fc.iterator().next().getType();
    s.createFeatureType(type);
    final FeatureWriter fw = s.getFeatureWriter(new Query(type.getName()));
    final Iterator<Feature> it = fc.iterator();
    while (it.hasNext()) {
        Feature feature = it.next();
        Feature newFeature = fw.next();
        FeatureExt.copy(feature, newFeature, false);
        fw.write();
    }
    fw.close();
    assertEquals(20, s.getCount(new Query(type.getName())));
}
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 17 with FeatureWriter

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

the class IndexedShapefileDataStoreTest method testUpdating.

/**
 * Create a set of features, then remove every other one, updating the
 * remaining. Test for removal and proper update after reloading...
 */
@Test
public void testUpdating() throws Throwable {
    IndexedShapefileFeatureStore sds = createDataStore();
    loadFeatures(sds);
    FeatureWriter writer = null;
    try {
        writer = sds.getFeatureWriter(new Query(sds.getName()));
        while (writer.hasNext()) {
            Feature feat = writer.next();
            Byte b = (Byte) feat.getPropertyValue("b");
            if ((b.byteValue() % 2) == 0) {
                writer.remove();
            } else {
                feat.setPropertyValue("b", new Byte((byte) -1));
            }
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
    FeatureCollection fc = loadFeatures(sds);
    assertEquals(10, fc.size());
    FeatureIterator i = fc.iterator();
    for (; i.hasNext(); ) {
        assertEquals(-1, ((Byte) i.next().getPropertyValue("b")).byteValue());
    }
    i.close();
    sds.close();
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 18 with FeatureWriter

use of org.geotoolkit.storage.feature.FeatureWriter 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)

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