Search in sources :

Example 1 with GridCoverage2D

use of org.apache.sis.coverage.grid.GridCoverage2D in project sis by apache.

the class RasterReader method readAsCoverage.

/**
 * Parses a raster from the given input stream and returns as a coverage.
 *
 * @param  input  source of bytes to read.
 * @return the raster as a coverage, or {@code null} if the raster is empty.
 * @throws Exception in an error occurred while reading from the given input or creating the coverage.
 *         Exception type may be I/O, SQL, factory, data store, arithmetic, raster format, <i>etc.</i>,
 *         too numerous for enumerating them all.
 */
public GridCoverage readAsCoverage(final ChannelDataInput input) throws Exception {
    final BufferedImage image = readAsImage(input);
    if (image == null) {
        return null;
    }
    CoordinateReferenceSystem crs = null;
    final int srid = getSRID();
    if (spatialRefSys != null) {
        crs = spatialRefSys.fetchCRS(srid);
    } else if (srid > 0) {
        crs = CRS.forCode(Constants.EPSG + ':' + srid);
    }
    if (crs == null) {
        crs = defaultCRS;
    }
    final GridExtent extent = new GridExtent(image.getWidth(), image.getHeight());
    final GridGeometry domain = new GridGeometry(extent, ANCHOR, getGridToCRS(), crs);
    /*
         * Create pseudo-categories with a transfer function if we need to specify "no data" value,
         * or the sign of stored data do not match the sign of expected values.
         */
    List<SampleDimension> range = null;
    if (needsTransferFunction()) {
        final SampleDimension[] sd = new SampleDimension[bands.length];
        final SampleDimension.Builder builder = new SampleDimension.Builder();
        for (int b = 0; b < sd.length; b++) {
            final Band band = bands[b];
            if ((band.getDataBufferType() & OPPOSITE_SIGN) != 0) {
                // See `Band.OPPOSITE_SIGN` javadoc for more information on this limitation.
                throw new RasterFormatException("Data type not yet supported.");
            }
            sd[b] = builder.setName(b + 1).setBackground(band.noDataValue).build();
            builder.clear();
        }
        range = Arrays.asList(sd);
    }
    return new GridCoverage2D(domain, range, image);
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) SampleDimension(org.apache.sis.coverage.SampleDimension) BufferedImage(java.awt.image.BufferedImage) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RasterFormatException(java.awt.image.RasterFormatException)

Example 2 with GridCoverage2D

use of org.apache.sis.coverage.grid.GridCoverage2D in project sis by apache.

the class GridResourceMock method read.

/**
 * Returns a grid geometry wrapping a dummy image having exactly the requested size.
 * The image will always be a {@link BufferedImage} with pixel coordinates starting at (0,0).
 *
 * @param  domain  desired grid extent and resolution, or {@code null} for the whole domain.
 * @param  range   must be null, empty or a singleton containing only value 0.
 * @return the grid coverage for the specified domain.
 */
@Override
public GridCoverage read(GridGeometry domain, final int... range) {
    assertTrue(range == null || range.length == 0 || (range.length == 1 && range[0] == 0));
    if (domain == null) {
        domain = gridGeometry;
    } else {
        domain = gridGeometry.derive().subgrid(domain).build();
    }
    final GridExtent extent = domain.getExtent();
    final BufferedImage img = new BufferedImage(StrictMath.toIntExact(extent.getSize(0)), StrictMath.toIntExact(extent.getSize(1)), BufferedImage.TYPE_BYTE_BINARY);
    return new GridCoverage2D(domain, sampleDimensions, img);
}
Also used : GridExtent(org.apache.sis.coverage.grid.GridExtent) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) BufferedImage(java.awt.image.BufferedImage)

Example 3 with GridCoverage2D

use of org.apache.sis.coverage.grid.GridCoverage2D in project sis by apache.

the class CoverageCanvasApp method createImage.

/**
 * Creates a dummy image for testing purpose. Some tiles will
 * have artificial errors in order to see the error controls.
 */
private static GridCoverage2D createImage() {
    final Random random = new Random();
    final int width = TILE_WIDTH * 4;
    final int height = TILE_HEIGHT * 2;
    final TiledImageMock image = new TiledImageMock(DataBuffer.TYPE_BYTE, 1, // minX
    random.nextInt(50) - 25, // minY
    random.nextInt(50) - 25, width, height, TILE_WIDTH, TILE_HEIGHT, // minTileX
    random.nextInt(10) - 5, // minTileY
    random.nextInt(10) - 5, false);
    image.validate();
    final double sc = 500d / Math.max(width, height);
    final WritablePixelIterator it = WritablePixelIterator.create(image);
    while (it.next()) {
        final Point p = it.getPosition();
        final double d = Math.hypot(p.x - width / 2, p.y - height / 2);
        int value = 0;
        if ((Math.round(d) & 16) == 0) {
            value = Math.max(0, 255 - (int) (d * sc));
        }
        it.setSample(0, value);
    }
    image.failRandomly(random, false);
    return new GridCoverage2D(new GridGeometry(null, PixelInCell.CELL_CORNER, MathTransforms.identity(2), CommonCRS.Engineering.DISPLAY.crs()), null, image);
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) Random(java.util.Random) WritablePixelIterator(org.apache.sis.image.WritablePixelIterator) TiledImageMock(org.apache.sis.image.TiledImageMock) Point(java.awt.Point) Point(java.awt.Point)

Aggregations

GridCoverage2D (org.apache.sis.coverage.grid.GridCoverage2D)3 BufferedImage (java.awt.image.BufferedImage)2 GridExtent (org.apache.sis.coverage.grid.GridExtent)2 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)2 Point (java.awt.Point)1 RasterFormatException (java.awt.image.RasterFormatException)1 Random (java.util.Random)1 SampleDimension (org.apache.sis.coverage.SampleDimension)1 TiledImageMock (org.apache.sis.image.TiledImageMock)1 WritablePixelIterator (org.apache.sis.image.WritablePixelIterator)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1