The process of creating a GridCoverage starts with creating an ImageReader to access the contents of the original data in some source, such as a file on disk. This ImageReader will need to provide both a RenderedImage and an object which extends IIOMetadata. See the geotk-coverageio module for more information on this topic.
The next step is to use the ImageReader we created above and use its contents to create the GridCoverage2D. This is merely a matter of using the ImageReader to get all the required pieces and reassembling those pieces to work for the factory we use to create the GridCoverage2D.
The ImageReader can furnish the required RenderedImage using the read(...) method. The rest of the required information which is described below will need to be extracted from the IIOMetadata object obtained from the getImageMetadata(...) method (depending, as explained in the note above, on where the programmer decides to obtain all the required metadata).
The builder will have to wrap up the following steps.
For each band in the input data matrix, we need to create one or more Category objects.
The Category instances will differ depending on how they are constructed and be either a qualitative category or a quantitative category.
For the example in the previous section, we would create three category objects:
Color[] colorramp = {
Color.BLUE,
Color.GREEN
};
Category[] catset = {
new Category("NoData", new Color(0,0,0,0), -9999),
new Category("Clouds", Color.WHITE, -8888),
new Category("Elevation off benchmark",
colorramp,
NumberRange.create(0, 5000),
NumberRange.create(-100.0, 400.0))
};With an array of categories and a unit we can create a SampleDimension. In our case we use the constructor of GridSampleDimension.
For the DEM example, we would have:
GridSampleDimension[] sampdims = {
new GridSampleDimension("Elevation data", catset, NonSI.FOOT)
};The easy way to do this is to create an Envelope with the right CRS and the coordinates of the corners. This will only work for GridCoverage instances which are axis aligned, with axes in the right order, and which do not have any unusual information.
For our DEM example, we would do:
double westmost = 2.0; //Degrees
double soutmost = 51.0;
double eastmost = 3.0;
double northmost = 52.0;
Envelope env = new Envelope2D(DefaultGeographicCRS.WGS84,
westmost,
soutmost,
eastmost-westmost,
northmost-soutmost);More complex cases can be handled by having the user create the actual MathTransform which should be used to go from the pixel row/column offset to georeferenced space.
Finally we have all the pieces to create the GridCoverage itself. We need to find a factory and then use it.
For our DEM example, we would do:
RenderedImage img = myImageReader.read(...);
//Using null for the hints, we get the first factory the finder obtains.
GridCoverageFactory fac = CoverageFactoryFinder.getGridCoverageFactory(null);
GridCoverage2D myGridCoverage = fac.create("Paris Elevation", img, env, sampdims, null, null);These steps can all be wrapped up into the builder class.
Next section: Use a Grid Coverage.