/** * Retrieves the legend for the provided request. * * @return a {@link BufferedImage} that represents the legend for the provided request. */ public BufferedImage getLegend() { if (rasterSymbolizer == null) { return null; } return createResponse(); }
private synchronized BufferedImage createResponse() { if (image == null) { if (cMapLegendCreator != null) // creating a legend image = cMapLegendCreator.getLegend(); else { image = ImageUtils.createImage(width, height, (IndexColorModel) null, transparent); final Graphics2D graphics = ImageUtils.prepareTransparency( transparent, bgColor, image, new HashMap<RenderingHints.Key, Object>()); if (defaultLegend == null) { drawRasterIcon(graphics); } else { graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.drawImage(defaultLegend, 0, 0, width, height, null); } graphics.dispose(); } } return image; }
/** * Constructor for a RasterLayerLegendHelper. * * <p>It takes a {@link GetLegendGraphicRequest} object in order to do its magic. * * @param request the {@link GetLegendGraphicRequest} for which we want to build a legend * @param style the {@link Style} for which we want to build a legend * @param rule the {@link Rule} to use for rendering */ public RasterLayerLegendHelper( final GetLegendGraphicRequest request, Style style, String ruleName) { PackagedUtils.ensureNotNull(request, "The provided GetLegendGraphicRequest is null"); parseRequest(request, style, ruleName); }
@Test public void testRuleTextIntervalOneElements() { StyleBuilder sb = new StyleBuilder(); ColorMap cmap = sb.createColorMap( new String[] {null}, new double[] {10}, new Color[] {Color.RED}, ColorMap.TYPE_INTERVALS); Style style = sb.createStyle(sb.createRasterSymbolizer(cmap, 1)); RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(1, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); assertEquals("x < 10.0", firstRow.getRuleManager().text); }
} else if (buildRasterLegend) { final RasterLayerLegendHelper rasterLegendHelper = new RasterLayerLegendHelper(request, gt2Style, ruleName); final BufferedImage image = rasterLegendHelper.getLegend(); if (image != null) { if (titleImage != null) {
@Test public void testRuleTextRampOneElements() { StyleBuilder sb = new StyleBuilder(); ColorMap cmap = sb.createColorMap( new String[] {null}, new double[] {10}, new Color[] {Color.RED}, ColorMap.TYPE_RAMP); Style style = sb.createStyle(sb.createRasterSymbolizer(cmap, 1)); RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(1, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); assertEquals("", firstRow.getRuleManager().text); }
new RasterLayerLegendHelper(request, gt2Style, ruleName); final BufferedImage image = rasterLegendHelper.getLegend(); if (image != null) { if (titleImage != null) {
@Test public void testRuleTextRampTwoElements() { StyleBuilder sb = new StyleBuilder(); ColorMap cmap = sb.createColorMap( new String[] {null, null}, new double[] {10, 100}, new Color[] {Color.RED, Color.BLUE}, ColorMap.TYPE_RAMP); Style style = sb.createStyle(sb.createRasterSymbolizer(cmap, 1)); RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(2, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); assertEquals("10.0 >= x", firstRow.getRuleManager().text); ColorMapEntryLegendBuilder lastRow = rows.get(1); assertEquals("100.0 <= x", lastRow.getRuleManager().text); }
@Test public void testRuleTextIntervalsTwoElements() { StyleBuilder sb = new StyleBuilder(); ColorMap cmap = sb.createColorMap( new String[] {null, null}, new double[] {10, 100}, new Color[] {Color.RED, Color.BLUE}, ColorMap.TYPE_INTERVALS); Style style = sb.createStyle(sb.createRasterSymbolizer(cmap, 1)); RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(2, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); assertEquals("x < 10.0", firstRow.getRuleManager().text); ColorMapEntryLegendBuilder lastRow = rows.get(1); assertEquals("10.0 <= x < 100.0", lastRow.getRuleManager().text); }
@Test public void testInfiniteOnIntervals() { StyleBuilder sb = new StyleBuilder(); ColorMap cmap = sb.createColorMap( new String[] {null, null, null}, new double[] {Double.NEGATIVE_INFINITY, 50, Double.POSITIVE_INFINITY}, new Color[] {Color.RED, Color.WHITE, Color.BLUE}, ColorMap.TYPE_INTERVALS); Style style = sb.createStyle(sb.createRasterSymbolizer(cmap, 1)); RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(2, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); assertEquals("x < 50.0", firstRow.getRuleManager().text); ColorMapEntryLegendBuilder midRow = rows.get(1); assertEquals("50.0 <= x", midRow.getRuleManager().text); }
@Test public void testRuleTextIntervalsThreeElements() { StyleBuilder sb = new StyleBuilder(); ColorMap cmap = sb.createColorMap( new String[] {null, null, null}, new double[] {10, 50, 100}, new Color[] {Color.RED, Color.WHITE, Color.BLUE}, ColorMap.TYPE_INTERVALS); Style style = sb.createStyle(sb.createRasterSymbolizer(cmap, 1)); RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(3, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); assertEquals("x < 10.0", firstRow.getRuleManager().text); ColorMapEntryLegendBuilder midRow = rows.get(1); assertEquals("10.0 <= x < 50.0", midRow.getRuleManager().text); ColorMapEntryLegendBuilder lastRow = rows.get(2); assertEquals("50.0 <= x < 100.0", lastRow.getRuleManager().text); }
@Test public void testRuleTextRampThreeElements() { StyleBuilder sb = new StyleBuilder(); ColorMap cmap = sb.createColorMap( new String[] {null, null, null}, new double[] {10, 50, 100}, new Color[] {Color.RED, Color.WHITE, Color.BLUE}, ColorMap.TYPE_RAMP); Style style = sb.createStyle(sb.createRasterSymbolizer(cmap, 1)); RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(3, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); assertEquals("10.0 >= x", firstRow.getRuleManager().text); ColorMapEntryLegendBuilder midRow = rows.get(1); assertEquals("50.0 = x", midRow.getRuleManager().text); ColorMapEntryLegendBuilder lastRow = rows.get(2); assertEquals("100.0 <= x", lastRow.getRuleManager().text); }
RasterLayerLegendHelper helper = new RasterLayerLegendHelper(request, style, null); List<ColorMapEntryLegendBuilder> rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(2, rows.size()); ColorMapEntryLegendBuilder firstRow = rows.get(0); helper = new RasterLayerLegendHelper(request, style, null); rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(2, rows.size()); firstRow = rows.get(0); helper = new RasterLayerLegendHelper(request, style, null); rows = new ArrayList<>(helper.getcMapLegendCreator().getBodyRows()); assertEquals(2, rows.size()); firstRow = rows.get(0);