@SuppressWarnings("unchecked")
protected List loadFeatureCollections(WMSMapContext map) throws IOException {
ReferencedEnvelope mapArea = map.getAreaOfInterest();
CoordinateReferenceSystem wgs84 = null;
FilterFactory ff = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
try {
// this should never throw an exception, but we have to deal with it anyways
wgs84 = CRS.decode("EPSG:4326");
} catch(Exception e) {
throw (IOException) (new IOException("Unable to decode WGS84...").initCause(e));
}
List featureCollections = new ArrayList();
for (int i = 0; i < map.getLayerCount(); i++) {
MapLayer layer = map.getLayer(i);
DefaultQuery query = new DefaultQuery(layer.getQuery());
FeatureCollection<SimpleFeatureType, SimpleFeature> features = null;
try {
FeatureSource<SimpleFeatureType, SimpleFeature> source;
source = (FeatureSource<SimpleFeatureType, SimpleFeature>) layer.getFeatureSource();
GeometryDescriptor gd = source.getSchema().getGeometryDescriptor();
if(gd == null) {
// geometryless layers...
features = source.getFeatures(query);
} else {
// make sure we are querying the source with the bbox in the right CRS, if
// not, reproject the bbox
ReferencedEnvelope env = new ReferencedEnvelope(mapArea);
CoordinateReferenceSystem sourceCRS = gd.getCoordinateReferenceSystem();
if(sourceCRS != null &&
!CRS.equalsIgnoreMetadata(mapArea.getCoordinateReferenceSystem(), sourceCRS)) {
env = env.transform(sourceCRS, true);
}
// build the mixed query
Filter original = query.getFilter();
Filter bbox = ff.bbox(gd.getLocalName(), env.getMinX(), env.getMinY(), env.getMaxX(), env.getMaxY(), null);
query.setFilter(ff.and(original, bbox));
// query and eventually reproject
features = source.getFeatures(query);
if(sourceCRS != null && !CRS.equalsIgnoreMetadata(wgs84, sourceCRS)) {
ReprojectingFeatureCollection coll = new ReprojectingFeatureCollection(features, wgs84);