Search in sources :

Example 1 with DRChannel

use of org.geotoolkit.display2d.ext.dynamicrange.DynamicRangeSymbolizer.DRChannel in project geotoolkit by Geomatys.

the class GO2Utilities method inferStyle.

/**
 * Try to create a simple style from given statistics. For now, two cases exists:
 * <ol>
 *     <li>
 *         Less than 3 bands are detected. In this case, we simply create a color map over first band. The
 *         color map make use of standard deviation to compute interpolation boundaries.
 *     </li>
 *     <li>
 *         For 3 bands or more, we create a dynamic range RGB rendering based on first bands in the statistics. We
 *         define value boundary for each band using {@link #buildRanges(ImageStatistics, int) a custom empiric
 *         method removing extremums}.
 *     </li>
 * </ol>
 * @param stats The statistics to use for style creation. Can be null or empty, in which case nothing is returned.
 * @param forceAlpha A flag used only if given statistics contains more than 3 bands. Indicates that for RGB styling,
 *                   we must interpret 4th band as alpha component.
 * @return A style inferred from input statistics, or an empty shell if given argument does not contains enough
 * information.
 * @throws IOException It can happen when trying to access a color map definition on disk, but it fails.
 */
public static Optional<MutableStyle> inferStyle(final ImageStatistics stats, final boolean forceAlpha) throws IOException {
    if (stats == null)
        return Optional.empty();
    final ImageStatistics.Band[] bands = stats.getBands();
    if (bands == null || bands.length < 1)
        return Optional.empty();
    if (bands.length < 3) {
        LOGGER.log(Level.FINE, "applyColorMapStyle : fallBack way is choosen." + "GrayScale interpretation of the first coverage image band.");
        final ImageStatistics.Band band0 = bands[0];
        final Double bmin = band0.getMin();
        final Double bmax = band0.getMax();
        if (bmin == null || bmax == null || !Double.isFinite(bmin) || !Double.isFinite(bmax)) {
            LOGGER.log(Level.WARNING, "Image statistics object is present but contains null or non-finite extremas. Ignore it");
            return Optional.empty();
        }
        double palMin = bmin;
        double palMax = bmax;
        final Double mean = band0.getMean();
        final Double std = band0.getStd();
        if (mean != null && std != null && Double.isFinite(mean) && Double.isFinite(std)) {
            palMin = Math.max(bmin, mean - 2 * std);
            palMax = Math.min(bmax, mean + 2 * std);
        }
        if (!Double.isFinite(palMin) || !Double.isFinite(palMax)) {
            LOGGER.finest("Adapting rendering distribution failed. Fallback on input min/max");
            palMin = bmin;
            palMax = bmax;
        }
        assert Double.isFinite(palMin) : "Raster Style fallback : minimum value should be finite. min = " + palMin;
        assert Double.isFinite(palMax) : "Raster Style fallback : maximum value should be finite. max = " + palMax;
        assert palMin >= bmin;
        assert palMax <= bmax;
        final List<InterpolationPoint> values = new ArrayList<>();
        final double[] nodatas = band0.getNoData();
        if (nodatas != null)
            for (double nodata : nodatas) {
                values.add(STYLE_FACTORY.interpolationPoint(nodata, STYLE_FACTORY.literal(new Color(0, 0, 0, 0))));
            }
        values.add(STYLE_FACTORY.interpolationPoint(Float.NaN, STYLE_FACTORY.literal(new Color(0, 0, 0, 0))));
        // -- Color palette
        // Color[] colorsPal = PaletteFactory.getDefault().getColors("rainbow-t");
        Color[] colorsPal = PaletteFactory.getDefault().getColors("grayscale");
        assert colorsPal.length >= 2;
        if (colorsPal.length < 4) {
            final double percent_5 = (colorsPal.length == 3) ? 0.1 : 0.05;
            final Color[] colorsPalTemp = colorsPal;
            colorsPal = Arrays.copyOf(colorsPal, colorsPal.length + 2);
            System.arraycopy(colorsPalTemp, 2, colorsPal, 2, colorsPalTemp.length - 2);
            colorsPal[colorsPal.length - 1] = colorsPalTemp[colorsPalTemp.length - 1];
            colorsPal[1] = DefaultInterpolate.interpolate(colorsPalTemp[0], colorsPalTemp[1], percent_5);
            colorsPal[colorsPal.length - 2] = DefaultInterpolate.interpolate(colorsPalTemp[colorsPalTemp.length - 2], colorsPalTemp[colorsPalTemp.length - 1], 1 - percent_5);
        }
        // -- if difference between band minimum statistic and palette minimum,
        // -- define values between them as transparency
        values.add(STYLE_FACTORY.interpolationPoint(bmin, STYLE_FACTORY.literal(colorsPal[0])));
        assert colorsPal.length >= 4;
        // -- min and max transparency
        final double step = (palMax - palMin) / (colorsPal.length - 3);
        double currentVal = palMin;
        for (int c = 1; c <= colorsPal.length - 2; c++) {
            values.add(STYLE_FACTORY.interpolationPoint(currentVal, STYLE_FACTORY.literal(colorsPal[c])));
            currentVal += step;
        }
        assert StrictMath.abs(currentVal - step - palMax) < 1E-9;
        values.add(STYLE_FACTORY.interpolationPoint(bmax, STYLE_FACTORY.literal(colorsPal[colorsPal.length - 1])));
        final Expression function = STYLE_FACTORY.interpolateFunction(DEFAULT_CATEGORIZE_LOOKUP, values, Method.COLOR, Mode.LINEAR, DEFAULT_FALLBACK);
        final ColorMap colorMap = STYLE_FACTORY.colorMap(function);
        final RasterSymbolizer symbol = STYLE_FACTORY.rasterSymbolizer(band0.getName(), null, null, null, colorMap, null, null, null);
        return Optional.of(STYLE_FACTORY.style(symbol));
    } else {
        LOGGER.log(Level.FINE, "RGBStyle : fallBack way is choosen." + "RGB interpretation of the three first coverage image bands.");
        final int rgbNumBand = forceAlpha && bands.length > 3 ? 4 : 3;
        assert rgbNumBand <= bands.length;
        final double[][] ranges = buildRanges(stats, 4);
        final List<DRChannel> channels = new ArrayList<>();
        for (int i = 0; i < rgbNumBand; i++) {
            final DRChannel channel = new DRChannel();
            final String bandName = bands[i].getName();
            channel.setBand(bandName == null || bandName.isEmpty() ? Integer.toString(i) : bandName);
            channel.setColorSpaceComponent(DynamicRangeSymbolizer.DRChannel.RGBA_COMPONENTS[i]);
            DynamicRangeSymbolizer.DRBound drBound = new DynamicRangeSymbolizer.DRBound();
            drBound.setValue(FILTER_FACTORY.literal(ranges[i][0]));
            channel.setLower(drBound);
            drBound = new DynamicRangeSymbolizer.DRBound();
            drBound.setValue(FILTER_FACTORY.literal(ranges[i][1]));
            channel.setUpper(drBound);
            channels.add(channel);
        }
        final DynamicRangeSymbolizer drgb = new DynamicRangeSymbolizer();
        drgb.setChannels(channels);
        return Optional.of(STYLE_FACTORY.style(drgb));
    }
}
Also used : InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) DRChannel(org.geotoolkit.display2d.ext.dynamicrange.DynamicRangeSymbolizer.DRChannel) Color(java.awt.Color) ArrayList(java.util.ArrayList) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) MultiPoint(org.locationtech.jts.geom.MultiPoint) Point(org.locationtech.jts.geom.Point) Paint(java.awt.Paint) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) DynamicRangeSymbolizer(org.geotoolkit.display2d.ext.dynamicrange.DynamicRangeSymbolizer) Expression(org.opengis.filter.Expression) ImageStatistics(org.geotoolkit.storage.coverage.ImageStatistics)

Aggregations

Color (java.awt.Color)1 Paint (java.awt.Paint)1 ArrayList (java.util.ArrayList)1 DynamicRangeSymbolizer (org.geotoolkit.display2d.ext.dynamicrange.DynamicRangeSymbolizer)1 DRChannel (org.geotoolkit.display2d.ext.dynamicrange.DynamicRangeSymbolizer.DRChannel)1 ImageStatistics (org.geotoolkit.storage.coverage.ImageStatistics)1 InterpolationPoint (org.geotoolkit.style.function.InterpolationPoint)1 LineString (org.locationtech.jts.geom.LineString)1 MultiLineString (org.locationtech.jts.geom.MultiLineString)1 MultiPoint (org.locationtech.jts.geom.MultiPoint)1 Point (org.locationtech.jts.geom.Point)1 Expression (org.opengis.filter.Expression)1