use of org.geotoolkit.storage.feature.FeatureReader in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method testGetReaderOptimizations.
/**
* Checks if feature reading optimizations still allow to execute the
* queries or not
*/
@Test
public void testGetReaderOptimizations() throws Exception {
URL url = ShapeTestData.url(STATE_POP);
ShapefileFeatureStore s = new ShapefileFeatureStore(url.toURI());
// attributes other than geometry can be ignored here
Query builder = new Query();
builder.setTypeName(s.getNames().iterator().next());
builder.setSelection(Filter.include());
builder.setProperties(new String[] { "the_geom" });
Query query = builder;
FeatureReader reader = s.getFeatureReader(query);
assertEquals(1, reader.getFeatureType().getProperties(true).size());
assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
// here too, the filter is using the geometry only
GeometryFactory gc = org.geotoolkit.geometry.jts.JTS.getFactory();
LinearRing ring = gc.createLinearRing(new Coordinate[] { new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(10, 10), new Coordinate(0, 10), new Coordinate(0, 0) });
Polygon polygon = gc.createPolygon(ring, null);
JTSEnvelope2D bounds = new JTSEnvelope2D(polygon.getEnvelopeInternal(), null);
Filter gf = ff.bbox(ff.property("the_geom"), bounds);
builder = new Query();
builder.setTypeName(s.getNames().iterator().next());
builder.setSelection(gf);
builder.setProperties(new String[] { "the_geom" });
query = builder;
reader.close();
reader = s.getFeatureReader(query);
assertEquals(1, reader.getFeatureType().getProperties(true).size());
assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
reader.close();
// here not, we need state_name in the feature type, so open the dbf
// file please
Filter cf = ff.equal(ff.property("STATE_NAME"), ff.literal("Illinois"));
builder = new Query();
builder.setTypeName(s.getNames().iterator().next());
builder.setSelection(cf);
builder.setProperties(new String[] { "the_geom" });
query = builder;
reader = s.getFeatureReader(query);
assertEquals(1, reader.getFeatureType().getProperties(true).size());
assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
reader.close();
}
use of org.geotoolkit.storage.feature.FeatureReader in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method testWriteReadBigNumbers.
@Test
public void testWriteReadBigNumbers() throws Exception {
// open feature type
final FeatureTypeBuilder ftb = new FeatureTypeBuilder().setName("junk");
ftb.addAttribute(Point.class).setName("a").addRole(AttributeRole.DEFAULT_GEOMETRY);
ftb.addAttribute(BigDecimal.class).setName("b");
ftb.addAttribute(BigInteger.class).setName("c");
FeatureType type = ftb.build();
Collection<Feature> features = new ArrayList<>();
BigInteger bigInteger = new BigInteger("1234567890123456789");
BigDecimal bigDecimal = new BigDecimal(bigInteger, 2);
final Feature feature = type.newInstance();
feature.setPropertyValue("a", org.geotoolkit.geometry.jts.JTS.getFactory().createPoint(new Coordinate(1, -1)));
feature.setPropertyValue("b", bigDecimal);
feature.setPropertyValue("c", bigInteger);
features.add(feature);
// store features
File tmpFile = getTempFile();
tmpFile.createNewFile();
ShapefileFeatureStore s = new ShapefileFeatureStore(tmpFile.toURI());
writeFeatures(s, features);
try (// read them back
FeatureReader reader = s.getFeatureReader(new Query(type.getName()))) {
Feature f = reader.next();
assertEquals("big decimal", bigDecimal.doubleValue(), ((Number) f.getPropertyValue("b")).doubleValue(), 0.00001);
assertEquals("big integer", bigInteger.longValue(), ((Number) f.getPropertyValue("c")).longValue(), 0.00001);
}
}
use of org.geotoolkit.storage.feature.FeatureReader in project geotoolkit by Geomatys.
the class IndexedShapefileDataStoreTest method count.
private int count(final FeatureStore ds, final String typeName, final Filter filter) throws Exception {
FeatureReader reader;
reader = ds.getFeatureReader(Query.filtered(typeName, filter));
int count = 0;
try {
while (reader.hasNext()) {
reader.next();
count++;
}
} finally {
reader.close();
}
return count;
}
use of org.geotoolkit.storage.feature.FeatureReader in project geotoolkit by Geomatys.
the class IndexedShapefileFeatureStore method getFeatureReader.
/**
* Use the spatial index if available and adds a small optimization: if no
* attributes are going to be read, don't uselessly open and read the dbf
* file.
*/
@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();
// check if we must read the 3d values
final boolean read3D = true;
// find the properties we will read and return --------------------------
final AttributeType idAttribute = (AttributeType) baseType.getProperty(AttributeConvention.IDENTIFIER);
Set<AttributeType> readProperties;
Set<PropertyType> returnedProperties;
if (queryPropertyNames == null) {
// return all properties. Note : preserve order by using a linked set implementation
readProperties = new LinkedHashSet<>(getAttributes(baseType, true));
returnedProperties = new LinkedHashSet<>((Collection) baseType.getProperties(true));
} else {
// return only a subset of properties. Note : preserve order by using a linked set implementation
readProperties = new LinkedHashSet<>(queryPropertyNames.length);
returnedProperties = new LinkedHashSet<>(queryPropertyNames.length);
for (String n : queryPropertyNames) {
final PropertyType cdt = baseType.getProperty(n);
if (cdt instanceof AttributeType) {
readProperties.add((AttributeType) cdt);
} else if (cdt instanceof AbstractOperation) {
final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
}
returnedProperties.add(cdt);
}
// add filter properties
final FilterAttributeExtractor fae = new FilterAttributeExtractor();
fae.visit(queryFilter, null);
final Set<GenericName> filterPropertyNames = fae.getAttributeNameSet();
for (GenericName n : filterPropertyNames) {
final PropertyType cdt = baseType.getProperty(n.toString());
if (cdt instanceof AttributeType) {
readProperties.add((AttributeType) cdt);
} else if (cdt instanceof AbstractOperation) {
final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
}
}
}
final Set<PropertyType> allProperties = new LinkedHashSet<>(returnedProperties);
allProperties.addAll(readProperties);
// create a reader ------------------------------------------------------
final FeatureType readType;
final FeatureReader reader;
try {
final GenericName[] readPropertyNames = new GenericName[allProperties.size()];
int i = 0;
for (PropertyType prop : allProperties) {
readPropertyNames[i++] = prop.getName();
}
readType = FeatureTypeExt.createSubType(baseType, readPropertyNames);
if (queryFilter.getOperatorType() == SpatialOperatorName.BBOX) {
// in case we have a BBOX filter only, which is very commun, we can speed
// the process by relying on the quadtree estimations
final Envelope bbox = ExtractBoundsFilterVisitor.bbox(queryFilter, null);
final boolean loose = (queryFilter instanceof LooseBBox);
queryFilter = Filter.include();
final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
attsProperties.remove(idAttribute);
reader = createFeatureReader(getBBoxAttributesReader(attsProperties, bbox, loose, queryHints, read3D, queryRes), readType, queryHints);
} else if (queryFilter instanceof ResourceId && ((ResourceId) queryFilter).getIdentifier() == null) {
// in case we have an empty id set (TODO: should never happen, maybe we should remove this case).
return FeatureStreams.emptyReader(getFeatureType());
} else {
final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
attsProperties.remove(idAttribute);
reader = createFeatureReader(getAttributesReader(attsProperties, queryFilter, read3D, queryRes), readType, queryHints);
}
} catch (IOException ex) {
throw new DataStoreException(ex);
}
// handle remaining query parameters ------------------------------------
final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query(queryTypeName);
if (readProperties.equals(returnedProperties)) {
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);
}
Aggregations