[geotk] Change the CRS of an existing GridCoverage2D

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Tue Mar 1 05:16:57 EST 2011

Hello Fabian

Sorry for the delay. I'm at the OGC meeting right now, which make me slower to 
answer emails.

Le 27/02/11 22:42, Fabien Carrion a écrit :
> With those two print I get
> crs GEOGCS["WGS 84",
>    DATUM["WGS_1984",
>      SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]],
>      AUTHORITY["EPSG","6326"]],
>    PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]],
>    UNIT["degree", 0.017453292519943295],
>    AXIS["Longitude", EAST],
>    AXIS["Latitude", NORTH],
>    AUTHORITY["EPSG","4326"]]
So we have a two dimensional CRS here.

> gridToCRS PARAM_MT["Affine",
>    PARAMETER["num_row", 4],
>    PARAMETER["num_col", 4],
>    PARAMETER["elt_0_0", 0.09000000357627869],
>    PARAMETER["elt_0_3", 215.86099243164062],
>    PARAMETER["elt_1_1", -0.07699999958276749],
>    PARAMETER["elt_1_3", 82.258999414742],
>    PARAMETER["elt_2_2", �],
>    PARAMETER["elt_2_3", �]]
This is expecting a 3D CRS, as seen from the "num_row" parameter (in the case of 
an "Affine" transform - basically a matrix -, the number of rows is the number 
of dimension of the target coordinate system plus one), so we have a dimension 
mismatch with the above 2D CRS. This is the cause of the 
MismatchedDimensionException. However the "?" value for "elt_2_2" and "elt_2_2" 
is suspect and will probably cause more issue even if the 
MismatchedDimensionException were solved.

> In my code I get the gridcoverage from the NetcdfReader I slightly
> modified and then I recreate a new gridcoverage based on the previous
> one to add the color to the image.
> Then I do
>    String wkt = "GEOGCS[\"WGS 84\",...snip...]]";
>    CoordinateReferenceSystem targetCRS = CRS.parseWKT(wkt);
The CRS needs to be a 3D one, or the grid geometry need to be a 2D one. If you 
don't care about the third axis (which I guess is the time axis), the easiest 
approach is to just drop the extra-dimension from the grid geometry. There is a 
convenience method for that. In:

> oldCoverage.getGridGeometry().getGridToCRS(),

Replace the "getGridToCRS()" call by "getGridToCRS2D()" (the later method is 
defined in the GridGeometry2D subclass).

> I am not very good at writting CoordinateReferenceSystem, is there any good documentation/book on it?
I'm not aware of a tutorial. The easiest way is to use an existing WKT, and 
modify it. For example use System.out.println(CRS.decode("EPSG:foo")) then edit 
the output.

The WKT syntax is defined at 
The projection and parameter names are listed at 


More information about the Geotoolkit mailing list