private SimpleFeatureCollection join(SimpleFeatureCollection inputCollection,
CoverageBand band, final String coverageName) throws IOException {
// TODO Improve this by doing batch join and storing the result into memory
final DefaultProgressListener listener = new DefaultProgressListener();
final ListFeatureCollection collection = new ListFeatureCollection(schema);
// Getting attributes structure to be filled
inputCollection.accepts(new AbstractFeatureVisitor() {
public void visit(Feature feature) {
if (feature instanceof SimpleFeature) {
// get the feature
final SimpleFeature sourceFeature = (SimpleFeature) feature;
Collection<Property> props = sourceFeature.getProperties();
Name propName = null;
Object propValue = null;
// Assigning value to dest feature for matching attributes
Filter filter = null;
for (Property prop : props) {
propName = prop.getName();
if (
!propName.getLocalPart().equalsIgnoreCase("imageIndex")
&& !propName.getLocalPart().equalsIgnoreCase("the_geom")
&& !propName.getLocalPart().equalsIgnoreCase("location")) {
propValue = prop.getValue();
Filter updatedFilter = Utils.FF.equal(Utils.FF.property(propName),
Utils.FF.literal(propValue), true);
if (filter == null) {
filter = updatedFilter;
} else {
filter = FF.and(filter, updatedFilter);
}
}
}
Query query = new Query();
query.setFilter(filter);
SimpleFeatureCollection coverageCollection;
try {
coverageCollection = reader.getGranules(coverageName, readOnly)
.getGranules(query);
coverageCollection.accepts(new AbstractFeatureVisitor() {
public void visit(Feature feature) {
if (feature instanceof SimpleFeature) {
// get the feature
final SimpleFeature destFeature = DataUtilities
.template(schema);
Collection<Property> props = destFeature.getProperties();
Name propName = null;
Object propValue = null;
// Assigning value to dest feature for matching attributes
for (Property prop : props) {
propName = prop.getName();
propValue = ((SimpleFeature) feature)
.getAttribute(propName);
// Matching attributes are set
destFeature.setAttribute(propName, propValue);
}
collection.add(destFeature);
// check if something bad occurred
if (listener.isCanceled() || listener.hasExceptions()) {
if (listener.hasExceptions())
throw new RuntimeException(listener.getExceptions()
.peek());
else
throw new IllegalStateException(
"Feature visitor has been canceled");
}
}
}
}, listener);
} catch (IOException e) {
LOGGER.log(Level.FINER, e.getMessage(), e);
} catch (UnsupportedOperationException e) {
LOGGER.log(Level.FINER, e.getMessage(), e);
}
// check if something bad occurred
if (listener.isCanceled() || listener.hasExceptions()) {
if (listener.hasExceptions())
throw new RuntimeException(listener.getExceptions().peek());
else
throw new IllegalStateException("Feature visitor has been canceled");
}
}
}