--- trunk/src/skrueger/RasterLegendData.java 2009/07/31 14:43:47 256 +++ branches/2.3-KECK/src/skrueger/RasterLegendData.java 2010/10/14 09:44:10 1102 @@ -25,29 +25,45 @@ * * Contributors: * Martin O. J. Schmitz - initial API and implementation - * Stefan A. Krüger - additional utility classes + * Stefan A. Tzeggai - additional utility classes ******************************************************************************/ package skrueger; + import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.TreeMap; import org.apache.log4j.Logger; - +import org.geotools.coverage.grid.GridCoverage2D; +import org.geotools.geometry.Envelope2D; +import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer; + +import schmitzm.geotools.GTUtil; +import schmitzm.geotools.grid.GridUtil; +import skrueger.geotools.Copyable; import skrueger.i8n.Translation; /** - * Holds all the additional information needed to paint a Legend for a RasterLayer. - * So far, only Legends for one-band raster layers are supported. - * - * @author Stefan Alfons Krüger - * + * Holds all the additional information needed to paint a Legend for a + * RasterLayer. So far, only Legends for one-band raster layers are supported. + * + * @author Stefan Alfons Tzeggai + * + * TODO implements {@link Copyable} */ -public class RasterLegendData extends HashMap { - static private final Logger LOGGER = Logger.getLogger(RasterLegendData.class); +public class RasterLegendData extends TreeMap implements + Copyable { + static private final Logger LOGGER = Logger + .getLogger(RasterLegendData.class); + private Boolean paintGaps = false; + /** + * Shall bigger gaps be painted between the raster images + */ public Boolean isPaintGaps() { return paintGaps; } @@ -57,29 +73,71 @@ } /** - * {@link #paintGaps} defines, if gaps should be painted between the legends colors, - * indicating nominal values in the raster (e.g. classifications) + * {@link #paintGaps} defines, if gaps should be painted between the legends + * colors, indicating nominal values in the raster (e.g. classifications) */ public RasterLegendData(boolean paintGaps) { - super(); this.paintGaps = paintGaps; } - public boolean getPaintGaps() { - return paintGaps ; - } - - public List getSortedKeys(){ - Object[] array = keySet().toArray(); + /** + * Returns a new list containing all {@link Double} values that shall apear + * in the legend. + */ + public List getSortedKeys() { + Double[] array = keySet().toArray(new Double[] {}); Arrays.sort(array); final LinkedList linkedList = new LinkedList(); - for (Object o : array){ - linkedList.add( (Double)o); + for (Double o : array) { + linkedList.add(o); } return linkedList; + } + + /** + * Creates a sample {@link GridCoverage2D} (size 1x1, WGS84) for each legend + * value. These rasters can be used to do visualize the legend item in the + * corresponding color via {@link GridCoverageRenderer}. + */ + public Map createSampleRasters() { + Map sampleRaster = new HashMap(); + + for (Double rasterValue : keySet()) { + GridCoverage2D grid = GridUtil.GRID_FAC.create("Legend_" + + rasterValue, + new float[][] { { rasterValue.floatValue() } }, + new Envelope2D(GTUtil.WGS84, 0, 0, 1, 1)); + sampleRaster.put(rasterValue, grid); + } + return sampleRaster; + } + + /** + * Creates a new {@link RasterLegendData} object with identical values + */ + @Override + public RasterLegendData copy() { + RasterLegendData copy = (RasterLegendData) super.clone(); + return copyTo(copy); + } + + /** + * Deep-copies all values of this {@link RasterLegendData} to another + * {@link RasterLegendData} + */ + @Override + public RasterLegendData copyTo(RasterLegendData target) { + target.clear(); + + target.setPaintGaps(isPaintGaps()); + + for (Double o : keySet()) { + target.put(new Double(o), get(o).copy()); + } + return target; } }