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());
}
}
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;
}
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);
}
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;
}
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);
}
Aggregations