Search in sources :

Example 16 with FeatureReader

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

the class FeatureStreamsTest method testRetypeFeatureIterator.

@Test
public void testRetypeFeatureIterator() throws DataStoreException {
    final FeatureCollection collection = buildSimpleFeatureCollection();
    final ViewMapper reducedType = new ViewMapper(collection.getType(), AttributeConvention.IDENTIFIER, "att_double");
    final Query query = new Query(collection.getType().getName());
    FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    FeatureReader retyped = FeatureStreams.decorate(reader, reducedType, null);
    assertEquals(reducedType.getMappedType(), retyped.getFeatureType());
    int mask = 0;
    Feature f;
    while (retyped.hasNext()) {
        f = retyped.next();
        final Object id = f.getPropertyValue(AttributeConvention.IDENTIFIER);
        assertEquals(2, f.getType().getProperties(true).size());
        if (id1.equals(id)) {
            mask |= 1 << 0;
            assertEquals(3d, (Double) f.getProperty("att_double").getValue(), DELTA);
        } else if (id2.equals(id)) {
            mask |= 1 << 1;
            assertEquals(1d, (Double) f.getProperty("att_double").getValue(), DELTA);
        } else if (id3.equals(id)) {
            mask |= 1 << 2;
            assertEquals(2d, (Double) f.getProperty("att_double").getValue(), DELTA);
        }
    }
    assertEquals("missing features in iterations", 7, mask);
    // check has next do not iterate
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    retyped = FeatureStreams.decorate(reader, reducedType, null);
    testIterationOnNext(retyped, 3);
    // check sub iterator is properly closed
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(reader);
    assertFalse(checkIte.isClosed());
    retyped = FeatureStreams.decorate(checkIte, reducedType, null);
    while (retyped.hasNext()) retyped.next();
    retyped.close();
    assertTrue(checkIte.isClosed());
}
Also used : Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) ViewMapper(org.geotoolkit.feature.ViewMapper) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) Point(org.locationtech.jts.geom.Point) CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) Test(org.junit.Test)

Example 17 with FeatureReader

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

the class GMLSparseStore method getType.

@Override
public synchronized FeatureType getType() throws DataStoreException {
    if (featureType == null) {
        final String xsd = parameters.getValue(GMLProvider.XSD);
        final String xsdTypeName = parameters.getValue(GMLProvider.XSD_TYPE_NAME);
        catalog = new GenericNameIndex();
        if (xsd != null) {
            // read types from XSD file
            final JAXBFeatureTypeReader reader = new JAXBFeatureTypeReader();
            try {
                catalog = reader.read(new URL(xsd));
                featureType = catalog.get(xsdTypeName);
            } catch (MalformedURLException | JAXBException ex) {
                throw new DataStoreException(ex.getMessage(), ex);
            }
        } else {
            final JAXPStreamFeatureReader reader = new JAXPStreamFeatureReader();
            reader.getProperties().put(JAXPStreamFeatureReader.LONGITUDE_FIRST, longitudeFirst);
            reader.getProperties().put(JAXPStreamFeatureReader.READ_EMBEDDED_FEATURE_TYPE, true);
            try {
                if (Files.isDirectory(file)) {
                    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(file, new PosixDirectoryFilter("*.gml", true))) {
                        final Iterator<Path> gmlPaths = directoryStream.iterator();
                        // get first gml file only
                        if (gmlPaths.hasNext()) {
                            final Path gmlPath = gmlPaths.next();
                            try (FeatureReader ite = reader.readAsStream(gmlPath)) {
                                catalog = reader.getFeatureTypes();
                                featureType = ite.getFeatureType();
                            }
                        }
                    }
                } else {
                    try (FeatureReader ite = reader.readAsStream(file)) {
                        catalog = reader.getFeatureTypes();
                        featureType = ite.getFeatureType();
                    }
                }
            } catch (IOException | XMLStreamException ex) {
                throw new DataStoreException(ex.getMessage(), ex);
            } finally {
                reader.dispose();
            }
        }
    }
    return featureType;
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) DataStoreException(org.apache.sis.storage.DataStoreException) PosixDirectoryFilter(org.geotoolkit.nio.PosixDirectoryFilter) JAXBException(javax.xml.bind.JAXBException) GenericNameIndex(org.geotoolkit.storage.feature.GenericNameIndex) IOException(java.io.IOException) URL(java.net.URL) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBFeatureTypeReader(org.geotoolkit.feature.xml.jaxb.JAXBFeatureTypeReader) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 18 with FeatureReader

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

the class GMLStore method getType.

@Override
public synchronized FeatureType getType() throws DataStoreException {
    if (rootType == null) {
        final String xsd = parameters.getValue(GMLProvider.XSD);
        final String xsdTypeName = parameters.getValue(GMLProvider.XSD_TYPE_NAME);
        catalog = new GenericNameIndex();
        if (xsd != null) {
            // read types from XSD file
            final JAXBFeatureTypeReader reader = new JAXBFeatureTypeReader();
            try {
                catalog = reader.read(new URL(xsd));
                rootType = catalog.get(xsdTypeName);
            // schemaLocations.put(reader.getTargetNamespace(),xsd); needed?
            } catch (MalformedURLException | JAXBException ex) {
                throw new DataStoreException(ex.getMessage(), ex);
            }
        } else {
            final JAXPStreamFeatureReader reader = new JAXPStreamFeatureReader();
            reader.getProperties().put(JAXPStreamFeatureReader.LONGITUDE_FIRST, longitudeFirst);
            reader.getProperties().put(JAXPStreamFeatureReader.READ_EMBEDDED_FEATURE_TYPE, true);
            try (FeatureReader ite = reader.readAsStream(file)) {
                catalog = reader.getFeatureTypes();
                rootType = ite.getFeatureType();
            } catch (IOException | XMLStreamException ex) {
                throw new DataStoreException(ex.getMessage(), ex);
            } finally {
                reader.dispose();
            }
        }
        featureType = getCollectionSubType(rootType);
    }
    return featureType;
}
Also used : MalformedURLException(java.net.MalformedURLException) DataStoreException(org.apache.sis.storage.DataStoreException) JAXBException(javax.xml.bind.JAXBException) GenericNameIndex(org.geotoolkit.storage.feature.GenericNameIndex) IOException(java.io.IOException) URL(java.net.URL) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBFeatureTypeReader(org.geotoolkit.feature.xml.jaxb.JAXBFeatureTypeReader) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 19 with FeatureReader

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

the class ShapefileFeatureStore method getFeatureReader.

/**
 * {@inheritDoc }
 */
@Override
public FeatureReader getFeatureReader(final Query query) throws DataStoreException {
    if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
        throw new UnsupportedQueryException();
    final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
    final FeatureType baseType = getFeatureType();
    final String queryTypeName = gquery.getTypeName();
    final String[] queryPropertyNames = gquery.getPropertyNames();
    final Hints queryHints = gquery.getHints();
    final double[] queryRes = gquery.getResolution();
    Filter queryFilter = gquery.getSelection();
    final String typeName = baseType.getName().tip().toString();
    // check if we must read the 3d values
    final boolean read3D = true;
    final ShapefileAttributeReader attReader = getAttributesReader(true, read3D, queryRes);
    final FeatureIDReader idReader = new DefaultFeatureIDReader(typeName);
    FeatureReader reader = ShapefileFeatureReader.create(attReader, idReader, baseType, queryHints);
    // handle remaining query parameters ------------------------------------
    final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query(queryTypeName);
    qb.setProperties(queryPropertyNames);
    qb.setSelection(queryFilter);
    qb.setHints(queryHints);
    qb.setSortBy(gquery.getSortBy());
    qb.setOffset(gquery.getOffset());
    gquery.getLimit().ifPresent(qb::setLimit);
    return FeatureStreams.subset(reader, qb);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) Hints(org.geotoolkit.factory.Hints) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) Filter(org.opengis.filter.Filter) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 20 with FeatureReader

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

Aggregations

FeatureReader (org.geotoolkit.storage.feature.FeatureReader)24 Test (org.junit.Test)11 FeatureType (org.opengis.feature.FeatureType)11 DataStoreException (org.apache.sis.storage.DataStoreException)10 Query (org.apache.sis.storage.Query)10 Feature (org.opengis.feature.Feature)9 Query (org.geotoolkit.storage.feature.query.Query)8 IOException (java.io.IOException)7 UnsupportedQueryException (org.apache.sis.storage.UnsupportedQueryException)7 Filter (org.opengis.filter.Filter)6 URL (java.net.URL)5 Hints (org.geotoolkit.factory.Hints)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 Point (org.locationtech.jts.geom.Point)5 ResourceId (org.opengis.filter.ResourceId)5 GenericName (org.opengis.util.GenericName)5 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)4 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)4 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3