final int lowerDim = Math.min(geometry.axisDimensionX, geometry.axisDimensionY);
final int upperDim = Math.max(geometry.axisDimensionX, geometry.axisDimensionY)+1;
final int sourceDim = sourceCRS.getCoordinateSystem().getDimension();
if (upperDim-lowerDim != srcCrs2D.getCoordinateSystem().getDimension()) {
// TODO: localize
throw new InvalidGridGeometryException("Unsupported CRS: "+sourceCRS.getName().getCode());
}
final CoordinateReferenceSystem headCRS = getSubCRS(sourceCRS, 0, lowerDim);
final CoordinateReferenceSystem tailCRS = getSubCRS(sourceCRS, upperDim, sourceDim);
CoordinateReferenceSystem[] components = new CoordinateReferenceSystem[3];
int count = 0;
if (headCRS != null) components[count++] = headCRS;
components[count++] = crs2D;
if (tailCRS != null) components[count++] = tailCRS;
components = XArray.resize(components, count);
if (count == 1) {
targetCRS = components[0];
} else try {
targetCRS = ReferencingFactoryFinder.getCRSFactory(hints).createCompoundCRS(
Collections.singletonMap(IdentifiedObject.NAME_KEY,
crs2D.getName().getCode()), components);
} catch (FactoryException exception) {
throw new CannotReprojectException(exception.getLocalizedMessage(), exception);
}
}
/*
* Constructs the 'gridToCRS' transform in the same way than the CRS:
* leading and trailing dimensions (if any) are preserved.
*/
final MathTransform toSource2D = geometry.getGridToCRS2D();
final MathTransform toSource = geometry.getGridToCRS();
MathTransform toTarget;
if (CRS.equalsIgnoreMetadata(gridToCrs2D, toSource2D)) {
toTarget = toSource;
} else {
/*
* Replaces the 2D part in the source MT, while preserving the leading and
* trailing MT (if any). This is similar to the 'lowerDim' and 'upperDim'
* variables in the CRS case above, except that we operate on "grid" space
* rather than "axis" spaces. The index are usually the same, but not always.
*/
final int lowerDim = Math.min(geometry.gridDimensionX, geometry.gridDimensionY);
final int upperDim = Math.max(geometry.gridDimensionX, geometry.gridDimensionY)+1;
final int sourceDim = toSource.getSourceDimensions();
if (upperDim-lowerDim != toSource2D.getSourceDimensions()) {
// TODO: localize
throw new InvalidGridGeometryException("Unsupported math transform.");
}
final MathTransformFactory factory = ReferencingFactoryFinder.getMathTransformFactory(hints);
final DimensionFilter filter = new DimensionFilter(factory);
toTarget = gridToCrs2D;
try {