//
// - calculating a new envelope which is 1/20 of the original one
// - 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);
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());
}
cinfo.getDimensions().addAll( getCoverageDimensions(gc.getSampleDimensions()));
//TODO:
//dimentionNames = getDimensionNames(gc);
/*
StringBuilder cvName =null;
int count = 0;
while (true) {
final StringBuilder key = new StringBuilder(gc.getName().toString());
if (count > 0) {
key.append("_").append(count);
}
Map coverages = dataConfig.getCoverages();
Set cvKeySet = coverages.keySet();
boolean key_exists = false;
for (Iterator it = cvKeySet.iterator(); it.hasNext();) {
String cvKey = ((String) it.next()).toLowerCase();
if (cvKey.endsWith(key.toString().toLowerCase())) {
key_exists = true;
}
}
if (!key_exists) {
cvName = key;
break;
} else {
count++;
}
}
String name = cvName.toString();
*/
String name = gc.getName().toString();
cinfo.setName(name);
cinfo.setTitle(name);
cinfo.setDescription(new StringBuffer("Generated from ").append(format.getName()).toString() );
//keywords
cinfo.getKeywords().add("WCS");
cinfo.getKeywords().add(format.getName());
cinfo.getKeywords().add(name);
//native format name
cinfo.setNativeFormat(format.getName());
cinfo.getMetadata().put( "dirName", new StringBuffer(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");