Module org.apache.sis.storage
Package org.apache.sis.storage.aggregate

# Class MergeStrategy

Object
MergeStrategy

public final class MergeStrategy extends Object
Algorithm to apply when more than one grid coverage can be found at the same grid index. A merge may happen if an aggregated coverage is created with `CoverageAggregator`, and the extent of some source coverages are overlapping in the dimension to aggregate.

## Example

A collection of `GridCoverage` instances may represent the same phenomenon (for example Sea Surface Temperature) over the same geographic area but at different dates and times. `CoverageAggregator` can be used for building a single data cube with a time axis. But if two coverages have overlapping time ranges, and if a user request data in the overlapping region, then the aggregated coverages have more than one source coverages capable to provide the requested data. This enumeration specify how to handle this multiplicity.

## Default behavior

If no merge strategy is specified, then the default behavior is to throw `SubspaceNotSpecifiedException` when the `GridCoverage​.render(GridExtent)` method is invoked and more than one source coverage (slice) is found for a specified grid index.

Since:
  1.3

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Resource`
  `apply(Resource resource)`
  Returns a resource with same data than specified resource but using this merge strategy.
  `static MergeStrategy`
  `selectByTimeThenArea(Duration timeGranularity)`
  Selects a single slice using criteria based first on temporal extent, then on geographic area.
  `String`
  `toString()`
  Returns a string representation of this strategy for debugging purposes.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Method Details

  - ### selectByTimeThenArea

    public static MergeStrategy selectByTimeThenArea(Duration timeGranularity)
    Selects a single slice using criteria based first on temporal extent, then on geographic area. This strategy applies the following rules, in order:
    1. Slice having largest intersection with the time of interest (TOI) is selected.
    2. If two or more slices have the same intersection with TOI, then the one with less "overtime" (time outside TOI) is selected.
    3. If two or more slices are considered equal after above criteria, then the one best centered on the TOI is selected.
    Rational: the "smallest time outside" criterion (rule 2) is before "best centered" criterion (rule 3) because of the following scenario: if a user specifies a "time of interest" (TOI) of 1 day and if there is two slices intersecting the TOI, with one slice being a raster of monthly averages the other slice being a raster of daily data, we want the daily data to be selected even if by coincidence the monthly averages is better centered.
    If the `timeGranularity` argument is non-null, then intersections with TOI will be rounded to an integer amount of the specified granularity and the last criterion in above list is relaxed. This is useful when data are expected at an approximately regular time interval (for example one remote sensing image per day) and we want to ignore slight variations in the temporal extent declared for each image.

    If there is no time of interest, or the slices do not declare time range, or some slices are still at equality after application of above criteria, then the selection continues on the basis of geographic criteria:

    1. Largest intersection with the area of interest (AOI) is selected.
    2. If two or more slices have the same intersection area with AOI, then the one with the less "irrelevant" material is selected. "Irrelevant" material are area outside the AOI.
    3. If two or more slices are considered equal after above criteria, the one best centered on the AOI is selected.
    4. If two or more slices are considered equal after above criteria, then the first of those candidates is selected.
    If two slices are still considered equal after all above criteria, then an arbitrary one is selected.

    #### Limitations

    Current implementation does not check the vertical dimension. This check may be added in a future version.
    Parameters:
      `timeGranularity` - the temporal granularity of the Time of Interest (TOI), or `null` if none.
    Returns:
      a merge strategy for selecting a slice based on temporal criteria first.

  - ### apply

    public Resource apply(Resource resource)
    Returns a resource with same data than specified resource but using this merge strategy. If the given resource is an instance created by `CoverageAggregator` and uses a different strategy, then a new resource using this merge strategy is returned. Otherwise the given resource is returned as-is. The returned resource will share the same resources and caches than the given resource.
    Parameters:
      `resource` - the resource for which to update the merge strategy, or `null`.
    Returns:
      resource with updated merge strategy, or `null` if the given resource was null.

  - ### toString

    public String toString()
    Returns a string representation of this strategy for debugging purposes.
    Overrides:
      `toString` in class `Object`
    Returns:
      string representation of this strategy.