Class GridCoverageBuilder

Object
GridCoverageBuilder

public class GridCoverageBuilder extends Object
Helper class for the creation of Grid­Coverage instances. A grid coverage is a function described by three parts:
  • A domain, which describes the input values (e.g. geographic coordinates).
  • One or more ranges, which describe the output values that the coverage can produce.
  • The actual values, distributed on a regular grid.
Each of those parts can be set by a set­Domain(…), set­Ranges(…) or set­Values(…) method. Those methods are overloaded with many variants accepting different kind of arguments. For example, values can be specified as a Rendered­Image, a Raster or some other types.

Example

The easiest way to create a Grid­Coverage from a matrix of values is to set the values in a Writable­Raster and to specify the domain as an Envelope:
    public GridCoverage createCoverage() {
        WritableRaster data = Raster.createBandedRaster(DataBuffer.TYPE_USHORT, width, height, numBands, null);
        for (int y=0; y<height; y++) {
            for (int x=0; x<width; x++) {
                int value = ...;                     // Compute a value here.
                data.setSample(x, y, 0, value);      // Set value in the first band.
            }
        }
        var builder = new GridCoverageBuilder();
        builder.setValues(data).flixAxis(1);

        Envelope domain = ...;                       // Specify here the "real world" coordinates.
        return builder.setDomain(domain).build();
    }

Limitations

Current implementation creates only two-dimensional coverages. A future version may extend this builder API for creating n-dimensional coverages.
Since:
1.1
See Also:
  • Constructor Details

    • GridCoverageBuilder

      public GridCoverageBuilder()
      Creates an initially empty builder.
  • Method Details

    • setDomain

      public GridCoverageBuilder setDomain(GridGeometry grid)
      Sets the domain envelope (including its CRS) and/or the transform from grid indices to domain coordinates. The given Grid­Geometry does not need to contain a Grid­Extent because that extent will be computed automatically if needed. However if an extent is present, then it must be consistent with the size of data given to set­Values(…) method (will be verified at build() time).
      Parameters:
      grid - the new grid geometry, or null for removing previous domain setting.
      Returns:
      this for method invocation chaining.
    • setDomain

      public GridCoverageBuilder setDomain(Envelope domain)
      Sets the domain as an enclosing envelope (including its CRS). The given envelope should contain all pixel area. For example, the envelope lower corner should locate the lower-left (or upper-left, depending on y axis orientation) pixel corner, not pixel center. If the given envelope contains a CRS, then that CRS will be the coverage CRS. A transform from grid indices to domain coordinates will be created automatically. That transform will map grid dimensions to envelope dimensions in the same order (i.e. the matrix representation of the affine transform will be diagonal, ignoring the translation column).

      Axis directions

      By default grid indices increase in the same direction than domain coordinates. When applied to images with pixels located by (column, row) indices, it means that by default row indices in the image are increasing toward up if the y coordinates in the coverage domain (e.g. latitude values) are also increasing toward up. It often results in images flipped vertically, because popular image formats such as PNG use row indices increasing in the opposite direction (toward down). This effect can be compensated by invoking flip­Grid­Axis(1).

      Grid­Coverage­Builder provides method only for flipping axes. If more sophisticated operations is desired (for example a rotation), then set­Domain(Grid­Geometry) should be used instead of this method.

      Design note
      Grid­Coverage­Builder does not flip the y axis by default because not all file formats have row indices increasing toward down. A counter-example is the netCDF format. Even if we consider that the majority of images have y axis flipped, things become less obvious when considering data in more than two dimensions. Having the same default policy (no flipping) for all dimensions make problem analysis easier.

      Default implementation

      The default implementation creates a new Grid­Geometry from the given envelope then invokes set­Domain(Grid­Geometry). Subclasses can override that later method as a single overriding point for all domain settings.
      Parameters:
      domain - envelope of the coverage domain together with its CRS, or null for removing previous domain setting.
      Returns:
      this for method invocation chaining.
      See Also:
    • setRanges

      public GridCoverageBuilder setRanges(Collection<? extends SampleDimension> bands)
      Sets the sample dimensions for all bands. The list size must be equal to the number of bands in the data specified to set­Values(…) method (it will be verified at build() time).
      Parameters:
      bands - the new sample dimensions, or null for removing previous range setting.
      Returns:
      this for method invocation chaining.
      Throws:
      Illegal­Argument­Exception - if the given list is empty.
      See Also:
    • setRanges

      public GridCoverageBuilder setRanges(SampleDimension... bands)
      Sets the sample dimensions for all bands. The array length must be equal to the number of bands in the data specified to set­Values(…) method (it will be verified at build() time).
      Parameters:
      bands - the new sample dimensions, or null for removing previous range setting.
      Returns:
      this for method invocation chaining.
      Throws:
      Illegal­Argument­Exception - if the given array is empty.
      See Also:
    • addRange

      public GridCoverageBuilder addRange(SampleDimension band)
      Adds a sample dimension for one band. This method can be invoked repeatedly until the number of sample dimensions is equal to the number of bands in the data specified to set­Values(…).
      Parameters:
      band - the sample dimension to add.
      Returns:
      this for method invocation chaining.
      See Also:
    • setValues

      public GridCoverageBuilder setValues(RenderedImage data)
      Sets a two-dimensional slice of sample values as a rendered image. If sample dimensions are specified, then the number of bands must be equal to the number of sample dimensions.

      Note: row indices in an image are usually increasing down, while geographic coordinates are usually increasing up. Consequently, the flip­Grid­Axis(1) method may need to be invoked after this method.

      Parameters:
      data - the rendered image to be wrapped in a Grid­Coverage. Cannot be null.
      Returns:
      this for method invocation chaining.
      See Also:
    • setValues

      public GridCoverageBuilder setValues(Raster data)
      Sets a two-dimensional slice of sample values as a raster. If sample dimensions are specified, then the number of bands must be equal to the number of sample dimensions.

      Note: row indices in a raster are usually increasing down, while geographic coordinates are usually increasing up. Consequently, the flip­Grid­Axis(1) method may need to be invoked after this method.

      Parameters:
      data - the raster to be wrapped in a Grid­Coverage. Cannot be null.
      Returns:
      this for method invocation chaining.
      See Also:
    • setValues

      public GridCoverageBuilder setValues(DataBuffer data, Dimension size)
      Sets a two-dimensional slice of sample values as a Java2D data buffer. The number of banks will be the number of bands in the image. If sample dimensions are specified, then the number of bands must be equal to the number of sample dimensions.
      Parameters:
      data - the data buffer to be wrapped in a Grid­Coverage. Cannot be null.
      size - the image size in pixels, or null if unspecified. If null, then the image size will be taken from the grid extent.
      Returns:
      this for method invocation chaining.
      Throws:
      Illegal­Argument­Exception - if width or height is negative or equals to zero.
    • flipGridAxis

      public GridCoverageBuilder flipGridAxis(int dimension)
      Reverses axis direction in the specified grid dimension. For example if grid indices are (column, row), then flip­Grid­Axis(1) will reverse the direction of rows axis. Invoking this method a second time for the same dimension will cancel the flipping.

      When building coverage with a domain specified by an envelope (i.e. with no explicit grid to CRS transform), the default Grid­Coverage­Builder behavior is to create a Grid­Geometry with grid indices increasing in the same direction than domain coordinates. This method allows to reverse direction for an axis. The most typical usage is to reverse the direction of the y axis in images.

      Parameters:
      dimension - index of the dimension in the grid to reverse direction.
      Returns:
      this for method invocation chaining.
      See Also:
    • addImageProperty

      public GridCoverageBuilder addImageProperty(String key, Object value)
      Adds a value associated to an image property. This method can be invoked only once for each key. Those properties will be given to the Rendered­Image created by the build() method.
      Parameters:
      key - key of the property to set.
      value - value to associate to the given key.
      Returns:
      this for method invocation chaining.
      Throws:
      Illegal­Argument­Exception - if a value is already associated to the given key.
      Since:
      1.1
    • build

      public GridCoverage build() throws IllegalStateException
      Creates the grid coverage from the domain, ranges and values given to setter methods. The returned coverage is often a Grid­Coverage2D instance, but not necessarily.
      Returns:
      grid coverage created from specified domain, ranges and sample values.
      Throws:
      Illegal­State­Exception - if some properties are inconsistent, for example grid extent not matching image size or number of sample dimensions not matching the number of bands. This exception often wraps an Illegal­Grid­Geometry­Exception, Illegal­Argument­Exception or Null­Pointer­Exception.