Search in sources :

Example 1 with DefaultInterpolationPoint

use of org.geotoolkit.style.function.DefaultInterpolationPoint in project geotoolkit by Geomatys.

the class StyleCacheTest method GraphicCacheTest.

@Test
public void GraphicCacheTest() throws Exception {
    // Test a complex graphic
    final Expression Lookup = FF.property("POP_CNTRY");
    final List<InterpolationPoint> values = new ArrayList<InterpolationPoint>();
    // test color interpolation ---------------------------------------------
    values.clear();
    values.add(new DefaultInterpolationPoint(0d, FF.literal(3d)));
    values.add(new DefaultInterpolationPoint(500000000d, FF.literal(50d)));
    Interpolate interpolate = new DefaultInterpolate(Lookup, values, Method.COLOR, Mode.CUBIC, null);
    List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
    symbols.add(SF.mark(StyleConstants.MARK_CIRCLE, SF.fill(Color.RED), SF.stroke()));
    Graphic graphic = SF.graphic(symbols, StyleConstants.DEFAULT_GRAPHIC_OPACITY, interpolate, StyleConstants.DEFAULT_GRAPHIC_ROTATION, StyleConstants.DEFAULT_ANCHOR_POINT, StyleConstants.DEFAULT_DISPLACEMENT);
    CachedGraphic cached = CachedGraphic.cache(graphic);
    assertFalse(cached.isStatic());
    assertEquals(VisibilityState.DYNAMIC, cached.isStaticVisible());
}
Also used : DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) DefaultInterpolate(org.geotoolkit.style.function.DefaultInterpolate) Interpolate(org.geotoolkit.style.function.Interpolate) CachedGraphic(org.geotoolkit.display2d.style.CachedGraphic) Expression(org.opengis.filter.Expression) Graphic(org.opengis.style.Graphic) CachedGraphic(org.geotoolkit.display2d.style.CachedGraphic) ExternalGraphic(org.opengis.style.ExternalGraphic) GraphicalSymbol(org.opengis.style.GraphicalSymbol) ArrayList(java.util.ArrayList) DefaultInterpolate(org.geotoolkit.style.function.DefaultInterpolate) DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) Test(org.junit.Test)

Example 2 with DefaultInterpolationPoint

use of org.geotoolkit.style.function.DefaultInterpolationPoint in project geotoolkit by Geomatys.

the class RasterPresentation method renderCoverage.

private boolean renderCoverage(RenderingContext2D renderingContext, org.apache.sis.coverage.grid.GridCoverage coverage, MathTransform trs2D) throws PortrayalException {
    final Graphics2D g2d = renderingContext.getGraphics();
    final CanvasMonitor monitor = renderingContext.getMonitor();
    boolean dataRendered = false;
    RenderedImage img = coverage.render(null);
    /*
         * Try to prefetch image before rendering
         * resampled image or mosaic have deferred tiles
         * java2d render tiles one by one which can be slow when working with
         * computed coverages or distant services like WMTS or TMS
         */
    if ((img.getWidth() * img.getHeight()) < 5000 * 5000) {
        ImageProcessor processor = new ImageProcessor();
        processor.setExecutionMode(ImageProcessor.Mode.PARALLEL);
        img = processor.prefetch(img, null);
    }
    final InterpolationCase interpolationCase = (InterpolationCase) renderingContext.getRenderingHints().get(GO2Hints.KEY_INTERPOLATION);
    if (interpolationCase != null) {
        switch(interpolationCase) {
            case NEIGHBOR:
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
                break;
            case BILINEAR:
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                break;
            case BICUBIC:
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                break;
            default:
                // resample image ourself
                try {
                    GridCoverage cov = new ResampleProcess(coverage, renderingContext.getGridGeometry().getCoordinateReferenceSystem(), renderingContext.getGridGeometry2D(), interpolationCase, null).executeNow();
                    trs2D = renderingTransform(cov.getGridGeometry());
                    img = cov.render(null);
                } catch (ProcessException ex) {
                    throw new PortrayalException(ex);
                }
                break;
        }
    }
    if (trs2D instanceof AffineTransform) {
        try {
            g2d.drawRenderedImage(RenderingWorkaround.wrap(img), (AffineTransform) trs2D);
            dataRendered = true;
        } catch (Exception ex) {
            final StringWriter sw = new StringWriter();
            final PrintWriter pw = new PrintWriter(sw);
            ex.printStackTrace(pw);
            if (ex instanceof ArrayIndexOutOfBoundsException) {
                // we can recover when it's an inapropriate componentcolormodel
                final StackTraceElement[] eles = ex.getStackTrace();
                if (eles.length > 0 && ComponentColorModel.class.getName().equalsIgnoreCase(eles[0].getClassName())) {
                    try {
                        final Map<String, Object> analyze = StatisticOp.analyze(img);
                        final double[] minArray = (double[]) analyze.get(StatisticOp.MINIMUM);
                        final double[] maxArray = (double[]) analyze.get(StatisticOp.MAXIMUM);
                        final double min = findExtremum(minArray, true);
                        final double max = findExtremum(maxArray, false);
                        final List<InterpolationPoint> values = new ArrayList<>();
                        values.add(new DefaultInterpolationPoint(Double.NaN, GO2Utilities.STYLE_FACTORY.literal(new Color(0, 0, 0, 0))));
                        values.add(new DefaultInterpolationPoint(min, GO2Utilities.STYLE_FACTORY.literal(Color.BLACK)));
                        values.add(new DefaultInterpolationPoint(max, GO2Utilities.STYLE_FACTORY.literal(Color.WHITE)));
                        final Literal lookup = StyleConstants.DEFAULT_CATEGORIZE_LOOKUP;
                        final Literal fallback = StyleConstants.DEFAULT_FALLBACK;
                        final Expression function = GO2Utilities.STYLE_FACTORY.interpolateFunction(lookup, values, Method.COLOR, Mode.LINEAR, fallback);
                        final CompatibleColorModel model = new CompatibleColorModel(img.getColorModel().getPixelSize(), function);
                        final ImageLayout layout = new ImageLayout().setColorModel(model);
                        img = new NullOpImage(img, layout, null, OpImage.OP_COMPUTE_BOUND);
                        g2d.drawRenderedImage(RenderingWorkaround.wrap(img), (AffineTransform) trs2D);
                        dataRendered = true;
                    } catch (Exception e) {
                        // plenty of errors can happen when painting an image
                        monitor.exceptionOccured(e, Level.WARNING);
                        // raise the original error
                        monitor.exceptionOccured(ex, Level.WARNING);
                    }
                } else {
                    // plenty of errors can happen when painting an image
                    monitor.exceptionOccured(ex, Level.WARNING);
                }
            } else {
                // plenty of errors can happen when painting an image
                monitor.exceptionOccured(ex, Level.WARNING);
            }
        }
    } else if (trs2D instanceof LinearTransform) {
        final LinearTransform lt = (LinearTransform) trs2D;
        final int col = lt.getMatrix().getNumCol();
        final int row = lt.getMatrix().getNumRow();
        // TODO using only the first parameters of the linear transform
        throw new PortrayalException("Could not render image, GridToCRS is a not an AffineTransform, found a " + trs2D.getClass());
    } else {
        throw new PortrayalException("Could not render image, GridToCRS is a not an AffineTransform, found a " + trs2D.getClass());
    }
    return dataRendered;
}
Also used : CompatibleColorModel(org.geotoolkit.style.function.CompatibleColorModel) ResampleProcess(org.geotoolkit.processing.coverage.resample.ResampleProcess) ComponentColorModel(java.awt.image.ComponentColorModel) ImageProcessor(org.apache.sis.image.ImageProcessor) StringWriter(java.io.StringWriter) Literal(org.opengis.filter.Literal) List(java.util.List) ArrayList(java.util.ArrayList) ImageLayout(javax.media.jai.ImageLayout) PrintWriter(java.io.PrintWriter) Color(java.awt.Color) InterpolationCase(org.geotoolkit.image.interpolation.InterpolationCase) NullOpImage(javax.media.jai.NullOpImage) LinearTransform(org.apache.sis.referencing.operation.transform.LinearTransform) PortrayalException(org.geotoolkit.display.PortrayalException) TransformException(org.opengis.referencing.operation.TransformException) FactoryException(org.opengis.util.FactoryException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) ProcessException(org.geotoolkit.process.ProcessException) Graphics2D(java.awt.Graphics2D) ProcessException(org.geotoolkit.process.ProcessException) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) Expression(org.opengis.filter.Expression) AffineTransform(java.awt.geom.AffineTransform) RenderedImage(java.awt.image.RenderedImage) Map(java.util.Map) PortrayalException(org.geotoolkit.display.PortrayalException) DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint)

Example 3 with DefaultInterpolationPoint

use of org.geotoolkit.style.function.DefaultInterpolationPoint in project geotoolkit by Geomatys.

the class PaletteReaderTest method readCLR.

@Test
public void readCLR() throws IOException {
    String palette = "ColorMap 1 1\n" + "0.000000 143 0 0\n" + "500.000000 244 0 0\n" + "1000.000000 255 89 0\n" + "1500.000000 255 189 0\n" + "2000.000000 236 255 34\n" + "2500.000000 135 255 135\n" + "3000.000000 34 255 236\n" + "3500.000000 0 189 255\n" + "4000.000000 0 89 255\n" + "4500.000000 0 0 244\n" + "5000.000000 0 0 143";
    final ColorMap cm = new PaletteReader(PaletteReader.PATTERN_CLR).read(palette);
    assertTrue(cm.getFunction() instanceof Interpolate);
    final Interpolate interpolate = (Interpolate) cm.getFunction();
    final List<InterpolationPoint> steps = interpolate.getInterpolationPoints();
    assertEquals(12, steps.size());
    for (int i = 0; i < steps.size(); i++) {
        final InterpolationPoint step = steps.get(i);
        InterpolationPoint expected = null;
        switch(i) {
            case 0:
                expected = new DefaultInterpolationPoint(Double.NaN, SF.literal(new Color(0, 0, 0, 0)));
                break;
            case 1:
                expected = new DefaultInterpolationPoint(0d, SF.literal(new Color(143, 0, 0)));
                break;
            case 2:
                expected = new DefaultInterpolationPoint(500d, SF.literal(new Color(244, 0, 0)));
                break;
            case 3:
                expected = new DefaultInterpolationPoint(1000d, SF.literal(new Color(255, 89, 0)));
                break;
            case 4:
                expected = new DefaultInterpolationPoint(1500d, SF.literal(new Color(255, 189, 0)));
                break;
            case 5:
                expected = new DefaultInterpolationPoint(2000d, SF.literal(new Color(236, 255, 34)));
                break;
            case 6:
                expected = new DefaultInterpolationPoint(2500d, SF.literal(new Color(135, 255, 135)));
                break;
            case 7:
                expected = new DefaultInterpolationPoint(3000d, SF.literal(new Color(34, 255, 236)));
                break;
            case 8:
                expected = new DefaultInterpolationPoint(3500d, SF.literal(new Color(0, 189, 255)));
                break;
            case 9:
                expected = new DefaultInterpolationPoint(4000d, SF.literal(new Color(0, 89, 255)));
                break;
            case 10:
                expected = new DefaultInterpolationPoint(4500d, SF.literal(new Color(0, 0, 244)));
                break;
            case 11:
                expected = new DefaultInterpolationPoint(5000d, SF.literal(new Color(0, 0, 143)));
                break;
            default:
                fail("Unexpected number of elements.");
        }
        assertEquals(expected, step);
    }
}
Also used : DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) Interpolate(org.geotoolkit.style.function.Interpolate) ColorMap(org.opengis.style.ColorMap) Color(java.awt.Color) DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) Test(org.junit.Test)

Example 4 with DefaultInterpolationPoint

use of org.geotoolkit.style.function.DefaultInterpolationPoint in project geotoolkit by Geomatys.

the class PrepareStyleVisitorTest method interpolationExpressionShouldNotCauseError.

/**
 * Temporary test to ensure interpolation expression is not broken by Style override visitor.
 * In the future, a rewrite of style/expression visitors might make it obsolete.
 */
@Test
public void interpolationExpressionShouldNotCauseError() {
    final PrepareStyleVisitor visitor = new PrepareStyleVisitor(Feature.class, null);
    final DefaultInterpolate interpol = new DefaultInterpolate(FF.property("random"), Arrays.asList(new DefaultInterpolationPoint(0, FF.literal(0)), new DefaultInterpolationPoint(1, FF.literal(1))), Method.NUMERIC, Mode.LINEAR, FF.literal(-1));
    final Object result = visitor.visit(interpol);
    assertNotNull("Visit result", result);
    assertTrue("Result should be an expression", result instanceof Expression);
    assertFalse(((Expression<?, ?>) result).getParameters().isEmpty());
    final Object stroke = visitor.visit(new DefaultStroke(interpol, null, null, null, null, null, null), null);
    assertNotNull(stroke);
    assertTrue(stroke instanceof Stroke);
    final Expression color = ((Stroke) stroke).getColor();
    assertNotNull("Stroke color", color);
    assertFalse(((Expression<?, ?>) result).getParameters().isEmpty());
}
Also used : DefaultStroke(org.geotoolkit.style.DefaultStroke) DefaultStroke(org.geotoolkit.style.DefaultStroke) Stroke(org.opengis.style.Stroke) Expression(org.opengis.filter.Expression) DefaultInterpolate(org.geotoolkit.style.function.DefaultInterpolate) DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) Test(org.junit.Test)

Aggregations

DefaultInterpolationPoint (org.geotoolkit.style.function.DefaultInterpolationPoint)4 Test (org.junit.Test)3 Expression (org.opengis.filter.Expression)3 Color (java.awt.Color)2 ArrayList (java.util.ArrayList)2 DefaultInterpolate (org.geotoolkit.style.function.DefaultInterpolate)2 Interpolate (org.geotoolkit.style.function.Interpolate)2 InterpolationPoint (org.geotoolkit.style.function.InterpolationPoint)2 Graphics2D (java.awt.Graphics2D)1 AffineTransform (java.awt.geom.AffineTransform)1 ComponentColorModel (java.awt.image.ComponentColorModel)1 RenderedImage (java.awt.image.RenderedImage)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1 Map (java.util.Map)1 ImageLayout (javax.media.jai.ImageLayout)1 NullOpImage (javax.media.jai.NullOpImage)1 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)1 ImageProcessor (org.apache.sis.image.ImageProcessor)1