*/
private Set<String> computeFids(Tile tile, Connection conn)
throws Exception {
Set<String> parentFids = getUpwardFids(tile.getParent(), conn);
Set<String> currFids = new HashSet<String>();
FeatureIterator fi = null;
try {
// grab the features
FeatureSource fs = featureType.getFeatureSource(null,null);
GeometryDescriptor geom = fs.getSchema().getGeometryDescriptor();
CoordinateReferenceSystem nativeCrs = geom
.getCoordinateReferenceSystem();
ReferencedEnvelope nativeTileEnvelope = null;
if (!CRS.equalsIgnoreMetadata(WGS84, nativeCrs)) {
try {
nativeTileEnvelope = tile.getEnvelope().transform(nativeCrs, true);
} catch (ProjectionException pe) {
// the WGS84 envelope of the tile is too big for this project,
// let's intersect it with the declared lat/lon bounds then
LOGGER.log(Level.INFO, "Could not reproject the current tile bounds "
+ tile.getEnvelope() + " to the native SRS, intersecting with "
+ "the layer declared lat/lon bounds and retrying");
// let's compare against the declared data bounds then
ReferencedEnvelope llEnv = featureType.getLatLonBoundingBox();
Envelope reduced = tile.getEnvelope().intersection(llEnv);
if(reduced.isNull() || reduced.getWidth() == 0 || reduced.getHeight() == 0) {
// no overlap, no party, the tile will be empty
return Collections.emptySet();
}
// there is some overlap, let's try the reprojection again.
// if even this fails, the user has evidently setup the
// geographics bounds improperly
ReferencedEnvelope refRed = new ReferencedEnvelope(reduced,
tile.getEnvelope().getCoordinateReferenceSystem());
nativeTileEnvelope = refRed.transform(nativeCrs, true);
}
} else {
nativeTileEnvelope = tile.getEnvelope();
}
fi = getSortedFeatures(geom, tile.getEnvelope(), nativeTileEnvelope, conn);
// if the crs is not wgs84, we'll need to transform the point
MathTransform tx = null;
double[] coords = new double[2];
// scan counting how many fids we've collected
boolean first = true;
while (fi.hasNext() && currFids.size() < featuresPerTile) {
// grab the feature, skip it if it's already in a parent element
SimpleFeature f = (SimpleFeature) fi.next();
if (parentFids.contains(f.getID()))
continue;
// check the need for a transformation
if (first) {
first = false;
CoordinateReferenceSystem nativeCRS = f.getType()
.getCoordinateReferenceSystem();
featureType.getFeatureType().getCoordinateReferenceSystem();
if (nativeCRS != null
&& !CRS.equalsIgnoreMetadata(nativeCRS, WGS84)) {
tx = CRS.findMathTransform(nativeCRS, WGS84);
}
}
// see if the features is to be included in this tile
Point p = ((Geometry) f.getDefaultGeometry()).getCentroid();
coords[0] = p.getX();
coords[1] = p.getY();
if (tx != null)
tx.transform(coords, 0, coords, 0, 1);
if (tile.contains(coords[0], coords[1]))
currFids.add(f.getID());
}
} finally {
if (fi != null)
fi.close();
}
return currFids;
}