--- trunk/src/skrueger/RasterLegendData.java 2009/02/24 22:43:52 2 +++ branches/2.0-RC1/src/skrueger/RasterLegendData.java 2009/12/09 15:13:42 607 @@ -1,56 +1,143 @@ -package skrueger; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; - -import org.apache.log4j.Logger; - -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 - * - */ -public class RasterLegendData extends HashMap { - static private final Logger LOGGER = Logger.getLogger(RasterLegendData.class); - private Boolean paintGaps = false; - - public Boolean isPaintGaps() { - return paintGaps; - } - - public void setPaintGaps(boolean paintPaps) { - this.paintGaps = paintPaps; - } - - /** - * {@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(); - - Arrays.sort(array); - - final LinkedList linkedList = new LinkedList(); - for (Object o : array){ - linkedList.add( (Double)o); - } - - return linkedList; - - } -} +/******************************************************************************* + * Copyright (c) 2009 Martin O. J. Schmitz. + * + * This file is part of the SCHMITZM library - a collection of utility + * classes based on Java 1.6, focusing (not only) on Java Swing + * and the Geotools library. + * + * The SCHMITZM project is hosted at: + * http://wald.intevation.org/projects/schmitzm/ + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 3 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License (license.txt) + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * or try this link: http://www.gnu.org/licenses/lgpl.html + * + * Contributors: + * Martin O. J. Schmitz - initial API and implementation + * Stefan A. Krüger - 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 + * + * TODO implements {@link Copyable} + */ +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; + } + + public void setPaintGaps(boolean paintPaps) { + this.paintGaps = paintPaps; + } + + /** + * {@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) { + this.paintGaps = paintGaps; + } + + /** + * 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 (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; + } +}