Search in sources :

Example 16 with Query

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

the class ShapefileDataStoreTest method testLoadAndVerify.

@Test
public void testLoadAndVerify() throws Exception {
    FeatureCollection features = loadFeatures(STATE_POP, new Query(NamesExt.create("statepop")));
    // FeatureCollection<SimpleFeatureType, SimpleFeature> features = loadFeaturesM2();
    int count = features.size();
    assertTrue("Have features", count > 0);
    // assertEquals("Number of Features loaded",49,features.size()); // FILE
    // (correct value)
    // assertEquals("Number of Features loaded",3, count); // JAR
    Feature firstFeature = firstFeature(features);
    FeatureType schema = firstFeature.getType();
    assertNotNull(FeatureExt.getDefaultGeometry(schema));
    assertEquals("Number of Attributes", 256, schema.getProperties(true).size());
    assertEquals("Value of statename is wrong", "Illinois", firstFeature.getPropertyValue("STATE_NAME"));
    assertEquals("Value of land area is wrong", 143986.61, ((Double) firstFeature.getPropertyValue("LAND_KM")).doubleValue(), 0.001);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Feature(org.opengis.feature.Feature) Point(org.locationtech.jts.geom.Point) Test(org.junit.Test)

Example 17 with Query

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

the class ShapefileDataStoreTest method testRemoveFromFrontAndClose.

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

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

the class ShapefileDataStoreTest 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 {
    ShapefileFeatureStore sds = createDataStore();
    loadFeatures(sds);
    FeatureWriter writer = null;
    try {
        writer = sds.getFeatureWriter(new Query(sds.getNames().iterator().next()));
        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();
}
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 19 with Query

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

the class ShapefileDataStoreTest method loadFeatures.

protected FeatureCollection loadFeatures(final String resource, Query query) throws Exception {
    assertNotNull(query);
    URL url = ShapeTestData.url(resource);
    ShapefileFeatureStore s = new ShapefileFeatureStore(url.toURI(), true, null);
    final Query builder = new Query();
    builder.copy(query);
    builder.setTypeName(s.getName());
    query = builder;
    return s.createSession(true).getFeatureCollection(query);
}
Also used : Query(org.geotoolkit.storage.feature.query.Query) URL(java.net.URL)

Example 20 with Query

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

the class ShapefileDataStoreTest method testRemoveFromFrontAndCloseTransaction.

/**
 * Create a test file, then continue removing the first entry until there
 * are no features left.
 */
@Test
public void testRemoveFromFrontAndCloseTransaction() throws Throwable {
    ShapefileFeatureStore sds = createDataStore();
    int idx = loadFeatures(sds).size();
    while (idx > 0) {
        FeatureWriter writer = null;
        try {
            writer = sds.getFeatureWriter(new Query(sds.getName()));
            // System.out.println("classe : " + writer.getClass());
            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

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