Search in sources :

Example 46 with FeatureIterator

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

the class IndexedShapefileDataStoreTest method runWriteReadTest.

private void runWriteReadTest(final Geometry geom, final boolean d3) throws Exception {
    // make features
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("Junk");
    ftb.addAttribute(geom.getClass()).setName("a").setCRS(CommonCRS.WGS84.normalizedGeographic());
    final FeatureType type = ftb.build();
    final Collection<Feature> features = new ArrayList<>();
    for (int i = 0, ii = 20; i < ii; i++) {
        Feature feature = type.newInstance();
        feature.setPropertyValue("a", geom.clone());
        features.add(feature);
    }
    // set up file
    File tmpFile = getTempFile();
    tmpFile.delete();
    // write features
    IndexedShapefileFeatureStore s = new IndexedShapefileFeatureStore(tmpFile.toURI());
    s.createFeatureType(type);
    writeFeatures(s, features);
    s.close();
    // read features
    s = new IndexedShapefileFeatureStore(tmpFile.toURI());
    FeatureCollection fc = loadFeatures(s);
    FeatureIterator fci = fc.iterator();
    // verify
    try {
        while (fci.hasNext()) {
            Feature f = fci.next();
            Geometry fromShape = FeatureExt.getDefaultGeometryValue(f).filter(Geometry.class::isInstance).map(Geometry.class::cast).orElseThrow(() -> new IllegalArgumentException("No geometry found in feature " + f));
            if (fromShape instanceof GeometryCollection) {
                if (!(geom instanceof GeometryCollection)) {
                    fromShape = ((GeometryCollection) fromShape).getGeometryN(0);
                }
            }
            try {
                Coordinate[] c1 = geom.getCoordinates();
                Coordinate[] c2 = fromShape.getCoordinates();
                for (int cc = 0, ccc = c1.length; cc < ccc; cc++) {
                    if (d3) {
                        assertTrue(c1[cc].equals3D(c2[cc]));
                    } else {
                        assertTrue(c1[cc].equals2D(c2[cc]));
                    }
                }
            } catch (Throwable t) {
                fail("Bogus : " + Arrays.asList(geom.getCoordinates()) + " : " + Arrays.asList(fromShape.getCoordinates()));
            }
        }
    } finally {
        fci.close();
        s.close();
        tmpFile.delete();
    }
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) Point(org.locationtech.jts.geom.Point) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Geometry(org.locationtech.jts.geom.Geometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Coordinate(org.locationtech.jts.geom.Coordinate) File(java.io.File)

Example 47 with FeatureIterator

use of org.geotoolkit.storage.feature.FeatureIterator 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 48 with FeatureIterator

use of org.geotoolkit.storage.feature.FeatureIterator 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 49 with FeatureIterator

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

the class FidQueryTest method assertFidsMatch.

private void assertFidsMatch() throws IOException, DataStoreException {
    int i = 0;
    for (Iterator<Entry<String, Feature>> iter = fids.entrySet().iterator(); iter.hasNext(); ) {
        i++;
        final Entry<String, Feature> entry = iter.next();
        final String fid = (String) entry.getKey();
        final Filter filter = fac.resourceId(fid);
        final Query query = Query.filtered(name.toString(), filter);
        final FeatureIterator features = ds.getFeatureReader(query);
        try {
            final Feature feature = features.next();
            assertFalse(features.hasNext());
            assertEquals(i + "th feature", entry.getValue(), feature);
        } finally {
            if (features != null)
                features.close();
        }
    }
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Entry(java.util.Map.Entry) Query(org.geotoolkit.storage.feature.query.Query) Filter(org.opengis.filter.Filter) Feature(org.opengis.feature.Feature)

Example 50 with FeatureIterator

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

the class FidQueryTest method testFIDBBoxQuery.

@Test
public void testFIDBBoxQuery() throws Exception {
    FeatureIterator features = ds.getFeatureReader(new Query(name));
    Feature feature;
    try {
        feature = features.next();
        feature = features.next();
        feature = features.next();
    } finally {
        if (features != null)
            features.close();
    }
    // FilterFactory factory = FilterFactoryFinder.createFilterFactory();
    // BBoxExpression bb =
    // factory.createBBoxExpression(feature.getBounds());
    // 
    // GeometryFilter bboxFilter =
    // factory.createGeometryFilter(FilterType.GEOMETRY_INTERSECTS);
    // bboxFilter.addRightGeometry(bb);
    // 
    // String geom = ds.getSchema().getDefaultGeometryValue().getLocalName();
    // 
    // bboxFilter.addLeftGeometry(factory.createAttributeExpression(geom));
    SpatialOperator bbox = fac.bbox(fac.property(""), FeatureExt.getEnvelope(feature));
    features = ds.getFeatureReader(Query.filtered(name.toString(), bbox));
    try {
        while (features.hasNext()) {
            Feature newFeature = features.next();
            assertEquals(newFeature, fids.get(FeatureExt.getId(newFeature).getIdentifier()));
        }
    } finally {
        if (features != null)
            features.close();
    }
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Query(org.geotoolkit.storage.feature.query.Query) SpatialOperator(org.opengis.filter.SpatialOperator) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Aggregations

FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)53 Feature (org.opengis.feature.Feature)41 Test (org.junit.Test)34 Query (org.geotoolkit.storage.feature.query.Query)30 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)24 FeatureType (org.opengis.feature.FeatureType)20 ResourceId (org.opengis.filter.ResourceId)20 Coordinate (org.locationtech.jts.geom.Coordinate)11 Point (org.locationtech.jts.geom.Point)11 Session (org.geotoolkit.storage.feature.session.Session)10 Geometry (org.locationtech.jts.geom.Geometry)10 HashMap (java.util.HashMap)9 CheckCloseFeatureIterator (org.geotoolkit.storage.feature.CheckCloseFeatureIterator)9 ArrayList (java.util.ArrayList)8 Date (java.util.Date)7 NoSuchElementException (java.util.NoSuchElementException)7 Filter (org.opengis.filter.Filter)7 File (java.io.File)6 Version (org.geotoolkit.version.Version)6 VersionControl (org.geotoolkit.version.VersionControl)6