// if no offsets has been specified we try to default on the
// native ones
if (offsets == null) {
if (!(gridToCRS instanceof AffineTransform2D)
&& !(gridToCRS instanceof IdentityTransform))
throw new WcsException(
"Internal error, the coverage we're playing with does not have an affine transform...");
if (gridToCRS instanceof IdentityTransform) {
if (gridCRS.getGridType().equals(GridType.GT2dSimpleGrid))
offsets = new Double[] { 1.0, 1.0 };
else
offsets = new Double[] { 1.0, 0.0, 0.0, 1.0 };
} else {
AffineTransform2D affine = (AffineTransform2D) gridToCRS;
if (gridCRS.getGridType().equals(GridType.GT2dSimpleGrid))
offsets = new Double[] { affine.getScaleX(), affine.getScaleY() };
else
offsets = new Double[] { affine.getScaleX(), affine.getShearX(),
affine.getShearY(), affine.getScaleY() };
}
}
// building the actual transform for the resulting grid geometry
AffineTransform tx;
if (gridCRS.getGridType().equals(GridType.GT2dSimpleGrid.getXmlConstant())) {
tx = new AffineTransform(offsets[0], 0, 0, offsets[1], origin[0], origin[1]);
} else {
tx = new AffineTransform(offsets[0], offsets[2], offsets[1], offsets[3],
origin[0], origin[1]);
}
gridToCRS = new AffineTransform2D(tx);
}
// now we have enough info to read the coverage, grab the parameters
// and add the grid geometry info
final Map parameters = CoverageUtils.getParametersKVP(reader.getFormat()
.getReadParameters());
final GeneralEnvelope intersected = new GeneralEnvelope(destinationEnvelopeInSourceCRS);
intersected.intersect(originalEnvelope);
final GridGeometry2D destinationGridGeometry =new GridGeometry2D(PixelInCell.CELL_CENTER, gridToCRS, intersected, null);
parameters.put(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString(),
destinationGridGeometry);
coverage = (GridCoverage2D) reader.read(CoverageUtils.getParameters(reader.getFormat()
.getReadParameters(), parameters, true));
if ((coverage == null) || !(coverage instanceof GridCoverage2D)) {
throw new IOException("The requested coverage could not be found.");
}
/**
* Band Select (works on just one field)
*/
GridCoverage2D bandSelectedCoverage = coverage;
String interpolationType = null;
if (request.getRangeSubset() != null) {
if (request.getRangeSubset().getFieldSubset().size() > 1) {
throw new WcsException("Multi field coverages are not supported yet");
}
FieldSubsetType field = (FieldSubsetType) request.getRangeSubset().getFieldSubset()
.get(0);
interpolationType = field.getInterpolationType();
// handle axis subset
if (field.getAxisSubset().size() > 1) {
throw new WcsException("Multi axis coverages are not supported yet");
}
if (field.getAxisSubset().size() == 1) {
// prepare a support structure to quickly get the band index
// of a
// key
List<CoverageDimensionInfo> dimensions = meta.getDimensions();
Map<String, Integer> dimensionMap = new HashMap<String, Integer>();
for (int i = 0; i < dimensions.size(); i++) {
String keyName = dimensions.get(i).getName().replace(' ', '_');
dimensionMap.put(keyName, i);
}
// extract the band indexes
AxisSubsetType axisSubset = (AxisSubsetType) field.getAxisSubset().get(0);
List keys = axisSubset.getKey();
int[] bands = new int[keys.size()];
for (int j = 0; j < bands.length; j++) {
final String key = (String) keys.get(j);
Integer index = dimensionMap.get(key);
if (index == null)
throw new WcsException("Unknown field/axis/key combination "
+ field.getIdentifier().getValue() + "/"
+ axisSubset.getIdentifier() + "/" + key);
bands[j] = index;
}
// finally execute the band select
try {
bandSelectedCoverage = (GridCoverage2D) WCSUtils
.bandSelect(coverage, bands);
} catch (WcsException e) {
throw new WcsException(e.getLocalizedMessage());
}
}
}
/**
* Checking for supported Interpolation Methods
*/
Interpolation interpolation = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
if (interpolationType != null) {
if (interpolationType.equalsIgnoreCase("bilinear")) {
interpolation = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
} else if (interpolationType.equalsIgnoreCase("bicubic")) {
interpolation = Interpolation.getInstance(Interpolation.INTERP_BICUBIC);
} else if (interpolationType.equalsIgnoreCase("nearest")) {
interpolation = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
}
}
/**
* Crop
*/
final GridCoverage2D croppedGridCoverage = WCSUtils.crop(bandSelectedCoverage,
(GeneralEnvelope) coverage.getEnvelope(), nativeCRS,
destinationEnvelopeInSourceCRS, Boolean.TRUE);
/**
* Scale
*/
final GridCoverage2D scaledCoverage = WCSUtils.scale(croppedGridCoverage,
destinationGridGeometry);
/**
* Reproject
*/
final GridCoverage2D reprojectedCoverage = WCSUtils.reproject(scaledCoverage,
nativeCRS, targetCRS, interpolation);
return new GridCoverage[] { reprojectedCoverage };
} catch (Exception e) {
if (e instanceof WcsException)
throw (WcsException) e;
else
throw new WcsException(e);
}
}