// - if it's a mosaic, limit the number of tiles we're going to read to one
// (with time and elevation there might be hundreds of superimposed tiles)
// - reading the GridCoverage subset
//
// /////////////////////////////////////////////////////////////////////
Format format = csinfo.getFormat();
final GridCoverage2D gc;
final ParameterValueGroup readParams = format.getReadParameters();
final Map parameters = CoverageUtils.getParametersKVP(readParams);
final int minX = originalRange.getLow(0);
final int minY = originalRange.getLow(1);
final int width = originalRange.getSpan(0);
final int height = originalRange.getSpan(1);
final int maxX = minX + (width <= 5 ? width : 5);
final int maxY = minY + (height <= 5 ? height : 5);
// we have to be sure that we are working against a valid grid range.
final GridEnvelope2D testRange = new GridEnvelope2D(minX, minY, maxX, maxY);
// build the corresponding envelope
final MathTransform gridToWorldCorner = reader.getOriginalGridToWorld(PixelInCell.CELL_CORNER);
final GeneralEnvelope testEnvelope = CRS.transform(gridToWorldCorner, new GeneralEnvelope(testRange.getBounds()));
testEnvelope.setCoordinateReferenceSystem(nativeCRS);
if (customParameters != null) {
parameters.putAll(customParameters);
}
// make sure mosaics with many superimposed tiles won't blow up with
// a "too many open files" exception
String maxAllowedTiles = ImageMosaicFormat.MAX_ALLOWED_TILES.getName().toString();
if (parameters.keySet().contains(maxAllowedTiles)) {
parameters.put(maxAllowedTiles, 1);
}
// Since the read sample image won't be greater than 5x5 pixels and we are limiting the
// number of granules to 1, we may do direct read instead of using JAI
String useJaiImageRead = ImageMosaicFormat.USE_JAI_IMAGEREAD.getName().toString();
if (parameters.keySet().contains(useJaiImageRead)) {
parameters.put(useJaiImageRead, false);
}
parameters.put(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString(), new GridGeometry2D(testRange, testEnvelope));
// try to read this coverage
gc = (GridCoverage2D) reader.read(CoverageUtils.getParameters(readParams, parameters, true));
if (gc == null) {
throw new Exception("Unable to acquire test coverage for format:" + format.getName());
}
// remove read grid geometry since it is request specific
parameters.remove(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString());
cinfo.getDimensions().addAll(getCoverageDimensions(gc.getSampleDimensions()));
String name = gc.getName().toString();
cinfo.setName(name);
cinfo.setNativeCoverageName(coverageName);
cinfo.setTitle(name);
cinfo.setDescription(new StringBuilder("Generated from ").append(format.getName()).toString());
// keywords
cinfo.getKeywords().add(new Keyword("WCS"));
cinfo.getKeywords().add(new Keyword(format.getName()));
cinfo.getKeywords().add(new Keyword(name));
// native format name
cinfo.setNativeFormat(format.getName());
cinfo.getMetadata().put("dirName", new StringBuilder(store.getName()).append("_").append(name).toString());
// request SRS's
if ((gc.getCoordinateReferenceSystem2D().getIdentifiers() != null)
&& !gc.getCoordinateReferenceSystem2D().getIdentifiers().isEmpty()) {
cinfo.getRequestSRS().add(((Identifier) gc.getCoordinateReferenceSystem2D().getIdentifiers().toArray()[0]).toString());
}
// response SRS's
if ((gc.getCoordinateReferenceSystem2D().getIdentifiers() != null)
&& !gc.getCoordinateReferenceSystem2D().getIdentifiers().isEmpty()) {
cinfo.getResponseSRS().add(((Identifier) gc.getCoordinateReferenceSystem2D().getIdentifiers().toArray()[0]).toString());
}
// supported formats
final List formats = CoverageStoreUtils.listDataFormats();
for (Iterator i = formats.iterator(); i.hasNext();) {
final Format fTmp = (Format) i.next();
final String fName = fTmp.getName();
if (fName.equalsIgnoreCase("WorldImage")) {
// TODO check if coverage can encode Format
cinfo.getSupportedFormats().add("GIF");
cinfo.getSupportedFormats().add("PNG");