use of org.geotoolkit.filter.binaryspatial.LooseBBox 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