Search in sources :

Example 6 with Dataset

use of org.gdal.gdal.Dataset 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)

Example 7 with Dataset

use of org.gdal.gdal.Dataset in project imageio-ext by geosolutions-it.

the class GDALCommonIIOImageMetadata method getGCPs.

/**
 * Returns the Ground Control Points
 */
public List<GCP> getGCPs() {
    if (super.getGCPs().isEmpty()) {
        Dataset ds = null;
        try {
            // Getting the number of GCPs
            final int nGCP = getGcpNumber();
            List<org.gdal.gdal.GCP> gcps = new Vector<org.gdal.gdal.GCP>(nGCP);
            ds = GDALUtilities.acquireDataSet(getDatasetName(), gdalconst.GA_ReadOnly);
            ds.GetGCPs((Vector<org.gdal.gdal.GCP>) gcps);
            // Scan GCPs
            if (gcps != null && !gcps.isEmpty()) {
                final List<GCP> groundControlPoints = new ArrayList<GCP>(nGCP);
                final Iterator<org.gdal.gdal.GCP> it = gcps.iterator();
                while (it.hasNext()) {
                    org.gdal.gdal.GCP gdalGcp = null;
                    try {
                        // Setting up a GCP
                        gdalGcp = (org.gdal.gdal.GCP) it.next();
                        GCP gcp = new GCP();
                        gcp.setId(gdalGcp.getId());
                        gcp.setDescription(gdalGcp.getInfo());
                        gcp.setColumn((int) gdalGcp.getGCPPixel());
                        gcp.setRow((int) gdalGcp.getGCPLine());
                        gcp.setEasting(gdalGcp.getGCPX());
                        gcp.setNorthing(gdalGcp.getGCPY());
                        gcp.setElevation(gdalGcp.getGCPZ());
                        groundControlPoints.add(gcp);
                    } finally {
                        if (gdalGcp != null) {
                            try {
                                // Releasing native GCP object
                                gdalGcp.delete();
                            } catch (Throwable e) {
                                if (LOGGER.isLoggable(Level.FINEST))
                                    LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
                            }
                        }
                    }
                }
                setGcps(groundControlPoints);
            }
        } finally {
            if (ds != null) {
                try {
                    // Closing the dataset
                    GDALUtilities.closeDataSet(ds);
                } catch (Throwable e) {
                    if (LOGGER.isLoggable(Level.FINEST))
                        LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
                }
            }
        }
    }
    return super.getGCPs();
}
Also used : GCP(it.geosolutions.imageio.core.GCP) Dataset(org.gdal.gdal.Dataset) org.gdal.gdal.gdal(org.gdal.gdal.gdal) ArrayList(java.util.ArrayList) Vector(java.util.Vector)

Example 8 with Dataset

use of org.gdal.gdal.Dataset in project imageio-ext by geosolutions-it.

the class GDALImageReader method setInput.

/**
 * Sets the input for the specialized reader.
 *
 * @throws IllegalArgumentException
 *                 if the provided input is <code>null</code>
 */
public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) {
    if (LOGGER.isLoggable(Level.FINE))
        LOGGER.fine("Setting Input");
    // check input
    if (input == null)
        throw new IllegalArgumentException("The provided input is null!");
    // is gdal available
    if (!GDALUtilities.isGDALAvailable())
        throw new IllegalStateException("GDAL native libraries are not available.");
    // to clear any value-object which was related to the previous input.
    if (this.imageInputStream != null) {
        reset();
        imageInputStream = null;
    }
    // //
    if (input instanceof File) {
        datasetSource = (File) input;
        try {
            imageInputStream = ImageIO.createImageInputStream(input);
        } catch (IOException e) {
            throw new RuntimeException("Failed to create a valid input stream ", e);
        }
    } else // //
    if (input instanceof FileImageInputStreamExt) {
        datasetSource = ((FileImageInputStreamExt) input).getFile();
        imageInputStream = (ImageInputStream) input;
    } else // //
    if (input instanceof URL) {
        final URL tempURL = (URL) input;
        if (tempURL.getProtocol().equalsIgnoreCase("file")) {
            try {
                datasetSource = ImageIOUtilities.urlToFile(tempURL);
                imageInputStream = ImageIO.createImageInputStream(input);
            } catch (IOException e) {
                throw new RuntimeException("Failed to create a valid input stream ", e);
            }
        }
    } else if (input instanceof URIImageInputStream) {
        imageInputStream = (URIImageInputStream) input;
        datasetSource = null;
        uriSource = ((URIImageInputStream) input).getUri();
    }
    // 
    // Checking if this input is of a supported format.
    // Now, I have an ImageInputStream and I can try to see if the input's
    // format is supported by the specialized reader
    // 
    boolean isInputDecodable = false;
    String mainDatasetName = null;
    Dataset mainDataSet = null;
    if (imageInputStream != null) {
        if (datasetSource != null) {
            mainDatasetName = datasetSource.getAbsolutePath();
            mainDataSet = GDALUtilities.acquireDataSet(datasetSource.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
        } else if (uriSource != null) {
            final String urisource = uriSource.toString();
            mainDatasetName = urisource;
            mainDataSet = GDALUtilities.acquireDataSet(urisource, gdalconstConstants.GA_ReadOnly);
        }
        if (mainDataSet != null) {
            isInputDecodable = ((GDALImageReaderSpi) this.getOriginatingProvider()).isDecodable(mainDataSet);
        } else
            isInputDecodable = false;
    }
    if (isInputDecodable) {
        // cache dataset
        datasetsMap.put(mainDatasetName, mainDataSet);
        // input is decodable
        super.setInput(imageInputStream, seekForwardOnly, ignoreMetadata);
        // Listing available subdatasets
        final List<String> subdatasets = mainDataSet.GetMetadata_List(GDALUtilities.GDALMetadataDomain.SUBDATASETS);
        // setting the number of subdatasets
        // It is worth to remind that the subdatasets vector
        // contains both Subdataset's Name and Subdataset's Description
        // Thus we need to divide its size by two.
        nSubdatasets = subdatasets.size() / 2;
        // Thus, theDataset is simply the main dataset.
        if (nSubdatasets == 0) {
            nSubdatasets = 1;
            datasetNames = new String[1];
            datasetNames[0] = mainDatasetName;
            datasetMetadataMap.put(datasetNames[0], this.createDatasetMetadata(mainDatasetName));
        } else {
            datasetNames = new String[nSubdatasets + 1];
            for (int i = 0; i < nSubdatasets; i++) {
                final String subdatasetName = (subdatasets.get(i * 2)).toString();
                final int nameStartAt = subdatasetName.lastIndexOf("_NAME=") + 6;
                datasetNames[i] = subdatasetName.substring(nameStartAt);
            }
            datasetNames[nSubdatasets] = mainDatasetName;
            datasetMetadataMap.put(datasetNames[nSubdatasets], createDatasetMetadata(mainDataSet, datasetNames[nSubdatasets]));
        }
        // clean list
        subdatasets.clear();
    } else {
        StringBuilder sb = new StringBuilder();
        if (imageInputStream == null) {
            sb.append("Unable to create a valid ImageInputStream for the provided input:");
            sb.append(GDALUtilities.NEWLINE);
            sb.append(input.toString());
        } else
            sb.append("The Provided input is not supported by this reader");
        throw new RuntimeException(sb.toString());
    }
}
Also used : Dataset(org.gdal.gdal.Dataset) ImageInputStream(javax.imageio.stream.ImageInputStream) URIImageInputStream(it.geosolutions.imageio.stream.input.URIImageInputStream) IOException(java.io.IOException) URL(java.net.URL) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) File(java.io.File) URIImageInputStream(it.geosolutions.imageio.stream.input.URIImageInputStream)

Example 9 with Dataset

use of org.gdal.gdal.Dataset in project imageio-ext by geosolutions-it.

the class GDALImageReader method getDatasetMetadata.

/**
 * Retrieves a {@link GDALCommonIIOImageMetadata} by index.
 *
 * @param imageIndex
 *                is the index of the required
 *                {@link GDALCommonIIOImageMetadata}.
 * @return a {@link GDALCommonIIOImageMetadata}
 */
public GDALCommonIIOImageMetadata getDatasetMetadata(final int imageIndex) {
    checkImageIndex(imageIndex);
    // getting dataset name
    final String datasetName = datasetNames[imageIndex];
    GDALCommonIIOImageMetadata retVal = datasetMetadataMap.get(datasetName);
    if (retVal == null) {
        // do we need to create a dataset
        Dataset ds = datasetsMap.get(datasetName);
        if (ds == null) {
            ds = GDALUtilities.acquireDataSet(datasetName, gdalconst.GA_ReadOnly);
            Dataset dsOld = datasetsMap.putIfAbsent(datasetName, ds);
            if (dsOld != null) {
                // abandon the DataSet we created
                GDALUtilities.closeDataSet(ds);
                ds = dsOld;
            }
        }
        // Add a new GDALCommonIIOImageMetadata to the HashMap
        final GDALCommonIIOImageMetadata datasetMetadataNew = createDatasetMetadata(datasetName);
        retVal = datasetMetadataMap.put(datasetName, datasetMetadataNew);
        if (retVal == null) {
            retVal = datasetMetadataNew;
        }
    }
    return retVal;
}
Also used : Dataset(org.gdal.gdal.Dataset)

Example 10 with Dataset

use of org.gdal.gdal.Dataset in project imageio-ext by geosolutions-it.

the class ECWImageReaderSpi method canDecodeInput.

/**
 * This method checks if the provided input can be decoded from this SPI
 */
public boolean canDecodeInput(Object input) throws IOException {
    if (input instanceof ECWPImageInputStream) {
        String ecwp = ((ECWPImageInputStream) input).getECWPLink();
        boolean isDecodeable = false;
        if (ecwp != null) {
            final Dataset ds = GDALUtilities.acquireDataSet(ecwp, gdalconst.GA_ReadOnly);
            if (ds != null)
                isDecodeable = isDecodable(ds);
        }
        return isDecodeable;
    } else
        return super.canDecodeInput(input);
}
Also used : Dataset(org.gdal.gdal.Dataset)

Aggregations

Dataset (org.gdal.gdal.Dataset)11 File (java.io.File)3 FileImageInputStreamExt (it.geosolutions.imageio.stream.input.FileImageInputStreamExt)2 Dimension (java.awt.Dimension)2 Rectangle (java.awt.Rectangle)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Vector (java.util.Vector)2 Driver (org.gdal.gdal.Driver)2 BoundingBox (com.revolsys.geometry.model.BoundingBox)1 PathResource (com.revolsys.spring.resource.PathResource)1 Resource (com.revolsys.spring.resource.Resource)1 GCP (it.geosolutions.imageio.core.GCP)1 DriverCreateCapabilities (it.geosolutions.imageio.gdalframework.GDALUtilities.DriverCreateCapabilities)1 EnhancedImageReadParam (it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam)1 URIImageInputStream (it.geosolutions.imageio.stream.input.URIImageInputStream)1 BandedSampleModel (java.awt.image.BandedSampleModel)1 ColorModel (java.awt.image.ColorModel)1 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)1