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());
}
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;
}
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);
}
}
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());
}
Aggregations