File info = new File(coverageDir, "info.xml");
info.createNewFile();
// let's grab the necessary metadata
AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(coverageFile);
GridCoverage2DReader reader;
try {
reader = (GridCoverage2DReader) format.getReader(coverageFile);
} catch (Exception e) {
String message = "Exception while trying to read " + coverageFile.getCanonicalPath() + " with format" + format.getName();
throw new RuntimeException(message, e);
}
if (reader == null) {
throw new RuntimeException("No reader for " + coverageFile.getCanonicalPath() + " with format " + format.getName());
}
// basic info
FileWriter writer = new FileWriter(info);
writer.write("<coverage format=\"" + coverage + "\">\n");
writer.write("<name>" + coverage + "</name>\n");
writer.write("<label>" + coverage + "</label>\n");
writer.write("<description>" + coverage + " description</description>\n");
writer.write("<metadataLink about = \"http://www.remotesensing.org:16080/websites/geotiff/geotiff.html\" metadataType = \"other\" />");
writer.write("<keywords>WCS," + coverage + " </keywords>\n");
if(styleName == null)
styleName = "raster";
writer.write("<styles default=\"" + styleName + "\"/>\n");
// envelope
CoordinateReferenceSystem crs = reader.getCoordinateReferenceSystem();
GeneralEnvelope envelope = reader.getOriginalEnvelope();
GeneralEnvelope wgs84envelope = CoverageStoreUtils.getWGS84LonLatEnvelope(envelope);
final String nativeCrsName = CRS.lookupIdentifier(crs, false);
writer.write("<envelope crs=\"" + crs.toString().replaceAll("\"", "'") + "\" srsName=\"" + nativeCrsName + "\">\n");
writer.write("<pos>" + wgs84envelope.getMinimum(0) + " " + wgs84envelope.getMinimum(1) + "</pos>\n");
writer.write("<pos>" + wgs84envelope.getMaximum(0) + " " + wgs84envelope.getMaximum(1) + "</pos>\n");
writer.write("</envelope>\n");
/**
* Now reading a fake small GridCoverage just to retrieve meta information:
* - calculating a new envelope which is 1/20 of the original one
* - reading the GridCoverage subset
*/
final ParameterValueGroup readParams = reader.getFormat().getReadParameters();
final Map parameters = CoverageUtils.getParametersKVP(readParams);
double[] minCP = envelope.getLowerCorner().getCoordinate();
double[] maxCP = new double[] {
minCP[0] + (envelope.getSpan(0) / 20.0),
minCP[1] + (envelope.getSpan(1) / 20.0)
};
final GeneralEnvelope subEnvelope = new GeneralEnvelope(minCP, maxCP);
subEnvelope.setCoordinateReferenceSystem(reader.getCoordinateReferenceSystem());
parameters.put(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString(),
new GridGeometry2D(reader.getOriginalGridRange(), subEnvelope));
GridCoverage2D gc = (GridCoverage2D) reader.read(CoverageUtils.getParameters(readParams, parameters,
true));
// grid geometry
final GridGeometry geometry = gc.getGridGeometry();
final int dimensions = geometry.getGridRange().getDimension();