Search in sources :

Example 1 with DriverCreateCapabilities

use of it.geosolutions.imageio.gdalframework.GDALUtilities.DriverCreateCapabilities in project imageio-ext by geosolutions-it.

the class GDALImageWriter method write.

/**
 * Write the input image to the output.
 * <p>
 * The output must have been set beforehand using the <code>setOutput</code>
 * method.
 *
 * <p>
 * An <code>ImageWriteParam</code> may optionally be supplied to control
 * the writing process. If <code>param</code> is <code>null</code>, a
 * default write param will be used.
 *
 * <p>
 * If the supplied <code>ImageWriteParam</code> contains optional setting
 * values not supported by this writer (<i>e.g.</i> progressive encoding
 * or any format-specific settings), they will be ignored.
 *
 * @param streamMetadata
 *                an <code>IIOMetadata</code> object representing stream
 *                metadata, or <code>null</code> to use default values.
 * @param image
 *                an <code>IIOImage</code> object containing an image, and
 *                metadata to be written. Note that metadata is actually
 *                supposed to be an instance of
 *                {@link GDALCommonIIOImageMetadata}.
 *                {@link GDALWritableCommonIIOImageMetadata} may be used to
 *                set properties from other type of ImageMetadata to a
 *                format which is understood by this writer.
 * @param param
 *                an <code>ImageWriteParam</code>, or <code>null</code>
 *                to use a default <code>ImageWriteParam</code>.
 *
 * @exception IllegalStateException
 *                    if the output has not been set.
 * @exception IllegalArgumentException
 *                    if <code>image</code> is <code>null</code>.
 * @exception IOException
 *                    if an error occurs during writing.
 */
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
    if (outputFile == null) {
        throw new IllegalStateException("the output is null!");
    }
    if (param == null)
        param = getDefaultWriteParam();
    // /////////////////////////////////////////////////////////////////////
    // 
    // Initial check on the capabilities of this writer as well as the
    // provided parameters.
    // 
    // /////////////////////////////////////////////////////////////////////
    final String driverName = (String) ((GDALImageWriterSpi) this.originatingProvider).getSupportedFormats().get(0);
    final DriverCreateCapabilities writingCapabilities = GDALUtilities.formatWritingCapabilities(driverName);
    if (writingCapabilities == GDALUtilities.DriverCreateCapabilities.READ_ONLY)
        throw new IllegalStateException("This writer seems to not support either create or create copy");
    if (image == null)
        throw new IllegalArgumentException("The provided input image is invalid.");
    // //
    // 
    // Getting the source image and its main properties
    // 
    // //
    final PlanarImage inputRenderedImage = PlanarImage.wrapRenderedImage(image.getRenderedImage());
    final int sourceWidth = inputRenderedImage.getWidth();
    final int sourceHeight = inputRenderedImage.getHeight();
    final int sourceMinX = inputRenderedImage.getMinX();
    final int sourceMinY = inputRenderedImage.getMinY();
    final int dataType = GDALUtilities.retrieveGDALDataBufferType(inputRenderedImage.getSampleModel().getDataType());
    final int nBands = inputRenderedImage.getNumBands();
    // //
    // 
    // Setting regions and sizes and retrieving parameters
    // 
    // //
    final int xSubsamplingFactor = param.getSourceXSubsampling();
    final int ySubsamplingFactor = param.getSourceYSubsampling();
    final Vector<String> myOptions = (Vector<String>) ((GDALImageWriteParam) param).getCreateOptionsHandler().getCreateOptions();
    Rectangle imageBounds = new Rectangle(sourceMinX, sourceMinY, sourceWidth, sourceHeight);
    Dimension destSize = new Dimension();
    computeRegions(imageBounds, destSize, param);
    // Destination sizes, needed for Dataset Creation
    final int destinationWidth = destSize.width;
    final int destinationHeight = destSize.height;
    // getting metadata before deciding if Create or CreateCopy will be used
    final IIOMetadata metadata = image.getMetadata();
    GDALCommonIIOImageMetadata imageMetadata = null;
    if (metadata != null) {
        if (metadata instanceof GDALCommonIIOImageMetadata) {
            imageMetadata = (GDALCommonIIOImageMetadata) metadata;
        } else {
        // TODO: build a metadata conversion to obtain an understandable
        // metadata object. Standard plugin-neutral format does not
        // contain really useful fields to be converted.
        // imageMetadata = new GDALWritableCommonIIOImageMetadata();
        // convertMetadata(IMAGE_METADATA_NAME, metadata,
        // imageMetadata);
        }
    }
    // /////////////////////////////////////////////////////////////////////
    // 
    // Some GDAL formats driver support both "Create" and "CreateCopy"
    // methods. Some others simply support "CreateCopy" method which only
    // allows to create a new File from an existing Dataset.
    // 
    // /////////////////////////////////////////////////////////////////////
    Dataset writeDataset = null;
    Driver driver = null;
    try {
        if (writingCapabilities == GDALUtilities.DriverCreateCapabilities.CREATE) {
            // /////////////////////////////////////////////////////////////////
            // 
            // Create is supported
            // -------------------
            // 
            // /////////////////////////////////////////////////////////////////
            // Retrieving the file name.
            final String fileName = outputFile.getAbsolutePath();
            // //
            // 
            // Dataset creation
            // 
            // //
            driver = gdal.GetDriverByName(driverName);
            writeDataset = driver.Create(fileName, destinationWidth, destinationHeight, nBands, dataType, myOptions);
            // //
            // 
            // Data Writing
            // 
            // //
            writeDataset = writeData(writeDataset, inputRenderedImage, imageBounds, nBands, dataType, xSubsamplingFactor, ySubsamplingFactor);
            // //
            if (imageMetadata != null) {
                setMetadata(writeDataset, imageMetadata);
            }
        } else {
            // ////////////////////////////////////////////////////////////////
            // 
            // Only CreateCopy is supported
            // ----------------------------------------------------------------
            // 
            // First of all, it is worth to point out that CreateCopy method
            // allows to create a File from an existing Dataset.
            // ////////////////////////////////////////////////////////////////
            driver = gdal.GetDriverByName(driverName);
            // //
            // 
            // Temporary Dataset creation from the originating image
            // 
            // //
            final File tempFile = File.createTempFile("datasetTemp", ".ds", null);
            Dataset tempDataset = null;
            try {
                tempDataset = createDatasetFromImage(inputRenderedImage, tempFile.getAbsolutePath(), imageBounds, nBands, dataType, destinationWidth, destinationHeight, xSubsamplingFactor, ySubsamplingFactor);
                tempDataset.FlushCache();
                // //
                if (imageMetadata != null) {
                    setMetadata(tempDataset, imageMetadata);
                }
                // //
                // 
                // Copy back the temporary dataset to the requested dataset
                // 
                // //
                writeDataset = driver.CreateCopy(outputFile.getPath(), tempDataset, 0, myOptions);
            } finally {
                if (tempDataset != null) {
                    try {
                        // Closing the dataset
                        GDALUtilities.closeDataSet(tempDataset);
                    } catch (Throwable e) {
                        if (LOGGER.isLoggable(Level.FINEST))
                            LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
                    }
                }
            }
            tempFile.delete();
        }
        // //
        // 
        // Flushing and closing dataset
        // 
        // //
        writeDataset.FlushCache();
    } finally {
        if (writeDataset != null) {
            try {
                // Closing the dataset
                GDALUtilities.closeDataSet(writeDataset);
            } catch (Throwable e) {
                if (LOGGER.isLoggable(Level.FINEST))
                    LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
            }
        }
        if (driver != null) {
            try {
                // Closing the driver
                driver.delete();
            } catch (Throwable e) {
                if (LOGGER.isLoggable(Level.FINEST))
                    LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
            }
        }
    }
}
Also used : Dataset(org.gdal.gdal.Dataset) Rectangle(java.awt.Rectangle) Driver(org.gdal.gdal.Driver) Dimension(java.awt.Dimension) IIOMetadata(javax.imageio.metadata.IIOMetadata) DriverCreateCapabilities(it.geosolutions.imageio.gdalframework.GDALUtilities.DriverCreateCapabilities) Vector(java.util.Vector) File(java.io.File) PlanarImage(javax.media.jai.PlanarImage)

Aggregations

DriverCreateCapabilities (it.geosolutions.imageio.gdalframework.GDALUtilities.DriverCreateCapabilities)1 Dimension (java.awt.Dimension)1 Rectangle (java.awt.Rectangle)1 File (java.io.File)1 Vector (java.util.Vector)1 IIOMetadata (javax.imageio.metadata.IIOMetadata)1 PlanarImage (javax.media.jai.PlanarImage)1 Dataset (org.gdal.gdal.Dataset)1 Driver (org.gdal.gdal.Driver)1