Search in sources :

Example 11 with FeatureReader

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

the class CPGFileTest method testReadUTF8.

/**
 * Test reading a shapefile with an UTF-8 cpg file.
 *
 * @throws DataStoreException
 */
@Test
public void testReadUTF8() throws DataStoreException, MalformedURLException, URISyntaxException {
    final URL url = CPGFileTest.class.getResource("/org/geotoolkit/test-data/shapes/utf8.shp");
    final ShapefileFeatureStore store = new ShapefileFeatureStore(url.toURI());
    try (final FeatureReader reader = store.getFeatureReader(new Query(store.getName()))) {
        Assert.assertTrue(reader.hasNext());
        final Feature feature = reader.next();
        Assert.assertEquals("&éè\"'(-_çà)=@%$*:test", feature.getProperty("text").getValue());
    }
}
Also used : Query(org.geotoolkit.storage.feature.query.Query) ShapefileFeatureStore(org.geotoolkit.data.shapefile.ShapefileFeatureStore) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) URL(java.net.URL) Test(org.junit.Test)

Example 12 with FeatureReader

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

the class WFSFeatureSet method requestFeature.

private FeatureReader requestFeature(final QName typeName, final Query query) throws XMLStreamException, DataStoreException, IOException {
    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;
    FeatureType type = getType();
    // TODO : remove SIS conventions
    final GetFeatureRequest request = store.createGetFeature();
    request.setTypeName(typeName);
    /* We create a secondary query whose role is to handle mappings we won't
         * delegate to the WFS service. Examples are start offset, which cannot
         * be converted to a proper WFS parameter, and the asked reprojection (if
         * any), because it happens that WFS servers handle it badly.
         *
         */
    final org.geotoolkit.storage.feature.query.Query remainingQuery;
    if (gquery != null) {
        remainingQuery = new org.geotoolkit.storage.feature.query.Query();
        remainingQuery.copy(gquery);
        final Map<String, String> replacements = type.getProperties(true).stream().filter(pType -> pType instanceof AbstractOperation).map(pType -> new AbstractMap.SimpleEntry<>(pType.getName(), ((AbstractOperation) pType).getDependencies())).filter(entry -> entry.getValue().size() == 1).collect(Collectors.toMap(entry -> entry.getKey().toString(), entry -> entry.getValue().iterator().next()));
        String[] propertyNames = gquery.getPropertyNames();
        if (propertyNames != null) {
            propertyNames = Stream.of(propertyNames).map(pName -> replacements.getOrDefault(pName, pName)).toArray(size -> new String[size]);
        }
        type = FeatureTypeExt.createSubType(type, propertyNames);
        final Filter filter = gquery.getSelection();
        if (filter == null) {
            request.setFilter(Filter.include());
        } else {
            final Object visited = new PropertyNameReplacement(replacements).visit(filter);
            if (visited instanceof Filter) {
                request.setFilter((Filter) visited);
            } else {
                request.setFilter(filter);
            }
        }
        // Filter is already processed, but a query builder does not support null filter.
        remainingQuery.setSelection(Filter.include());
        final long start = gquery.getOffset();
        final long max = gquery.getLimit().orElse(-1);
        if (start <= 0 && max != -1) {
            request.setMaxFeatures((int) max);
        // For this one, do not remove from remaining queries : If the
        // wfs service does not manage it, we will do it afterwards.
        } else if (max > 0) {
            /* If an offset is provided, we'll have to skip elements manually,
                 * so we keep start index in remaining query. All we can do here
                 * is query that we want no more than the number of features
                 * needed to do the skip, + the number of features wanted by
                */
            request.setMaxFeatures((int) (start + max));
        }
        if (propertyNames != null) {
            final GenericName[] names = type.getProperties(true).stream().filter(pType -> !(pType instanceof Operation)).map(PropertyType::getName).toArray(size -> new GenericName[size]);
            request.setPropertyNames(names);
            remainingQuery.setProperties(null);
        }
    } else
        remainingQuery = null;
    final XmlFeatureReader reader = new JAXPStreamFeatureReader(type);
    reader.getProperties().put(JAXPStreamFeatureReader.SKIP_UNEXPECTED_PROPERTY_TAGS, true);
    final InputStream stream;
    if (store.getUsePost()) {
        store.getLogger().log(Level.INFO, "[WFS Client] request feature by POST.");
        stream = request.getResponseStream();
    } else {
        final URL url = request.getURL();
        store.getLogger().log(Level.INFO, "[WFS Client] request feature : {0}", url);
        stream = url.openStream();
    }
    FeatureReader streamReader = reader.readAsStream(stream);
    if (remainingQuery != null) {
        streamReader = FeatureStreams.subset(streamReader, remainingQuery);
    }
    return streamReader;
}
Also used : Arrays(java.util.Arrays) CRS(org.apache.sis.referencing.CRS) URL(java.net.URL) Spliterators(java.util.Spliterators) StoreListener(org.apache.sis.storage.event.StoreListener) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) UnaryOperator(java.util.function.UnaryOperator) DefaultDataIdentification(org.apache.sis.metadata.iso.identification.DefaultDataIdentification) Envelope(org.opengis.geometry.Envelope) FeatureTypeExt(org.geotoolkit.feature.FeatureTypeExt) FeatureType(org.opengis.feature.FeatureType) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) ValueReference(org.opengis.filter.ValueReference) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) FeatureStreams(org.geotoolkit.storage.feature.FeatureStreams) AbstractOperation(org.apache.sis.feature.AbstractOperation) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Feature(org.opengis.feature.Feature) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Predicate(java.util.function.Predicate) AxesConvention(org.apache.sis.referencing.cs.AxesConvention) AbstractCRS(org.apache.sis.referencing.crs.AbstractCRS) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) List(java.util.List) Stream(java.util.stream.Stream) NamesExt(org.geotoolkit.util.NamesExt) AttributeRole(org.apache.sis.feature.builder.AttributeRole) Optional(java.util.Optional) FeatureMapUpdate(org.geotoolkit.storage.FeatureMapUpdate) Geometry(org.locationtech.jts.geom.Geometry) QName(javax.xml.namespace.QName) Spliterator(java.util.Spliterator) Query(org.apache.sis.storage.Query) XmlFeatureReader(org.geotoolkit.feature.xml.XmlFeatureReader) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.util.FactoryException) GenericNameIndex(org.geotoolkit.storage.feature.GenericNameIndex) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader) JAXBFeatureTypeReader(org.geotoolkit.feature.xml.jaxb.JAXBFeatureTypeReader) BoundingBox(org.geotoolkit.ows.xml.BoundingBox) WFSCapabilities(org.geotoolkit.wfs.xml.WFSCapabilities) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) IllegalNameException(org.apache.sis.storage.IllegalNameException) Operation(org.opengis.feature.Operation) DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) StreamSupport(java.util.stream.StreamSupport) CommonCRS(org.apache.sis.referencing.CommonCRS) DataStoreException(org.apache.sis.storage.DataStoreException) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) Unmarshaller(javax.xml.bind.Unmarshaller) TransactionResponse(org.geotoolkit.wfs.xml.TransactionResponse) DuplicatingFilterVisitor(org.geotoolkit.filter.visitor.DuplicatingFilterVisitor) Iterator(java.util.Iterator) JAXBElement(javax.xml.bind.JAXBElement) DefaultMetadata(org.apache.sis.metadata.iso.DefaultMetadata) FunctionNames(org.apache.sis.internal.filter.FunctionNames) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Metadata(org.opengis.metadata.Metadata) IOException(java.io.IOException) StoreEvent(org.apache.sis.storage.event.StoreEvent) PropertyTypeBuilder(org.apache.sis.feature.builder.PropertyTypeBuilder) AtomicLong(java.util.concurrent.atomic.AtomicLong) NamedIdentifier(org.apache.sis.referencing.NamedIdentifier) AbstractMap(java.util.AbstractMap) GenericName(org.opengis.util.GenericName) PropertyType(org.opengis.feature.PropertyType) WFSMarshallerPool(org.geotoolkit.wfs.xml.WFSMarshallerPool) FeatureSet(org.apache.sis.storage.FeatureSet) Filter(org.opengis.filter.Filter) Collections(java.util.Collections) InputStream(java.io.InputStream) AttributeTypeBuilder(org.apache.sis.feature.builder.AttributeTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) AbstractOperation(org.apache.sis.feature.AbstractOperation) Operation(org.opengis.feature.Operation) URL(java.net.URL) AbstractMap(java.util.AbstractMap) GenericName(org.opengis.util.GenericName) XmlFeatureReader(org.geotoolkit.feature.xml.XmlFeatureReader) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) XmlFeatureReader(org.geotoolkit.feature.xml.XmlFeatureReader) InputStream(java.io.InputStream) AbstractOperation(org.apache.sis.feature.AbstractOperation) Filter(org.opengis.filter.Filter) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader)

Example 13 with FeatureReader

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

the class WFSFeatureSet method features.

@Override
public Stream<Feature> features(boolean parallel) throws DataStoreException {
    final FeatureType sft = getType();
    FeatureReader reader;
    final GenericName gName = sft.getName();
    final String namespace = NamesExt.getNamespace(gName);
    final QName q;
    if (namespace == null || namespace.isEmpty()) {
        q = new QName(gName.tip().toString());
    } else {
        q = new QName(namespace, gName.tip().toString(), prefixes.get(namespace));
    }
    try {
        reader = requestFeature(q, new org.geotoolkit.storage.feature.query.Query(sft.getName()));
    } catch (IOException | XMLStreamException ex) {
        throw new DataStoreException(ex);
    }
    final Stream<Feature> stream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(reader, Spliterator.ORDERED), false);
    return stream.onClose(reader::close);
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.apache.sis.storage.Query) QName(javax.xml.namespace.QName) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) GenericName(org.opengis.util.GenericName) XMLStreamException(javax.xml.stream.XMLStreamException) XmlFeatureReader(org.geotoolkit.feature.xml.XmlFeatureReader) JAXPStreamFeatureReader(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 14 with FeatureReader

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

the class MemoryFeatureStore method getAffectedFeatures.

private Collection<ResourceId> getAffectedFeatures(final String groupName, final Filter filter) throws DataStoreException {
    final Group grp = groups.get(this, groupName);
    final Collection<ResourceId> affected;
    if (filter instanceof ResourceId || filter.getOperatorType() == LogicalOperatorName.OR) {
        affected = identifiers(filter);
    } else {
        affected = new ArrayList<>();
        final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query(groupName);
        qb.setSelection(filter);
        // no properties, only ids
        qb.setProperties(new String[] { AttributeConvention.IDENTIFIER });
        final FeatureReader reader = getFeatureReader(qb);
        try {
            while (reader.hasNext()) {
                affected.add(FeatureExt.getId(reader.next()));
            }
        } finally {
            reader.close();
        }
    }
    return affected;
}
Also used : Query(org.apache.sis.storage.Query) ResourceId(org.opengis.filter.ResourceId) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 15 with FeatureReader

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

the class MemoryFeatureStore 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 Group grp = groups.get(this, gquery.getTypeName());
    if (grp == null) {
        throw new DataStoreException("No featureType for name : " + gquery.getTypeName());
    }
    // we can handle id filter
    Filter filter = gquery.getSelection();
    if (filter == null)
        filter = Filter.include();
    final org.geotoolkit.storage.feature.query.Query remaining = new org.geotoolkit.storage.feature.query.Query();
    remaining.copy(gquery);
    final Iterator<? extends Feature> ite;
    if (grp instanceof GroupWithId) {
        if (filter instanceof ResourceId) {
            ite = ((GroupWithId) grp).createIterator((ResourceId) filter);
            if (ite != null) {
                remaining.setSelection(Filter.include());
            }
        } else {
            ite = ((GroupWithId) grp).createIterator(null);
        }
    } else {
        ite = ((GroupNoId) grp).createIterator();
    }
    final FeatureReader reader;
    reader = FeatureStreams.asReader(ite, grp.getFeatureType());
    // todo we should handle at least spatial filter here by using a quadtree.
    return FeatureStreams.subset(reader, remaining);
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.apache.sis.storage.Query) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) Filter(org.opengis.filter.Filter) ResourceId(org.opengis.filter.ResourceId) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

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