Search in sources :

Example 1 with RasterPresentation

use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.

the class RasterSymbolizerRenderer method presentations.

@Override
public Stream<Presentation> presentations(MapLayer layer, Feature feature) {
    final RasterSymbolizer sourceSymbol = symbol.getSource();
    final Object candidate = GO2Utilities.evaluate(sourceSymbol.getGeometry(), feature, null, null);
    GridCoverageResource ref = null;
    if (candidate instanceof GridCoverageResource) {
        ref = (GridCoverageResource) candidate;
    } else if (candidate instanceof GridCoverage) {
        ref = new InMemoryGridCoverageResource((GridCoverage) candidate);
    } else {
        return Stream.empty();
    }
    try {
        final int[] channelSelection = channelSelection(sourceSymbol, ref);
        final GridCoverage dataCoverage = getObjectiveCoverage(ref, renderingContext.getGridGeometry(), false, channelSelection);
        if (dataCoverage == null) {
            return Stream.empty();
        }
        final GridCoverage2D dataImage = applyStyle(ref, dataCoverage, sourceSymbol);
        final RasterPresentation rasterPresentation = new RasterPresentation(layer, ref, dataImage);
        rasterPresentation.forGrid(renderingContext);
        return Stream.concat(Stream.of(rasterPresentation), outline(layer, dataImage.getGridGeometry()));
    } catch (NoSuchDataException | DisjointExtentException e) {
        LOGGER.log(Level.FINE, "Disjoint exception: " + e.getMessage(), e);
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Portrayal exception: " + e.getMessage(), e);
    }
    return Stream.empty();
}
Also used : RasterSymbolizer(org.opengis.style.RasterSymbolizer) CachedRasterSymbolizer(org.geotoolkit.display2d.style.CachedRasterSymbolizer) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) PortrayalException(org.geotoolkit.display.PortrayalException) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) FactoryException(org.opengis.util.FactoryException) CannotEvaluateException(org.opengis.coverage.CannotEvaluateException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException)

Example 2 with RasterPresentation

use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.

the class RasterSymbolizerRenderer method presentations.

@Override
public Stream<Presentation> presentations(MapLayer layer, Resource rs) {
    if (rs instanceof BandedCoverageResource) {
        BandedCoverageResource bcr = (BandedCoverageResource) rs;
        try {
            GridCoverage coverage = BandedCoverageResource.sample(bcr, renderingContext.getGridGeometry2D());
            rs = new InMemoryGridCoverageResource(rs.getIdentifier().orElse(null), coverage);
        } catch (DataStoreException ex) {
            ExceptionPresentation ep = new ExceptionPresentation(ex);
            ep.setLayer(layer);
            ep.setResource(rs);
            return Stream.of(ep);
        }
    }
    if (rs instanceof GridCoverageResource) {
        GridCoverageResource ref = (GridCoverageResource) rs;
        try {
            final RasterSymbolizer sourceSymbol = symbol.getSource();
            final int[] channelSelection = channelSelection(sourceSymbol, ref);
            final GridCoverage dataCoverage = getObjectiveCoverage(ref, renderingContext.getGridGeometry(), false, channelSelection);
            if (dataCoverage == null) {
                return Stream.empty();
            }
            final GridCoverage2D dataImage = applyStyle(ref, dataCoverage, sourceSymbol);
            final RasterPresentation rasterPresentation = new RasterPresentation(layer, layer.getData(), dataImage);
            rasterPresentation.forGrid(renderingContext);
            return Stream.concat(Stream.of(rasterPresentation), outline(layer, dataImage.getGridGeometry()));
        } catch (NoSuchDataException | DisjointExtentException e) {
            LOGGER.log(Level.FINE, "Disjoint exception: " + e.getMessage(), e);
        } catch (Exception e) {
            ExceptionPresentation ep = new ExceptionPresentation(e);
            ep.setLayer(layer);
            ep.setResource(rs);
            return Stream.of(ep);
        }
    } else {
        return super.presentations(layer, rs);
    }
    return Stream.empty();
}
Also used : DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) DataStoreException(org.apache.sis.storage.DataStoreException) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) PortrayalException(org.geotoolkit.display.PortrayalException) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) FactoryException(org.opengis.util.FactoryException) CannotEvaluateException(org.opengis.coverage.CannotEvaluateException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) RasterSymbolizer(org.opengis.style.RasterSymbolizer) CachedRasterSymbolizer(org.geotoolkit.display2d.style.CachedRasterSymbolizer) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource)

Example 3 with RasterPresentation

use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.

the class DynamicRangeSymbolizerRenderer method presentations.

@Override
public Stream<Presentation> presentations(MapLayer layer, Resource resource) {
    if (resource instanceof GridCoverageResource) {
        try {
            final GridCoverageResource covref = (GridCoverageResource) resource;
            final DynamicRangeSymbolizer symbolizer = symbol.getSource();
            final int[] bands = new int[] { -1, -1, -1, -1 };
            final double[][] ranges = new double[][] { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } };
            final Map<String, Object> stats = new HashMap<>();
            // we can avoid loading metadata if all ranges are literals,
            // metadata can be expensive because of histogram informations
            boolean allLiteral = true;
            for (DynamicRangeSymbolizer.DRChannel channel : symbolizer.getChannels()) {
                allLiteral &= channel.getLower().getValue() instanceof Literal;
                allLiteral &= channel.getUpper().getValue() instanceof Literal;
            }
            CoverageDescription covdesc = null;
            if (!allLiteral) {
                if (covdesc == null) {
                    Metadata metadata = covref.getMetadata();
                    if (metadata != null) {
                        for (ContentInformation ci : metadata.getContentInfo()) {
                            if (ci instanceof CoverageDescription) {
                                covdesc = (CoverageDescription) ci;
                                break;
                            }
                        }
                    }
                }
                if (covdesc == null) {
                    // create an empty description
                    covdesc = new DefaultCoverageDescription();
                }
            }
            for (DynamicRangeSymbolizer.DRChannel channel : symbolizer.getChannels()) {
                final Integer bandIdx;
                try {
                    bandIdx = Integer.valueOf(channel.getBand());
                } catch (NumberFormatException ex) {
                    // not a number index
                    continue;
                }
                final String cs = channel.getColorSpaceComponent().trim();
                final int idx;
                if (DynamicRangeSymbolizer.DRChannel.BAND_RED.equalsIgnoreCase(cs))
                    idx = 0;
                else if (DynamicRangeSymbolizer.DRChannel.BAND_GREEN.equalsIgnoreCase(cs))
                    idx = 1;
                else if (DynamicRangeSymbolizer.DRChannel.BAND_BLUE.equalsIgnoreCase(cs))
                    idx = 2;
                else if (DynamicRangeSymbolizer.DRChannel.BAND_ALPHA.equalsIgnoreCase(cs))
                    idx = 3;
                else {
                    // no mapping
                    continue;
                }
                bands[idx] = bandIdx;
                // search for band statistics
                stats.clear();
                if (!allLiteral) {
                    search: for (AttributeGroup attg : covdesc.getAttributeGroups()) {
                        for (RangeDimension rd : attg.getAttributes()) {
                            if (!(rd instanceof org.opengis.metadata.content.SampleDimension))
                                continue;
                            final int i = Integer.parseInt(rd.getSequenceIdentifier().tip().toString());
                            if (i == bandIdx) {
                                final org.opengis.metadata.content.SampleDimension sd = (org.opengis.metadata.content.SampleDimension) rd;
                                stats.put(DynamicRangeSymbolizer.PROPERTY_MIN, sd.getMinValue());
                                stats.put(DynamicRangeSymbolizer.PROPERTY_MAX, sd.getMaxValue());
                                stats.put(DynamicRangeSymbolizer.PROPERTY_MEAN, sd.getMeanValue());
                                stats.put(DynamicRangeSymbolizer.PROPERTY_STD, sd.getStandardDeviation());
                                if (sd instanceof DefaultSampleDimensionExt) {
                                    final DefaultSampleDimensionExt dsd = (DefaultSampleDimensionExt) sd;
                                    stats.put(DynamicRangeSymbolizer.PROPERTY_HISTO, dsd.getHistogram());
                                    stats.put(DynamicRangeSymbolizer.PROPERTY_HISTO_MIN, dsd.getHistogramMin());
                                    stats.put(DynamicRangeSymbolizer.PROPERTY_HISTO_MAX, dsd.getHistogramMax());
                                }
                                break search;
                            }
                        }
                    }
                }
                ranges[idx][0] = evaluate(channel.getLower(), stats);
                ranges[idx][1] = evaluate(channel.getUpper(), stats);
            }
            // read only requested coverage bands
            int[] toRead = new int[0];
            final int[] mapping = new int[4];
            for (int i = 0; i < 4; i++) {
                if (bands[i] != -1) {
                    int index = 0;
                    boolean contained = false;
                    for (int k = 0; k < toRead.length; k++) {
                        if (toRead[k] == bands[i]) {
                            index = k;
                            contained = true;
                            break;
                        }
                    }
                    if (!contained) {
                        toRead = Arrays.copyOf(toRead, toRead.length + 1);
                        index = toRead.length - 1;
                        toRead[index] = bands[i];
                    }
                    mapping[i] = index;
                }
            }
            GridCoverage dataCoverage;
            try {
                dataCoverage = covref.read(renderingContext.getGridGeometry(), toRead);
            } catch (NoSuchDataException ex) {
                return Stream.empty();
            }
            if (dataCoverage == null) {
                return Stream.empty();
            }
            // check if the reader honored the band request
            final List<SampleDimension> readDimensions = dataCoverage.getSampleDimensions();
            final List<SampleDimension> sampleDimensions = covref.getSampleDimensions();
            boolean bandReadHonored = (readDimensions.size() == toRead.length);
            for (int i = 0; bandReadHonored && i < toRead.length; i++) {
                bandReadHonored &= Objects.equals(readDimensions.get(i).getName(), (sampleDimensions == null) ? null : sampleDimensions.get(toRead[i]).getName());
            }
            // swap new band indexes
            if (bandReadHonored) {
                for (int i = 0; i < 4; i++) {
                    if (bands[i] != -1)
                        bands[i] = mapping[i];
                }
            }
            dataCoverage = dataCoverage.forConvertedValues(true);
            final RenderedImage ri = dataCoverage.render(null);
            final DynamicRangeStretchProcess p = new DynamicRangeStretchProcess(ri, bands, ranges);
            RenderedImage img = p.executeNow();
            if (img instanceof WritableRenderedImage)
                GO2Utilities.removeBlackBorder((WritableRenderedImage) img);
            final GridCoverageBuilder gcb = new GridCoverageBuilder();
            gcb.setDomain(dataCoverage.getGridGeometry());
            gcb.setValues(img);
            final GridCoverage recolored = gcb.build();
            final RasterPresentation presentation = new RasterPresentation(layer, layer.getData(), recolored);
            presentation.forGrid(renderingContext);
            return Stream.of(presentation);
        } catch (DataStoreException e) {
            if (e.getCause() instanceof ProjectionException) {
                // out of domain exception
                monitor.exceptionOccured(e, Level.FINE);
            } else {
                monitor.exceptionOccured(e, Level.WARNING);
            }
        } catch (Exception e) {
            monitor.exceptionOccured(e, Level.WARNING);
        }
    }
    return Stream.empty();
}
Also used : HashMap(java.util.HashMap) Metadata(org.opengis.metadata.Metadata) DefaultCoverageDescription(org.apache.sis.metadata.iso.content.DefaultCoverageDescription) CoverageDescription(org.opengis.metadata.content.CoverageDescription) WritableRenderedImage(java.awt.image.WritableRenderedImage) DefaultSampleDimensionExt(org.geotoolkit.storage.coverage.DefaultSampleDimensionExt) DynamicRangeStretchProcess(org.geotoolkit.processing.image.dynamicrange.DynamicRangeStretchProcess) Literal(org.opengis.filter.Literal) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) DefaultCoverageDescription(org.apache.sis.metadata.iso.content.DefaultCoverageDescription) DataStoreException(org.apache.sis.storage.DataStoreException) ContentInformation(org.opengis.metadata.content.ContentInformation) AttributeGroup(org.opengis.metadata.content.AttributeGroup) SampleDimension(org.apache.sis.coverage.SampleDimension) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) ProjectionException(org.apache.sis.referencing.operation.projection.ProjectionException) ProjectionException(org.apache.sis.referencing.operation.projection.ProjectionException) PortrayalException(org.geotoolkit.display.PortrayalException) DataStoreException(org.apache.sis.storage.DataStoreException) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) RangeDimension(org.opengis.metadata.content.RangeDimension) WritableRenderedImage(java.awt.image.WritableRenderedImage) RenderedImage(java.awt.image.RenderedImage)

Example 4 with RasterPresentation

use of org.geotoolkit.display2d.presentation.RasterPresentation in project geotoolkit by Geomatys.

the class MapLayerJ2D method paintLayer.

/**
 * Render layer, will only be painted if an appropriate graphic builder is attached
 * to it.
 */
public Stream<Presentation> paintLayer(final RenderingContext2D context) throws PortrayalException, DataStoreException {
    // we abort painting if the layer is not visible.
    if (!item.isVisible())
        return Stream.empty();
    // we abort if opacity is to low
    final double opacity = item.getOpacity();
    if (opacity < 1e-6)
        return Stream.empty();
    if (1 - opacity < 1e-6) {
        // we are very close to opacity one, no need to create a intermediate image
        return streamPresentations(context);
    } else {
        // create an intermediate layer which will be painted on the main context
        // after with the given opacity
        final Rectangle rect = context.getCanvasDisplayBounds();
        final BufferedImage inter = createBufferedImage(ColorModel.getRGBdefault(), ColorModel.getRGBdefault().createCompatibleSampleModel(rect.width, rect.height));
        final Graphics2D g2d = inter.createGraphics();
        final RenderingContext2D interContext = context.create(g2d);
        final J2DPainter painter = new J2DPainter();
        try (Stream<Presentation> stream = streamPresentations(interContext)) {
            painter.paint(interContext, stream, true);
        }
        final GridCoverageBuilder gcb = new GridCoverageBuilder();
        gcb.setDomain(interContext.getGridGeometry());
        gcb.setValues(inter);
        final GridCoverage coverage = gcb.build();
        final RasterPresentation rp = new RasterPresentation(layer, null, coverage);
        rp.forGrid(context);
        rp.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) opacity);
        return Stream.of(rp);
    }
}
Also used : GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) Rectangle(java.awt.Rectangle) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) Presentation(org.apache.sis.internal.map.Presentation) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

GridCoverage (org.apache.sis.coverage.grid.GridCoverage)4 RasterPresentation (org.geotoolkit.display2d.presentation.RasterPresentation)4 DataStoreException (org.apache.sis.storage.DataStoreException)3 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)3 NoSuchDataException (org.apache.sis.storage.NoSuchDataException)3 PortrayalException (org.geotoolkit.display.PortrayalException)3 IOException (java.io.IOException)2 DisjointExtentException (org.apache.sis.coverage.grid.DisjointExtentException)2 GridCoverage2D (org.apache.sis.coverage.grid.GridCoverage2D)2 GridCoverageBuilder (org.apache.sis.coverage.grid.GridCoverageBuilder)2 CachedRasterSymbolizer (org.geotoolkit.display2d.style.CachedRasterSymbolizer)2 ProcessException (org.geotoolkit.process.ProcessException)2 InMemoryGridCoverageResource (org.geotoolkit.storage.memory.InMemoryGridCoverageResource)2 CannotEvaluateException (org.opengis.coverage.CannotEvaluateException)2 TransformException (org.opengis.referencing.operation.TransformException)2 RasterSymbolizer (org.opengis.style.RasterSymbolizer)2 FactoryException (org.opengis.util.FactoryException)2 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 BufferedImage (java.awt.image.BufferedImage)1