org.geotoolkit.image.io.mosaic
Class MosaicBuilder

Object
  extended by Builder<TileManager>
      extended by MosaicBuilder
All Implemented Interfaces:
LogProducer

@ThreadSafe
public class MosaicBuilder
extends Builder<TileManager>
implements LogProducer

Creates TileManager from a set of images organized according a given tile layout. This class can work with pre-existing tile files (in which case it just build a TileManager), or can write the tiles to the disk if the files do not already exist.


Example creating tiles to disk
For example in order to create a mosaic for a set of tiles of size 256×256 pixels, with overviews having pixels 2, 3 and 4 times the width and height of original pixels and for writing the tiles in the "output" directory, use the following:

Object originalMosaic = ...; // May be a File, URL, list of Tiles, etc.
MosaicBuilder builder = new MosaicBuilder();
builder.setTileDirectory(new File("output"));
builder.setTileSize(new Dimension(256, 256));
builder.setSubsamplings(1, 2, 3, 4);
TileManager newMosaic = builder.writeFromInput(originalMosaic, null);

Since:
2.5
Version:
3.17
Author:
Martin Desruisseaux (Geomatys), Cédric Briançon (Geomatys)
See Also:
MosaicBuilderEditor
Module:
coverage/geotk-coverageio (download)    View source code for this class

Field Summary
protected  TileManagerFactory factory
          The factory to use for creating TileManager instances.
 
Constructor Summary
MosaicBuilder()
          Creates a new instance which will use the default tile manager factory.
MosaicBuilder(TileManagerFactory factory)
          Generates tiles using the specified tile manager factory.
 
Method Summary
 TileManager build()
          Creates a tile manager from the informations supplied in above setters.
 TileManager createTileManager()
          Creates a tile manager from the informations supplied in above setters.
 TileManager createTileManager(Object input)
          Creates a tile manager from an untiled image.
protected  String generateFilename(int level, int column, int row)
          Generates a filename for the current tile based on the position of this tile in the raster.
 AffineTransform getGridToCRS()
          Returns the transform from mosaic pixel coordinates to mosaic geodetic coordinates, or null if none.
 Level getLogLevel()
          Returns the logging level for information about tiles being read and written.
 Dimension[] getSubsamplings()
          Returns the subsampling for overview computations.
 File getTileDirectory()
          Returns the tile directory, or null for current directory.
 TileLayout getTileLayout()
          Returns the tile layout.
 ImageReaderSpi getTileReaderSpi()
          Returns the image reader provider to use for reading tiles.
 Dimension getTileSize()
          Returns the tile size.
 Rectangle getUntiledImageBounds()
          Returns the grid envelope (in pixels) of the mosaic as a whole, or null if not set.
 IIOListeners listeners()
          Returns a modifiable collection of image I/O listeners.
protected  void onTileWrite(Tile tile, ImageWriteParam parameters)
          Invoked automatically when a tile is about to be written.
 void setGridToCRS(AffineTransform tr)
          Sets the transform from mosaic pixel coordinates to mosaic geodetic coordinates.
 void setLogLevel(Level level)
          Sets the logging level for information about tiles being read and written.
 void setSubsamplings(Dimension... subsamplings)
          Sets the subsamplings for overview computations.
 void setSubsamplings(int... subsamplings)
          Sets uniform subsamplings for overview computations.
 void setTileDirectory(File directory)
          Sets the directory where tiles will be read or written.
 void setTileLayout(TileLayout layout)
          Sets the tile layout to the specified value.
 void setTileReaderSpi(ImageReaderSpi provider)
          Sets the image reader provider for each tiles to be read.
 void setTileReaderSpi(String format)
          Sets the image reader provider by name.
 void setTileSize(Dimension size)
          Sets the tile size.
 void setUntiledImageBounds(Rectangle bounds)
          Sets the grid envelope (in pixels) of the mosaic as a whole.
static int suggestedTileSize(int imageSize, int tileSize, int minSize, int maxSize)
          Returns a suggested tile size (width or height) for the given image size.
 TileManager writeFromInput(Object input, int inputIndex, MosaicImageWriteParam param)
          Creates the tile manager and writes the tiles on disk.
 TileManager writeFromInput(Object input, MosaicImageWriteParam param)
          Creates the tile manager and writes the tiles on disk.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factory

protected final TileManagerFactory factory
The factory to use for creating TileManager instances.

Constructor Detail

MosaicBuilder

public MosaicBuilder()
Creates a new instance which will use the default tile manager factory.


MosaicBuilder

public MosaicBuilder(TileManagerFactory factory)
Generates tiles using the specified tile manager factory.

Parameters:
factory - The factory to use, or null for the default one.
Method Detail

getLogLevel

public Level getLogLevel()
Returns the logging level for information about tiles being read and written. The default value is one of the PerformanceLevel constants, determined according the duration of the operation.

Specified by:
getLogLevel in interface LogProducer
Returns:
The current logging level.

setLogLevel

public void setLogLevel(Level level)
Sets the logging level for information about tiles being read and written. A null value restores the default level documented in the getLogLevel() method.

Specified by:
setLogLevel in interface LogProducer
Parameters:
level - The new logging level.

getTileLayout

public TileLayout getTileLayout()
Returns the tile layout. This is an enumeration that specify how this MosaicBuilder will lay out the new tiles relative to each other. For example if the pixels in an image overview cover a geographic area 2 time larger (in width and height) than the pixels in an original image, then we have a choice:

The default value is CONSTANT_TILE_SIZE.

Returns:
An identification of current tile layout.

setTileLayout

public void setTileLayout(TileLayout layout)
Sets the tile layout to the specified value. Valid values are CONSTANT_TILE_SIZE and CONSTANT_GEOGRAPHIC_AREA.

Parameters:
layout - An identification of new tile layout.

getTileDirectory

public File getTileDirectory()
Returns the tile directory, or null for current directory. This is the directory where writeFromInput methods will write the new tiles, if writing tiles is allowed. This is also the directory where the TileManager created by the above methods will read the tiles back.

The directory may be either relative or absolute. The default value is null.

Returns:
The current tiles directory.

setTileDirectory

public void setTileDirectory(File directory)
Sets the directory where tiles will be read or written. May be a relative or absolute path, or null (the default) for current directory.

Parameters:
directory - The new tiles directory.

getTileReaderSpi

public ImageReaderSpi getTileReaderSpi()
Returns the image reader provider to use for reading tiles. The initial value is null, which means that the provider should be the same than the one detected by createTileManager(Object) from its input argument.

Returns:
The current image reader provider for tiles.

setTileReaderSpi

public void setTileReaderSpi(ImageReaderSpi provider)
Sets the image reader provider for each tiles to be read. A null value means that the provider should be automatically detected by createTileManager(Object).

It is recommended to avoid WorldFileImageReader provider, in order to avoid unnecessary attempts to read the ".tfw" and ".prj" files. Callers can use the following code:

setTileReaderSpi(WorldFileImageReader.Spi.unwrap(provider));

Parameters:
provider - The new image reader provider for tiles.

setTileReaderSpi

public void setTileReaderSpi(String format)
                      throws IllegalArgumentException
Sets the image reader provider by name. This convenience method searches a provider for the given name in the default IIORegistry and delegates to setTileReaderSpi(ImageReaderSpi).

Parameters:
format - The image format name for tiles.
Throws:
IllegalArgumentException - if no provider was found for the given name.

getGridToCRS

public AffineTransform getGridToCRS()
Returns the transform from mosaic pixel coordinates to mosaic geodetic coordinates, or null if none. This transform is optional. If specified, then the builder will forward this value to TileManager.setGridToCRS(AffineTransform).

Returns:
The pixel to geodetic transform, or null if none.
Since:
3.16

setGridToCRS

public void setGridToCRS(AffineTransform tr)
Sets the transform from mosaic pixel coordinates to mosaic geodetic coordinates. This transform is optional. If specified, then the builder will forward this value to TileManager.setGridToCRS(AffineTransform).

Tips

Parameters:
tr - The pixel to geodetic transform, or null if none.
Since:
3.16

getUntiledImageBounds

public Rectangle getUntiledImageBounds()
Returns the grid envelope (in pixels) of the mosaic as a whole, or null if not set. In the later case, the bounds will be inferred from the input image when createTileManager(Object) is invoked.

Returns:
The current grid envelope of the mosaic, or null.

setUntiledImageBounds

public void setUntiledImageBounds(Rectangle bounds)
Sets the grid envelope (in pixels) of the mosaic as a whole. A null value discards any value previously set.

Parameters:
bounds - The new grid envelope of the mosaic, or null.

getTileSize

public Dimension getTileSize()
Returns the tile size. If no tile size has been explicitly set, then a default tile size will be computed from the untiled image bounds. If no size can be computed, then this method returns null.

Returns:
The current tile size.
See Also:
suggestedTileSize(int)

setTileSize

public void setTileSize(Dimension size)
Sets the tile size. A null value discards any value previously set.

Parameters:
size - The new tile size.

suggestedTileSize

public static int suggestedTileSize(int imageSize,
                                    int tileSize,
                                    int minSize,
                                    int maxSize)
                             throws IllegalArgumentException
Returns a suggested tile size (width or height) for the given image size. This method searches for a value x inside the [minSize...maxSize] range where imageSize/x has the largest amount of divisors. If more than one value have the same amount of divisors, then the one which is the closest to tileSize is returned.

Parameters:
imageSize - The image size.
tileSize - The preferred tile size. Must be inside the [minSize...maxSize] range.
minSize - The minimum size, inclusive. Must be greater than 0.
maxSize - The maximum size, inclusive. Must be equals or greater that minSize.
Returns:
The suggested tile size. This value is inside the [minSize...maxSize] range except if imageSize was smaller than minSize.
Throws:
IllegalArgumentException - if any argument doesn't meet the above-cited conditions.

getSubsamplings

public Dimension[] getSubsamplings()
Returns the subsampling for overview computations. If no subsamplings were explicitly set, then this method computes automatically some subsamplings from the untiled image bounds and tile size, with the following properties (note that those properties are not guaranteed if the subsampling was explicitly specified rather than computed):

If no subsampling can be computed, then this method returns null.

Returns:
The current subsamplings for each overview levels.

setSubsamplings

public void setSubsamplings(Dimension... subsamplings)
Sets the subsamplings for overview computations. The number of overview levels created by this MosaicBuilder will be equal to the subsamplings array length.

Subsamplings most be explicitly provided for TileLayout.CONSTANT_GEOGRAPHIC_AREA, but is optional for TileLayout.CONSTANT_TILE_SIZE. In the later case subsamplings may be null (the default), in which case they will be automatically computed from the untiled image bounds and tile size in order to have only entire tiles (i.e. tiles in last columns and last rows don't need to be cropped).

Parameters:
subsamplings - The new subsamplings for each overview levels.

setSubsamplings

public void setSubsamplings(int... subsamplings)
Sets uniform subsamplings for overview computations. This convenience method delegates to setSubsamplings(Dimension[]) with the same value affected to both width and height.

Parameters:
subsamplings - The new subsamplings for each overview levels.

build

public TileManager build()
                  throws BackingStoreException
Creates a tile manager from the informations supplied in above setters. The default implementation delegates to createTileManager(), wrapping any potential IOException into a BackingStoreException.

Specified by:
build in class Builder<TileManager>
Returns:
The tile manager created from the information returned by getter methods.
Throws:
BackingStoreException - if createTileManager() threw IOException.
Since:
3.20

createTileManager

public TileManager createTileManager()
                              throws IOException
Creates a tile manager from the informations supplied in above setters. The following methods must be invoked prior this one:

The other setter methods are optional.

Returns:
The tile manager created from the information returned by getter methods.
Throws:
IOException - if an I/O operation was required and failed. The default implementation does not perform any I/O, but subclasses are allowed to do so.

createTileManager

public TileManager createTileManager(Object input)
                              throws IOException
Creates a tile manager from an untiled image. The untiled image bounds and tile reader SPI are inferred from the input, unless they were explicitly specified. The input may be a File if the mosaic should be created from a single input image, or may be a collection of Tiles or a TileManager if the new mosaic should be created from an existing one.

This method does not write any tile to disk.

Parameters:
input - The image input, typically as a File or an other TileManager.
Returns:
The tiles, or null if the process has been aborted.
Throws:
IOException - if an error occurred while reading the untiled image.

writeFromInput

public TileManager writeFromInput(Object input,
                                  MosaicImageWriteParam param)
                           throws IOException
Creates the tile manager and writes the tiles on disk. This is equivalent to writeFromInput(input, 0, policy) except that this method ensures that the input contains only one image. If more than one image is found, then an exception is throw. This is often desireable when the input is a collection of Tiles, since having more than one "image" (where "image" in this context means different instances of TileManager) means that we failed to create a single mosaic from a set of source tiles.

Parameters:
input - The image input, typically as a File or an other TileManager.
param - The parameter to be given to MosaicImageWriter, or null for the default parameters.
Returns:
The tiles, or null if the process has been aborted while writing tiles.
Throws:
IOException - if an error occurred while reading the untiled image or while writing the tiles to disk.
Since:
3.01

writeFromInput

public TileManager writeFromInput(Object input,
                                  int inputIndex,
                                  MosaicImageWriteParam param)
                           throws IOException
Creates the tile manager and writes the tiles on disk. The untiled image bounds and tile reader SPI are inferred from the input, unless they were explicitly specified. The input may be a File if the mosaic should be created from a single input image, or may be a collection of Tiles or a TileManager if the new mosaic should be created from an existing one.

Parameters:
input - The image input, typically as a File or an other TileManager.
inputIndex - Index of image to read, typically 0.
param - The parameter to be given to MosaicImageWriter, or null for the default parameters.
Returns:
The tiles, or null if the process has been aborted while writing tiles.
Throws:
IOException - if an error occurred while reading the untiled image or while writing the tiles to disk.
Since:
3.01

listeners

public IIOListeners listeners()
Returns a modifiable collection of image I/O listeners. Methods like addIIOReadProgressListener and addIIOWriteProgressListener can be invoked on the returned object. The read listeners are used when reading the input mosaic, while the write listeners are used when writing the output mosaic.

Returns:
The manager of image I/O listeners.
Since:
3.02

generateFilename

protected String generateFilename(int level,
                                  int column,
                                  int row)
Generates a filename for the current tile based on the position of this tile in the raster. For example, a tile in the first overview level, which is localized on the 5th column and 2nd row may have a name like "L1_E2.png".

Subclasses may override this method if they want more control on generated tile filenames.

Parameters:
level - The level of overview. First level is 0.
column - The index of columns. First column is 0.
row - The index of rows. First row is 0.
Returns:
A filename based on the position of the tile in the whole raster.

onTileWrite

protected void onTileWrite(Tile tile,
                           ImageWriteParam parameters)
                    throws IOException
Invoked automatically when a tile is about to be written. The default implementation does nothing. Subclasses can override this method in order to set custom write parameters.

The source region and source subsampling parameters can not be set through this method. Their setting will be overwritten by the caller because their values depend on the strategy chosen by MosaicImageWriter for reading images, which itself depends on the amount of available memory.

Parameters:
tile - The tile to be written.
parameters - The parameters to be given to the image writer.
Throws:
IOException - if an I/O operation was required and failed.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.