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