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)) {