Search in sources :

Example 1 with AsciiGridRaster

use of it.geosolutions.imageio.plugins.arcgrid.raster.AsciiGridRaster in project imageio-ext by geosolutions-it.

the class AsciiGridsImageReaderSpi method canDecodeInput.

/**
 * This method check if the input source can be decoded by the reader
 * provided by this specific subclass of ImageReaderSpi. Return true if the
 * check was successfully passed input source type accepted and handled are
 * String, File, Url, InputStream and ImageInputStream.
 *
 * @see javax.imageio.spi.ImageReaderSpi#canDecodeInput(java.lang.Object)
 */
public boolean canDecodeInput(Object input) throws IOException {
    if (LOGGER.isLoggable(Level.FINE))
        LOGGER.info("canDecodeInput?");
    if (input instanceof ImageInputStream)
        ((ImageInputStream) input).mark();
    // /////////////////////////////////////////////////////////////////////
    // temp vars
    // /////////////////////////////////////////////////////////////////////
    AsciiGridRaster asciiRaster;
    ImageInputStream spiImageInputStream;
    // if the stream is opened here we need to close
    boolean closeMe = false;
    // convert input from String to File
    if (input instanceof String) {
        input = new File((String) input);
        closeMe = true;
    }
    // open an InputStream
    if (input instanceof URL) {
        final URL tempURL = (URL) input;
        if (tempURL.getProtocol().equalsIgnoreCase("file"))
            input = Utilities.urlToFile(tempURL);
        else
            input = ((URL) input).openStream();
        closeMe = true;
    }
    // convert input from File to FileInputStream
    if (input instanceof File) {
        input = ImageIO.createImageInputStream(input);
        closeMe = true;
    }
    // input source is it an InputStream?
    if (input instanceof InputStream) {
    /**
     * ISSUE on the GZipped test.
     *
     * Example A) final File f = new File("file.asc.gz"); final
     * InputStream is = new FileInputStream(f); Iterator it =
     * ImageIO.getImageReaders(stream);
     *
     * Example B) final File f = new File("file.asc.gz"); final
     * GZIPInputStream stream = new GZIPInputStream( new
     * FileInputStream(f)); Iterator it =
     * ImageIO.getImageReaders(stream);
     *
     * if we use the code of the Example A instead of that of Example B,
     * the parseHeader method (which works with ImageInputStreams) is
     * unable to try to read the header because the stream is GZipped.
     *
     * Thus, if we want to provide explicitly an InputStream (instead of
     * a File) for a GZipped File, we need to use the code of the
     * Example B which make use of the GZIPInputStream Subclass.
     */
    // creating an ImageInputStream from the InputStream
    // input = ImageInputStreamAdapter.getStream((InputStream) input);
    }
    // input source is it an ImageInputStream?
    if (input instanceof ImageInputStream) {
        ((ImageInputStream) input).mark();
        // marking the initial stream
        // casting from object to ImageInputStream and setting
        // the imageInputStream
        spiImageInputStream = (ImageInputStream) input;
    // if (!(spiImageInputStream instanceof GZIPImageInputStream)) {
    // 
    // try {
    // final ImageInputStream temp = new GZIPImageInputStream(
    // spiImageInputStream);
    // spiImageInputStream = temp;
    // } catch (IOException e) {
    // spiImageInputStream.reset();
    // 
    // }
    // }
    } else {
        return false;
    }
    // /////////////////////////////////////////////////////////////////////
    try {
        // Header Parsing to check if it is an EsriAsciiGridRaster
        asciiRaster = AsciiGridRaster.AsciiGridRasterType.ESRI.createAsciiGridRaster(spiImageInputStream);
        asciiRaster.parseHeader();
    } catch (IOException e) {
        try {
            // Header Parsing to know if it is a GrassAsciiGridRaster
            asciiRaster = AsciiGridRaster.AsciiGridRasterType.GRASS.createAsciiGridRaster(spiImageInputStream);
            asciiRaster.parseHeader();
        } catch (IOException e2) {
            // Input cannot be decoded
            if (LOGGER.isLoggable(Level.FINE))
                LOGGER.info("Unable to parse Header Succesfully. Format not recognized");
            // /////////////////////////////////////////////////////////////////////
            // reset the stream
            // /////////////////////////////////////////////////////////////////////
            ((ImageInputStream) input).reset();
            if (closeMe)
                spiImageInputStream.close();
            return false;
        }
    }
    // /////////////////////////////////////////////////////////////////////
    if (input instanceof ImageInputStream)
        ((ImageInputStream) input).reset();
    if (closeMe)
        spiImageInputStream.close();
    return true;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) AsciiGridRaster(it.geosolutions.imageio.plugins.arcgrid.raster.AsciiGridRaster) File(java.io.File) URL(java.net.URL)

Example 2 with AsciiGridRaster

use of it.geosolutions.imageio.plugins.arcgrid.raster.AsciiGridRaster in project imageio-ext by geosolutions-it.

the class AsciiGridTest method testReadAsEsriAndWriteAsGrass.

/**
 * Read an ESRI ArcGrid file and write it back as GRASS
 */
public void testReadAsEsriAndWriteAsGrass() throws FileNotFoundException, IOException {
    String title = new String("Simple JAI ImageRead operation test");
    File testFile = TestData.file(this, "095b_dem_90m.asc");
    ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
    pbjImageRead.setParameter("Input", testFile);
    RenderedOp image = JAI.create("ImageRead", pbjImageRead);
    if (TestData.isInteractiveTest()) {
        ImageIOUtilities.visualize(image, title, true);
    } else {
        assertNotNull(image.getTiles());
    }
    // //
    // 
    // Writing it out
    // 
    // //
    final File foutput = TestData.temp(this, "file.asc", true);
    final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
    pbjImageWrite.setParameter("Output", foutput);
    pbjImageWrite.addSource(image);
    final ImageReader reader = (ImageReader) image.getProperty(ImageReadDescriptor.PROPERTY_NAME_IMAGE_READER);
    final AsciiGridRaster raster = ((AsciiGridsImageReader) reader).getRasterReader();
    AsciiGridsImageMetadata grassMetadata = new AsciiGridsImageMetadata(raster.getNCols(), raster.getNRows(), raster.getCellSizeX(), raster.getCellSizeY(), raster.getXllCellCoordinate(), raster.getYllCellCoordinate(), raster.isCorner(), true, raster.getNoData());
    // writer.write (new IIOImage(image, null, grassMetadata));
    // writer.dispose();
    pbjImageWrite.setParameter("ImageMetadata", grassMetadata);
    pbjImageWrite.setParameter("Transcode", false);
    // //
    // 
    // What I am doing here is crucial, that is getting the used writer and
    // disposing it. This will force the underlying stream to write data on
    // disk.
    // 
    // //
    final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
    final ImageWriter writer = (ImageWriter) op.getProperty(ImageWriteDescriptor.PROPERTY_NAME_IMAGE_WRITER);
    writer.dispose();
    // //
    // 
    // Reading it back
    // 
    // //
    pbjImageRead = new ParameterBlockJAI("ImageRead");
    pbjImageRead.setParameter("Input", foutput);
    RenderedOp image2 = JAI.create("ImageRead", pbjImageRead);
    title = new String("Read Back the just written image");
    if (TestData.isInteractiveTest()) {
        ImageIOUtilities.visualize(image2, title, true);
    } else {
        assertNotNull(image2.getTiles());
    }
    final String[] error = new String[1];
    final boolean result = compare(image, image2, error, raster.getNoData());
    assertTrue(error[0], result);
}
Also used : RenderedOp(javax.media.jai.RenderedOp) ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ImageWriter(javax.imageio.ImageWriter) ImageReader(javax.imageio.ImageReader) File(java.io.File) AsciiGridRaster(it.geosolutions.imageio.plugins.arcgrid.raster.AsciiGridRaster)

Aggregations

AsciiGridRaster (it.geosolutions.imageio.plugins.arcgrid.raster.AsciiGridRaster)2 File (java.io.File)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ImageReader (javax.imageio.ImageReader)1 ImageWriter (javax.imageio.ImageWriter)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)1 RenderedOp (javax.media.jai.RenderedOp)1