Search in sources :

Example 71 with Query

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

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

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

the class ShapefileQuadTreeReadWriteTest method testGetBoundsQuery.

/**
 * Test optimized getBounds(). Testing when filter is a bbox filter and a fidfilter
 *
 * @throws Exception
 */
@Test
// fails randomly, urgent need to write shapefile store in SIS
@Ignore
public void testGetBoundsQuery() throws Exception {
    File file = copyShapefiles("shapes/streams.shp");
    ShapefileProvider fac = new ShapefileProvider();
    final ParameterValueGroup params = fac.getOpenParameters().createValue();
    params.parameter(ShapefileProvider.LOCATION).setValue(file.toURI());
    params.parameter(ShapefileProvider.CREATE_SPATIAL_INDEX.getName().toString()).setValue(Boolean.TRUE);
    IndexedShapefileFeatureStore ds = (IndexedShapefileFeatureStore) fac.open(params);
    FilterFactory2 ff = FilterUtilities.FF;
    ResourceId filter = ff.resourceId("streams.84");
    FeatureIterator iter = ds.getFeatureReader(Query.filtered(ds.getName().toString(), filter));
    JTSEnvelope2D bounds;
    try {
        bounds = new JTSEnvelope2D(FeatureExt.getEnvelope(iter.next()));
    } finally {
        iter.close();
    }
    Query query = Query.filtered(ds.getNames().iterator().next().toString(), filter);
    Envelope result = (Envelope) ds.getEnvelope(query);
    assertTrue(result.equals(bounds));
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) Query(org.geotoolkit.storage.feature.query.Query) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ResourceId(org.opengis.filter.ResourceId) Envelope(org.locationtech.jts.geom.Envelope) File(java.io.File) FilterFactory2(org.geotoolkit.filter.FilterFactory2) ShapefileProvider(org.geotoolkit.data.shapefile.ShapefileProvider) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 74 with Query

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

the class ShapefileQuadTreeReadWriteTest method doubleWrite.

private void doubleWrite(final FeatureType type, final FeatureCollection one, final File tmp, final ShapefileProvider maker, final boolean memorymapped) throws IOException, MalformedURLException, DataStoreException, URISyntaxException {
    FeatureStore s;
    s = createDataStore(maker, tmp.toURI().toURL(), memorymapped);
    s.createFeatureType(type);
    Session session = s.createSession(true);
    session.addFeatures(type.getName().toString(), one);
    session.addFeatures(type.getName().toString(), one);
    session.commit();
    s = createDataStore(maker, tmp.toURI().toURL(), true);
    assertEquals(one.size() * 2, s.getCount(new Query(s.getNames().iterator().next())));
}
Also used : Query(org.geotoolkit.storage.feature.query.Query) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) Session(org.geotoolkit.storage.feature.session.Session)

Example 75 with Query

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

the class ShapefileRTreeReadWriteTest method test.

void test(final String f) throws Exception {
    // Work on File rather than URL from JAR.
    File file = copyShapefiles(f);
    IndexedShapefileFeatureStore s = new IndexedShapefileFeatureStore(file.toURI());
    FeatureType type = s.getFeatureType();
    FeatureCollection one = s.createSession(true).getFeatureCollection(new Query(type.getName()));
    test(type, one, getTempFile(), false);
    test(type, one, getTempFile(), true);
    s.close();
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) File(java.io.File)

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