Class TileOrganizer

java.lang.Object
org.apache.sis.internal.referencing.j2d.TileOrganizer

public class TileOrganizer extends Object
Creates a collection of Tiles from their grid to CRS affine transforms. When the Rectangle that describe the destination region is known for each tiles, the Tile(Rectangle, Dimension) constructor should be invoked directly. But in some cases the destination rectangle is not known directly. Instead we have a set of tiles, all of them with an upper-left corner located at (0,0), but different grid to CRS affine transforms read from World Files. This TileOrganizer class infers the destination regions automatically from the set of affine transforms.
Since:
1.1
Version:
1.1
  • Field Details

    • EPS

      private static final double EPS
      Small number for floating point comparisons.
      See Also:
    • xLocation

      private final int xLocation
      The location of the final bounding box (the one including every tiles). Tiles will be translated as needed in order to fit this location. This is usually zero, but not necessarily.
    • yLocation

      private final int yLocation
      The location of the final bounding box (the one including every tiles). Tiles will be translated as needed in order to fit this location. This is usually zero, but not necessarily.
    • tiles

      private final Map<AffineTransform,Tile> tiles
      Tiles for which we should compute the bounding box after we have them all. Their bounding box (region) will need to be adjusted for the affine transform.
    • X_COMPARATOR

      private static final Comparator<AffineTransform> X_COMPARATOR
      Sorts affine transform by increasing X scales in absolute value. For computePyramidLevels(Collection) internal working only.
    • Y_COMPARATOR

      private static final Comparator<AffineTransform> Y_COMPARATOR
      Sorts affine transform by increasing Y scales in absolute value. For computePyramidLevels(Collection) internal working only.
  • Constructor Details

    • TileOrganizer

      public TileOrganizer(Point location)
      Creates an initially empty tile collection with the given location.
      Parameters:
      location - the location, or null for (0,0).
  • Method Details

    • getLocation

      public Point getLocation()
      Returns the location of the tile collections to be created. The location is often (0,0) as expected in BufferedImage, but does not have to.
      Returns:
      origin of the tile collections to be created.
    • add

      public boolean add(Tile tile)
      Adds a tile to the collection of tiles to process. Each tile can be added only once.
      Parameters:
      tile - the tile to add.
      Returns:
      true if the tile has been successfully added, or false if the tile does not need to be processed by this class.
    • tiles

      public Map<Tile,Tile[]> tiles() throws IOException
      Returns the tiles. Keys are pyramid geometry (containing mosaic bounds and grid to CRS transforms) and values are the tiles in that pyramid. This method usually returns a singleton map, but more entries may be present if this method was not able to build a single pyramid using all provided tiles.

      Invoking this method clear the collection. On return, this instance is empty. This is because current implementation modify its workspace directly for efficiency.

      Returns:
      all tiles added to this TileOrganizer, grouped by pyramids.
      Throws:
      IOException - if a call to Tile.getSize() or Tile.getRegion() failed, and unavailableSize(Tile, IOException) did not consumed the exception.
    • computePyramidLevels

      private static List<Map<AffineTransform,Dimension>> computePyramidLevels(Collection<AffineTransform> gridToCRS)
      From a set of arbitrary affine transforms, computes pyramid levels that can be given to Tile constructors. This method tries to locate the affine transform with finest resolution. This is typically (but not always, depending on rotation or axis flip) the transform with smallest scale X and scale Y coefficients in absolute value. That transform is given a "resolution" of (1,1) and stored in an IdentityHashMap. Other transforms are stored in the same map with their resolution relative to the first one, or discarded if the relative resolution is not an integer. In the latter case, the transforms that were discarded from the first pass will be put in a new map to be added as the second element in the returned list. A new pass is run, discarded transforms from the second pass are put in the third element of the list, etc.
      Parameters:
      gridToCRS - the grid to CRS affine transforms computed from the image to use in a pyramid. The collection and the transform elements are not modified by this method (they may be modified by the caller however).
      Returns:
      a subset of the given transforms with their relative resolution. This method typically returns one map, but more could be returned if the scale ratio is not an integer for every transforms.
    • computePyramidLevels

      private static int computePyramidLevels(AffineTransform[] gridToCRS, int length, Map<AffineTransform,Dimension> result, boolean isY)
      Computes the pyramid level for the given affine transforms along the X or Y axis, and stores the result in the given map.
      Parameters:
      gridToCRS - the AffineTransform to analyze. This array must be sorted along the dimension specified by isY.
      length - the number of valid entries in the gridToCRS array.
      result - an initially empty map in which to store the results.
      isY - false for analyzing the X axis, or true for the Y axis.
      Returns:
      the number of entries remaining in gridToCRS.
    • level

      private static int level(double ratio)
      Computes the pyramid level from the ratio between two affine transform coefficients. If the ratio has been computed from entry2.scaleX / entry1.scaleX, then a return value of:
      • 1 means that both entries are at the same level.
      • 2 means that the second entry has pixels twice as large as first entry.
      • 3 means that the second entry has pixels three time larger than first entry.
      • etc...
      • A negative number means that the second entry has pixels smaller than first entry.
      • 0 means that the ratio between entries is not an integer number.
      Parameters:
      ratio - the ratio between affine transform coefficients.
      Returns:
      the pixel size (actually subsampling) relative to the smallest pixel, or 0 if it cannot be computed. If the ratio is between 0 and 1, then this method returns a negative number.
    • unavailableSize

      protected boolean unavailableSize(Tile tile, IOException exception)
      Invoked when an I/O error occurred in Tile.getSize() or Tile.getRegion(). This error is non-fatal since TileOrganizer can fallback on calculation based on tile location only (without size).

      The default implementation returns false, which instructs the caller to let the exception propagate.

      Parameters:
      tile - the tile on which an error occurred.
      exception - the error that occurred.
      Returns:
      true if the exception has been consumed, or false for re-throwing it.
    • toString

      public String toString()
      Returns a string representation of the tiles contained in this object. Since this method is for debugging purpose, only the first tiles may be formatted in order to avoid consuming to much space in the debugger.
      Overrides:
      toString in class Object