Search in sources :

Example 1 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class CoverageImageTest method coverageReaderTest.

/**
 * Test between output image from renderer and source image within {@link GridCoverageReader}.
 *
 * @throws PortrayalException
 * @throws DataStoreException
 * @throws IOException
 */
@Test
public void coverageReaderTest() throws PortrayalException, DataStoreException, IOException {
    ImageIO.scanForPlugins();
    Setup.initialize(null);
    final File input = new File("src/test/resources/org/geotoolkit/display2d/clouds.jpg");
    final ImageCoverageReader reader = CoverageIO.createSimpleReader(input);
    final BufferedImage img = ImageIO.read(input);
    final GridCoverage gridcov = reader.read(null);
    proportionalityCoefficient = 2;
    final MapLayer cl = MapBuilder.createCoverageLayer(input);
    // Envelope result
    resEnv = gridcov.getGridGeometry().getEnvelope();
    srcWidth = img.getWidth();
    srcHeight = img.getHeight();
    testImageLayer(img, cl);
}
Also used : ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) MapLayer(org.apache.sis.portrayal.MapLayer) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 2 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class FileCoverageResource method getSampleDimensions.

@Override
public synchronized List<SampleDimension> getSampleDimensions() throws DataStoreException {
    if (cachedSampleDimensions == null) {
        final ImageCoverageReader reader = acquireReader();
        try {
            // Reader are allowed to return null when dimensions are unknown
            List<SampleDimension> sd = reader.getSampleDimensions();
            if (sd == null) {
                final GridCoverageReadParam param = new GridCoverageReadParam();
                GeneralEnvelope envelope = new GeneralEnvelope(reader.getGridGeometry().getEnvelope());
                for (int i = 0, n = envelope.getDimension(); i < n; i++) {
                    double min = envelope.getMinimum(i);
                    double span = envelope.getSpan(i);
                    envelope.setRange(i, min, min + span / 100.0);
                }
                param.setEnvelope(reader.getGridGeometry().getEnvelope());
                param.setDeferred(true);
                final GridCoverage coverage = reader.read(param);
                final RenderedImage img = coverage.render(null);
                final SampleModel sampleModel = img.getSampleModel();
                final int numBands = sampleModel.getNumBands();
                final NumberRange<?> range = TypeMap.getRange(TypeMap.getSampleDimensionType(sampleModel, 0));
                sd = new ArrayList<>();
                for (int i = 0; i < numBands; i++) {
                    sd.add(new SampleDimension.Builder().setName(i).addQuantitative("data", range, (MathTransform1D) MathTransforms.linear(1.0, 0.0), Units.UNITY).build());
                }
            }
            cachedSampleDimensions = Collections.unmodifiableList(sd);
        } finally {
            try {
                reader.dispose();
            } catch (DataStoreException ex) {
                LOGGER.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
    return cachedSampleDimensions;
}
Also used : ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) DataStoreException(org.apache.sis.storage.DataStoreException) SampleDimension(org.apache.sis.coverage.SampleDimension) SampleModel(java.awt.image.SampleModel) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageReadParam(org.geotoolkit.coverage.io.GridCoverageReadParam) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) RenderedImage(java.awt.image.RenderedImage)

Example 3 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class CoverageReaderDemo method main.

public static void main(String[] args) throws Exception {
    Demos.init();
    // Create a temp file to extract data from jar file.
    final Path tempData = Files.createTempFile("tempCvg", ".grb");
    try (InputStream stream = CoverageReaderDemo.class.getResourceAsStream("/data/grib/Atlantic.wave.grb")) {
        IOUtilities.writeStream(stream, tempData);
    }
    /*
         * DEFERRED READING
         */
    final GridCoverageReadParam readParam = new GridCoverageReadParam();
    // Here is the parameter which tells the reader to perform lazy loading.
    readParam.setDeferred(true);
    final ImageCoverageReader reader = CoverageIO.createSimpleReader(tempData);
    final GridCoverage coverage = reader.read(readParam);
    // Ok, so how to use it now ?
    // You can get pixel values directly.
    coverage.evaluator().apply(new GeneralDirectPosition(-100, 10, 0));
    // But in most cases ...
    // ... You will acquire iterator for fast and safe browsing.
    final RenderedImage cvgData = coverage.render(null);
    final PixelIterator pxIterator = PixelIterator.create(cvgData);
    // What should you avoid to do with deferred reading ?
    // Don't asked for the entire Raster of the image, it load all image data in memory.
    cvgData.getData();
    // Do not close your coverage reader before you've ended using your coverage, it would close connexion to the source,
    // and tile loading will return you an error.
    reader.dispose();
    try {
        cvgData.getTile(cvgData.getMinTileX(), cvgData.getMinTileY());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // final File input = new File(CoverageReaderDemo.class.getResource("/data/coverage/clouds.jpg").toURI());
    // final GridCoverageReader reader = CoverageIO.createSimpleReader(input);
    // //print the iso 19115 metadata
    // final Metadata metadata = reader.getMetadata();
    // System.out.println(metadata);
    // 
    // //read a piece of coverage
    // final GridCoverageReadParam param = new GridCoverageReadParam();
    // param.setResolution(1,1);
    // param.setEnvelope(new Rectangle2D.Double(0, 0, 100, 100), CommonCRS.WGS84.normalizedGeographic());
    // 
    // final GridCoverage2D coverage = (GridCoverage2D) reader.read(0, param);
    // coverage.show();
    /*
         * USING THE RENDERER.
         */
    // create a mapcontext
    final MapLayers context = MapBuilder.createContext();
    final MapLayer cl = MapBuilder.createCoverageLayer(tempData);
    context.getComponents().add(cl);
// display it
// FXMapFrame.show(context);
}
Also used : Path(java.nio.file.Path) ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) PixelIterator(org.apache.sis.image.PixelIterator) InputStream(java.io.InputStream) MapLayer(org.apache.sis.portrayal.MapLayer) GridCoverageReadParam(org.geotoolkit.coverage.io.GridCoverageReadParam) RenderedImage(java.awt.image.RenderedImage) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 4 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class ReferenceToGridCoverageResourceConverter method convert.

/**
 * {@inheritDoc}
 *
 * @return GridCoverageResource.
 */
@Override
public GridCoverageResource convert(final Reference source, final Map<String, Object> params) throws UnconvertibleObjectException {
    final InputStream stream = getInputStreamFromReference(source);
    String encoding = null;
    if (params != null && params.get(ENCODING) != null) {
        encoding = (String) params.get(ENCODING);
    }
    ImageInputStream imageStream = null;
    try {
        // decode form base64 stream
        if (encoding != null && encoding.equals(WPSEncoding.BASE64.getValue())) {
            final String encodedImage = IOUtilities.toString(stream);
            final byte[] byteData = Base64.getDecoder().decode(encodedImage.trim());
            if (byteData != null && byteData.length > 0) {
                try (InputStream is = new ByteArrayInputStream(byteData)) {
                    imageStream = ImageIO.createImageInputStream(is);
                }
            }
        } else {
            imageStream = ImageIO.createImageInputStream(stream);
        }
        if (imageStream != null) {
            final ImageReader reader;
            if (source.getMimeType() != null) {
                reader = XImageIO.getReaderByMIMEType(source.getMimeType(), imageStream, null, null);
            } else {
                reader = XImageIO.getReader(imageStream, null, Boolean.FALSE);
            }
            ImageCoverageReader imgReader = CoverageIO.createSimpleReader(reader);
            GridCoverage cov2d = imgReader.read(new GridCoverageReadParam());
            return new InMemoryGridCoverageResource(cov2d);
        } else {
            throw new UnconvertibleObjectException("Error during image stream acquisition.");
        }
    } catch (MalformedURLException ex) {
        throw new UnconvertibleObjectException("ReferenceType grid coverage invalid input : Malformed url", ex);
    } catch (DataStoreException ex) {
        throw new UnconvertibleObjectException("ReferenceType grid coverage invalid input : Can't read coverage", ex);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException("ReferenceType grid coverage invalid input : IO", ex);
    } finally {
        if (imageStream != null) {
            try {
                imageStream.close();
            } catch (IOException ex) {
                LOGGER.log(Level.WARNING, "Error during release the image stream.", ex);
            }
        }
    }
}
Also used : ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) MalformedURLException(java.net.MalformedURLException) DataStoreException(org.apache.sis.storage.DataStoreException) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) ByteArrayInputStream(java.io.ByteArrayInputStream) GridCoverageReadParam(org.geotoolkit.coverage.io.GridCoverageReadParam) ImageReader(javax.imageio.ImageReader)

Example 5 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class GeoTiffRWTest method test.

private void test(final String fileName, final CoordinateReferenceSystem crs, final AffineTransform gridToCRS) throws IOException, DataStoreException {
    File file = TestData.file(GeoTiffRWTest.class, fileName);
    ImageCoverageReader reader = new ImageCoverageReader();
    reader.setInput(file);
    try {
        // first test
        GridCoverage coverage = reader.read(null);
        compare(coverage, crs, gridToCRS);
        // write it and test again
        file = write(coverage, reader.getCoverageMetadata());
        reader.dispose();
        final ImageReader imgReader = new TiffImageReader(new TiffImageReader.Spi());
        imgReader.setInput(file);
        reader = (ImageCoverageReader) CoverageIO.createSimpleReader(imgReader);
        // second test
        coverage = reader.read(null);
        compare(coverage, crs, gridToCRS);
    } finally {
        reader.dispose();
    }
}
Also used : ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) TiffImageReader(org.geotoolkit.image.io.plugin.TiffImageReader) ImageReader(javax.imageio.ImageReader) TiffImageReader(org.geotoolkit.image.io.plugin.TiffImageReader) File(java.io.File)

Aggregations

ImageCoverageReader (org.geotoolkit.coverage.io.ImageCoverageReader)11 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)7 DataStoreException (org.apache.sis.storage.DataStoreException)6 File (java.io.File)4 IOException (java.io.IOException)4 ImageReader (javax.imageio.ImageReader)4 GridCoverageReadParam (org.geotoolkit.coverage.io.GridCoverageReadParam)4 InputStream (java.io.InputStream)3 MalformedURLException (java.net.MalformedURLException)3 Path (java.nio.file.Path)3 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)3 RenderedImage (java.awt.image.RenderedImage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 SampleDimension (org.apache.sis.coverage.SampleDimension)2 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)2 MapLayer (org.apache.sis.portrayal.MapLayer)2 TiffImageReader (org.geotoolkit.image.io.plugin.TiffImageReader)2 Test (org.junit.Test)2 Envelope (org.opengis.geometry.Envelope)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1