Search in sources :

Example 51 with Query

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

the class ShapefileDataStoreTest method testGetCount.

@Test
public void testGetCount() throws Exception {
    assertTrue(copyShapefiles(STREAM).canRead());
    // The following test seems to fail in the URL point into the JAR file.
    ShapefileFeatureStore store = (ShapefileFeatureStore) new ShapefileProvider().createDataStore(TestData.url(AbstractTestCaseSupport.class, STREAM).toURI());
    int count = 0;
    try (FeatureReader reader = store.getFeatureReader(new Query(store.getNames().iterator().next()))) {
        while (reader.hasNext()) {
            count++;
            reader.next();
        }
        assertEquals(count, store.getCount(new Query(store.getNames().iterator().next())));
    }
}
Also used : Query(org.geotoolkit.storage.feature.query.Query) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Point(org.locationtech.jts.geom.Point) Test(org.junit.Test)

Example 52 with Query

use of org.geotoolkit.storage.feature.query.Query 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 53 with Query

use of org.geotoolkit.storage.feature.query.Query 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 54 with Query

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

the class ShapefileDataStoreTest method testGetReaderOptimizations.

/**
 * Checks if feature reading optimizations still allow to execute the
 * queries or not
 */
@Test
public void testGetReaderOptimizations() throws Exception {
    URL url = ShapeTestData.url(STATE_POP);
    ShapefileFeatureStore s = new ShapefileFeatureStore(url.toURI());
    // attributes other than geometry can be ignored here
    Query builder = new Query();
    builder.setTypeName(s.getNames().iterator().next());
    builder.setSelection(Filter.include());
    builder.setProperties(new String[] { "the_geom" });
    Query query = builder;
    FeatureReader reader = s.getFeatureReader(query);
    assertEquals(1, reader.getFeatureType().getProperties(true).size());
    assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
    // here too, the filter is using the geometry only
    GeometryFactory gc = org.geotoolkit.geometry.jts.JTS.getFactory();
    LinearRing ring = gc.createLinearRing(new Coordinate[] { new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(10, 10), new Coordinate(0, 10), new Coordinate(0, 0) });
    Polygon polygon = gc.createPolygon(ring, null);
    JTSEnvelope2D bounds = new JTSEnvelope2D(polygon.getEnvelopeInternal(), null);
    Filter gf = ff.bbox(ff.property("the_geom"), bounds);
    builder = new Query();
    builder.setTypeName(s.getNames().iterator().next());
    builder.setSelection(gf);
    builder.setProperties(new String[] { "the_geom" });
    query = builder;
    reader.close();
    reader = s.getFeatureReader(query);
    assertEquals(1, reader.getFeatureType().getProperties(true).size());
    assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
    reader.close();
    // here not, we need state_name in the feature type, so open the dbf
    // file please
    Filter cf = ff.equal(ff.property("STATE_NAME"), ff.literal("Illinois"));
    builder = new Query();
    builder.setTypeName(s.getNames().iterator().next());
    builder.setSelection(cf);
    builder.setProperties(new String[] { "the_geom" });
    query = builder;
    reader = s.getFeatureReader(query);
    assertEquals(1, reader.getFeatureType().getProperties(true).size());
    assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
    reader.close();
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotoolkit.storage.feature.query.Query) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) Coordinate(org.locationtech.jts.geom.Coordinate) Filter(org.opengis.filter.Filter) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) URL(java.net.URL) Test(org.junit.Test)

Example 55 with Query

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

the class ShapefileDataStoreTest method testWriteReadBigNumbers.

@Test
public void testWriteReadBigNumbers() throws Exception {
    // open feature type
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder().setName("junk");
    ftb.addAttribute(Point.class).setName("a").addRole(AttributeRole.DEFAULT_GEOMETRY);
    ftb.addAttribute(BigDecimal.class).setName("b");
    ftb.addAttribute(BigInteger.class).setName("c");
    FeatureType type = ftb.build();
    Collection<Feature> features = new ArrayList<>();
    BigInteger bigInteger = new BigInteger("1234567890123456789");
    BigDecimal bigDecimal = new BigDecimal(bigInteger, 2);
    final Feature feature = type.newInstance();
    feature.setPropertyValue("a", org.geotoolkit.geometry.jts.JTS.getFactory().createPoint(new Coordinate(1, -1)));
    feature.setPropertyValue("b", bigDecimal);
    feature.setPropertyValue("c", bigInteger);
    features.add(feature);
    // store features
    File tmpFile = getTempFile();
    tmpFile.createNewFile();
    ShapefileFeatureStore s = new ShapefileFeatureStore(tmpFile.toURI());
    writeFeatures(s, features);
    try (// read them back
    FeatureReader reader = s.getFeatureReader(new Query(type.getName()))) {
        Feature f = reader.next();
        assertEquals("big decimal", bigDecimal.doubleValue(), ((Number) f.getPropertyValue("b")).doubleValue(), 0.00001);
        assertEquals("big integer", bigInteger.longValue(), ((Number) f.getPropertyValue("c")).longValue(), 0.00001);
    }
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) Coordinate(org.locationtech.jts.geom.Coordinate) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) File(java.io.File) BigDecimal(java.math.BigDecimal) 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