/[schmitzm]/trunk/src/skrueger/geotools/StyledLayerUtil.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/StyledLayerUtil.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/geotools/StyledLayerUtil.java revision 224 by alfonx, Tue Jul 14 15:57:19 2009 UTC branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java revision 419 by alfonx, Wed Sep 30 15:36:39 2009 UTC
# Line 1  Line 1 
1  package skrueger.geotools;  /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3  import java.io.File;   *
4  import java.io.FileNotFoundException;   * This file is part of the SCHMITZM library - a collection of utility
5  import java.io.FileWriter;   * classes based on Java 1.6, focusing (not only) on Java Swing
6  import java.net.URL;   * and the Geotools library.
7  import java.text.DecimalFormat;   *
8  import java.util.HashMap;   * The SCHMITZM project is hosted at:
9  import java.util.List;   * http://wald.intevation.org/projects/schmitzm/
10  import java.util.Map;   *
11  import java.util.SortedMap;   * This program is free software; you can redistribute it and/or
12  import java.util.TreeMap;   * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14  import org.apache.log4j.Logger;   * of the License, or (at your option) any later version.
15  import org.geotools.coverage.grid.GridCoverage2D;   *
16  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;   * This program is distributed in the hope that it will be useful,
17  import org.geotools.feature.FeatureCollection;   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  import org.geotools.map.DefaultMapLayer;   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  import org.geotools.map.MapLayer;   * GNU General Public License for more details.
20  import org.geotools.styling.ColorMap;   *
21  import org.geotools.styling.ColorMapEntry;   * You should have received a copy of the GNU Lesser General Public License (license.txt)
22  import org.geotools.styling.RasterSymbolizer;   * along with this program; if not, write to the Free Software
23  import org.geotools.styling.Style;   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  import org.jdom.Document;   * or try this link: http://www.gnu.org/licenses/lgpl.html
25  import org.jdom.Element;   *
26  import org.jdom.input.SAXBuilder;   * Contributors:
27  import org.jdom.output.XMLOutputter;   *     Martin O. J. Schmitz - initial API and implementation
28     *     Stefan A. Krüger - additional utility classes
29  import schmitzm.geotools.styling.StylingUtil;   ******************************************************************************/
30  import schmitzm.io.IOUtil;  package skrueger.geotools;
31  import schmitzm.lang.LangUtil;  
32  import schmitzm.swing.SwingUtil;  import java.awt.Color;
33  import skrueger.AttributeMetaData;  import java.awt.Dimension;
34  import skrueger.RasterLegendData;  import java.awt.Graphics;
35  import skrueger.i8n.Translation;  import java.awt.Graphics2D;
36    import java.awt.Rectangle;
37  /**  import java.awt.geom.AffineTransform;
38   * This class provides static helper methods for dealing with  import java.awt.image.BufferedImage;
39   * {@link StyledLayerInterface} stuff.  import java.awt.image.ColorModel;
40   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)  import java.awt.image.ComponentColorModel;
41   * @version 1.0  import java.awt.image.DataBuffer;
42   */  import java.io.File;
43  public class StyledLayerUtil {  import java.io.FileNotFoundException;
44    private static final Logger LOGGER = Logger.getLogger(StyledLayerUtil.class.getName());  import java.io.FileWriter;
45    private static final SAXBuilder SAX_BUILDER = new SAXBuilder();  import java.net.URL;
46    private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter();  import java.text.DecimalFormat;
47    import java.util.HashMap;
48    /** URL for Atlas XML schema */  import java.util.List;
49    public static final String AMLURI = "http://www.wikisquare.de/AtlasML";  import java.util.Map;
50    /** Name of the XML Element for the attribute meta data map */  import java.util.SortedMap;
51    public static final String ELEM_NAME_AMD = "attributeMetaData";  import java.util.TreeMap;
52    /** Name of the XML Element for the raster legend data */  
53    public static final String ELEM_NAME_RLD = "rasterLegendData";  import javax.swing.BorderFactory;
54    /** Name of the XML Element for an attribute meta data map entry */  import javax.swing.Box;
55    public static final String ELEM_NAME_ATTRIBUTE = "dataAttribute";  import javax.swing.BoxLayout;
56    /** Name of the XML Element for an raster legend data entry */  import javax.swing.ImageIcon;
57    public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem";  import javax.swing.JLabel;
58    /** Name of the XML Element for a translation */  
59    public static final String ELEM_NAME_TRANSLATION = "translation";  import org.apache.log4j.Logger;
60    import org.geotools.coverage.grid.GeneralGridEnvelope;
61    /**  import org.geotools.coverage.grid.GridCoverage2D;
62     * Creates a Geotools {@link MapLayer} from an object. If the object is a  import org.geotools.coverage.grid.GridGeometry2D;
63     * {@link StyledLayerInterface} then its sytle is used. In case of direct  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
64     * Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader},  import org.geotools.coverage.grid.io.AbstractGridFormat;
65     * {@link FeatureCollection}) a default style is generated.  import org.geotools.feature.FeatureCollection;
66     * @param object an Object  import org.geotools.gce.imagepyramid.ImagePyramidReader;
67     * @exception Exception if {@code null} is given as object or an error occurs during layer creation  import org.geotools.geometry.jts.ReferencedEnvelope;
68     */  import org.geotools.map.DefaultMapLayer;
69    public static MapLayer createMapLayer(Object object) throws Exception {  import org.geotools.map.MapLayer;
70      return createMapLayer(object,null);  import org.geotools.parameter.Parameter;
71    }  import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer;
72    import org.geotools.styling.ColorMap;
73    /**  import org.geotools.styling.ColorMapEntry;
74     * Creates a Geotools {@link MapLayer} from an object. If the object is a  import org.geotools.styling.FeatureTypeStyle;
75     * {@link StyledLayerInterface} then its sytle is used. In case of direct  import org.geotools.styling.RasterSymbolizer;
76     * Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader},  import org.geotools.styling.Rule;
77     * {@link FeatureCollection}) a default style is generated.  import org.geotools.styling.Style;
78     * @param object an Object  import org.jdom.Document;
79     * @param forcedStyle (SLD-)Style to force for the object  import org.jdom.Element;
80     * @exception Exception if {@code null} is given as object or an error occurs during layer creation  import org.jdom.input.SAXBuilder;
81     */  import org.jdom.output.XMLOutputter;
82    public static MapLayer createMapLayer(Object object, Style forcedStyle) throws Exception {  import org.opengis.feature.simple.SimpleFeatureType;
83      MapLayer layer     = null;  import org.opengis.parameter.GeneralParameterValue;
84      Style    style     = null;  
85      if ( object instanceof StyledLayerInterface ) {  import schmitzm.geotools.JTSUtil;
86        style =  ((StyledLayerInterface<?>)object).getStyle();  import schmitzm.geotools.styling.StylingUtil;
87        object = ((StyledLayerInterface<?>)object).getGeoObject();  import schmitzm.io.IOUtil;
88      }  import schmitzm.lang.LangUtil;
89      if ( forcedStyle != null )  import schmitzm.swing.SwingUtil;
90        style = forcedStyle;  import skrueger.AttributeMetaData;
91      if ( style == null )  import skrueger.RasterLegendData;
92        style = StylingUtil.createDefaultStyle(object);  import skrueger.i8n.Translation;
93    
94      if (object instanceof GridCoverage2D)  /**
95        layer = new DefaultMapLayer( (GridCoverage2D) object, style);   * This class provides static helper methods for dealing with
96      if (object instanceof AbstractGridCoverage2DReader)   * {@link StyledLayerInterface} stuff.
97        layer = new DefaultMapLayer( (AbstractGridCoverage2DReader) object, style);   *
98      if (object instanceof FeatureCollection)   * @author <a href="mailto:[email protected]">Martin Schmitz</a>
99        layer = new DefaultMapLayer( (FeatureCollection) object, style);   *         (University of Bonn/Germany)
100     * @version 1.0
101      if ( layer == null )   */
102        throw new Exception("Can not create MapLayer from "+(object == null ? "null" : object.getClass()));  public class StyledLayerUtil {
103            private static final Logger LOGGER = Logger.getLogger(StyledLayerUtil.class
104      return layer;                          .getName());
105    }          private static final SAXBuilder SAX_BUILDER = new SAXBuilder();
106            private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter();
107    /**  
108     * Creates an default instance of {@link StyledLayerInterface} for a Geotools          /** URL for Atlas XML schema */
109     * object ({@link GridCoverage2D}, {@link FeatureCollection}) with a default          public static final String AMLURI = "http://www.wikisquare.de/AtlasML";
110     * style.          /** Name of the XML Element for the attribute meta data map */
111     * @param object an Object          public static final String ELEM_NAME_AMD = "attributeMetaData";
112     * @param title  title for the object          /** Name of the XML Element for the raster legend data */
113     * @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation          public static final String ELEM_NAME_RLD = "rasterLegendData";
114     */          /** Name of the XML Element for an attribute meta data map entry */
115    public static StyledLayerInterface<?> createStyledLayer(Object object, String title) {          public static final String ELEM_NAME_ATTRIBUTE = "dataAttribute";
116       return createStyledLayer(object, title, null);          /** Name of the XML Element for an raster legend data entry */
117    }          public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem";
118            /** Name of the XML Element for a translation */
119    /**          public static final String ELEM_NAME_TRANSLATION = "translation";
120     * Creates an default instance of {@link StyledLayerInterface} for a Geotools  
121     * object ({@link GridCoverage2D}, {@link FeatureCollection}) with a given          /**
122     * style.           * Creates a Geotools {@link MapLayer} from an object. If the object is a
123     * @param object an Object           * {@link StyledLayerInterface} then its sytle is used. In case of direct
124     * @param title  title for the object           * Geotools objects ({@link GridCoverage2D},
125     * @param style  style and meta data for the object           * {@link AbstractGridCoverage2DReader}, {@link FeatureCollection}) a
126     * @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation           * default style is generated.
127     */           *
128    public static StyledLayerInterface<?> createStyledLayer(Object object, String title, StyledLayerStyle style) {           * @param object
129      StyledLayerInterface<?> styledLayer = null;           *            an Object
130             * @exception Exception
131      String id = (title != null) ? title : "defaultID";           *                if {@code null} is given as object or an error occurs
132             *                during layer creation
133      if ( object instanceof GridCoverage2D )           */
134        styledLayer = new StyledGridCoverage(          public static MapLayer createMapLayer(Object object) throws Exception {
135            (GridCoverage2D)object,                  return createMapLayer(object, null);
136            id,          }
137            title,  
138            style          /**
139        );           * Creates a Geotools {@link MapLayer} from an object. If the object is a
140      else if ( object instanceof AbstractGridCoverage2DReader )           * {@link StyledLayerInterface} then its sytle is used. In case of direct
141             styledLayer = new StyledGridCoverageReader(           * Geotools objects ({@link GridCoverage2D},
142                 (AbstractGridCoverage2DReader)object,           * {@link AbstractGridCoverage2DReader}, {@link FeatureCollection}) a
143                 id,           * default style is generated.
144                 title,           *
145                 style           * @param object
146             );           *            an Object
147      else if ( object instanceof FeatureCollection )           * @param forcedStyle
148        styledLayer = new StyledFeatureCollection(           *            (SLD-)Style to force for the object
149            (FeatureCollection)object,           * @exception Exception
150            id,           *                if {@code null} is given as object or an error occurs
151            title,           *                during layer creation
152            style           */
153        );          public static MapLayer createMapLayer(Object object, Style forcedStyle)
154                                throws Exception {
155      if ( styledLayer == null )                  MapLayer layer = null;
156        throw new UnsupportedOperationException("Can not create StyledLayerInterface object from "+(object == null ? "null" : object.getClass()));                  Style style = null;
157                    if (object instanceof StyledLayerInterface) {
158      return styledLayer;                          style = ((StyledLayerInterface<?>) object).getStyle();
159    }                          object = ((StyledLayerInterface<?>) object).getGeoObject();
160                    }
161    /**                  if (forcedStyle != null)
162     * Return only the visible or invisible entries of an AttributeMetaData-Map.                          style = forcedStyle;
163     * @param amdMap AttributeMetaData-Map                  if (style == null)
164     * @param visible indicated whether the visible or invisible entries are                          style = StylingUtil.createDefaultStyle(object);
165     *                returned  
166     */                  if (object instanceof GridCoverage2D)
167    public static SortedMap<Integer,AttributeMetaData> getVisibleAttributeMetaData(Map<Integer,AttributeMetaData> amdMap, boolean visible) {                          layer = new DefaultMapLayer((GridCoverage2D) object, style);
168      SortedMap<Integer,AttributeMetaData> filteredMap = new TreeMap<Integer,AttributeMetaData>();                  if (object instanceof AbstractGridCoverage2DReader)
169      for (AttributeMetaData amd : amdMap.values())                          layer = new DefaultMapLayer((AbstractGridCoverage2DReader) object,
170        if ( amd.isVisible() )                                          style);
171          filteredMap.put(amd.getColIdx(), amd);                  if (object instanceof FeatureCollection)
172                                layer = new DefaultMapLayer((FeatureCollection) object, style);
173      return filteredMap;  
174    }                  if (layer == null)
175                              throw new Exception("Can not create MapLayer from "
176                                              + (object == null ? "null" : object.getClass()));
177    /**  
178     * Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}.                  return layer;
179     * This method works like {@link AMLImport#parseDataAttribute(org.w3c.dom.Node},          }
180     * but for JDOM.  
181     * @param element {@link Element} to parse          /**
182     */           * Creates an default instance of {@link StyledLayerInterface} for a
183    public static AttributeMetaData parseAttributeMetaData(final Element element) {           * Geotools object ({@link GridCoverage2D}, {@link FeatureCollection}) with
184      final Integer col = Integer.valueOf(element.getAttributeValue("col"));           * a default style.
185      final Boolean visible = Boolean.valueOf(element.getAttributeValue("visible"));           *
186      final String unit = element.getAttributeValue("unit");           * @param object
187             *            an Object
188      Translation name = new Translation();           * @param title
189      Translation desc = new Translation();           *            title for the object
190      for (final Element childElement : (List<Element>)element.getChildren()) {           * @exception UnsupportedOperationException
191        if (childElement.getName() == null)           *                if {@code null} is given as object or an error occurs
192          continue;           *                during creation
193             */
194        if (childElement.getName().equals("name"))          public static StyledLayerInterface<?> createStyledLayer(Object object,
195          name = parseTranslation(childElement);                          String title) {
196        else if (childElement.getName().equals("desc"))                  return createStyledLayer(object, title, null);
197          desc = parseTranslation(childElement);          }
198      }  
199      return new AttributeMetaData(col, visible, name, desc, unit);          /**
200    }           * Creates an default instance of {@link StyledLayerInterface} for a
201             * Geotools object ({@link GridCoverage2D}, {@link FeatureCollection}) with
202    /**           * a given style.
203     * Parses a {@link AttributeMetaData} map from an JDOM-{@link Element}           *
204     * with {@code <attribute>}-childs.           * @param object
205     * @param element {@link Element} to parse           *            an Object
206     */           * @param title
207    public static Map<Integer,AttributeMetaData> parseAttributeMetaDataMap(final Element element) {           *            title for the object
208      HashMap<Integer,AttributeMetaData> metaData = new HashMap<Integer,AttributeMetaData>();           * @param style
209      List<Element> attributesElements = element.getChildren( ELEM_NAME_ATTRIBUTE );           *            style and meta data for the object
210      for (Element attibuteElement : attributesElements)           * @exception UnsupportedOperationException
211      {           *                if {@code null} is given as object or an error occurs
212        AttributeMetaData attrMetaData = parseAttributeMetaData( attibuteElement );           *                during creation
213        metaData.put( attrMetaData.getColIdx(), attrMetaData );           */
214      }          public static StyledLayerInterface<?> createStyledLayer(Object object,
215      return metaData;                          String title, StyledLayerStyle style) {
216    }                  StyledLayerInterface<?> styledLayer = null;
217    
218    /**                  String id = (title != null) ? title : "defaultID";
219     * Loads a {@link AttributeMetaData} object from an URL.  
220     * @param documentUrl {@link URL} to parse                  if (object instanceof GridCoverage2D)
221     * @see #parseAttributeMetaData(Element)                          styledLayer = new StyledGridCoverage((GridCoverage2D) object, id,
222     */                                          title, style);
223    public static Map<Integer,AttributeMetaData> loadAttributeMetaDataMap(final URL documentUrl) throws Exception {                  else if (object instanceof AbstractGridCoverage2DReader)
224      Document document = SAX_BUILDER.build(documentUrl);                          styledLayer = new StyledGridCoverageReader(
225      return parseAttributeMetaDataMap( document.getRootElement() );                                          (AbstractGridCoverage2DReader) object, id, title, style);
226    }                  else if (object instanceof FeatureCollection)
227                            styledLayer = new StyledFeatureCollection(
228    /**                                          (FeatureCollection) object, id, title, style);
229     * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}  
230     * object.                  if (styledLayer == null)
231     * @param amd meta data for one attribute                          throw new UnsupportedOperationException(
232     */                                          "Can not create StyledLayerInterface object from "
233    public static Element createAttributeMetaDataElement(final AttributeMetaData amd) {                                                          + (object == null ? "null" : object.getClass()));
234      final Element element = new Element( ELEM_NAME_ATTRIBUTE , AMLURI);  
235      element.setAttribute("col", String.valueOf( amd.getColIdx() ) );                  return styledLayer;
236      element.setAttribute("visible", String.valueOf( amd.isVisible() ) );          }
237      element.setAttribute("unit", amd.getUnit() );  
238      // Creating a aml:name tag...          /**
239      element.addContent( createTranslationElement("name", amd.getTitle()) );           * Return only the visible or invisible entries of an AttributeMetaData-Map.
240      // Creating a aml:desc tag...           *
241      element.addContent( createTranslationElement("desc", amd.getDesc()) );           * @param amdMap
242      return element;           *            AttributeMetaData-Map
243    }           * @param visible
244             *            indicated whether the visible or invisible entries are
245    /**           *            returned
246     * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}           */
247     * map.          public static SortedMap<Integer, AttributeMetaData> getVisibleAttributeMetaData(
248     * @param amdMap map of attribute meta data                          Map<Integer, AttributeMetaData> amdMap, boolean visible) {
249     */                  SortedMap<Integer, AttributeMetaData> filteredMap = new TreeMap<Integer, AttributeMetaData>();
250    public static Element createAttributeMetaDataMapElement(final Map<Integer,AttributeMetaData> amdMap) {                  for (AttributeMetaData amd : amdMap.values())
251      final Element element = new Element( ELEM_NAME_AMD , AMLURI);                          if (amd.isVisible())
252      for (AttributeMetaData amd : amdMap.values())                                  filteredMap.put(amd.getColIdx(), amd);
253        element.addContent( createAttributeMetaDataElement( amd ) );  
254      return element;                  return filteredMap;
255    }          }
256    
257    /**          /**
258     * Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL.           * Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}.
259     * @param amdMap map of {@link AttributeMetaData}           * This method works like {@link
260     * @param documentUrl {@link URL} to store the XML           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.
261     */           *
262    public static void saveAttributeMetaDataMap(final Map<Integer,AttributeMetaData> amdMap, final URL documentUrl) throws Exception {           * @param element
263      // Create XML-Document           *            {@link Element} to parse
264      final FileWriter out = new FileWriter( new File(documentUrl.toURI()) );           */
265      XML_OUTPUTTER.output(          public static AttributeMetaData parseAttributeMetaData(final Element element) {
266        createAttributeMetaDataMapElement(amdMap),                  final Integer col = Integer.valueOf(element.getAttributeValue("col"));
267        out                  final Boolean visible = Boolean.valueOf(element
268      );                                  .getAttributeValue("visible"));
269      out.flush();                  final String unit = element.getAttributeValue("unit");
270      out.close();  
271    }                  Translation name = new Translation();
272                    Translation desc = new Translation();
273                    for (final Element childElement : (List<Element>) element.getChildren()) {
274                            if (childElement.getName() == null)
275    /**                                  continue;
276     * Parses a {@link RasterLegendData} object from an JDOM-{@link Element}.  
277     * This method works like {@link AMLImport#parseRasterLegendData(org.w3c.dom.Node},                          if (childElement.getName().equals("name"))
278     * but for JDOM.                                  name = parseTranslation(childElement);
279     * @param element {@link Element} to parse                          else if (childElement.getName().equals("desc"))
280     */                                  desc = parseTranslation(childElement);
281    public static RasterLegendData parseRasterLegendData(Element element) {                  }
282                    return new AttributeMetaData(col, visible, name, desc, unit);
283      final boolean paintGaps = Boolean.valueOf( element.getAttributeValue("paintGaps") );          }
284    
285      RasterLegendData rld = new RasterLegendData(paintGaps);          /**
286             * Parses a {@link AttributeMetaData} map from an JDOM-{@link Element} with
287      for ( Element childElement : (List<Element>)element.getChildren() ) {           * {@code <attribute>}-childs.
288        final String name = childElement.getName();           *
289        // Cancel if it's an attribute           * @param element
290        if ( childElement.getChildren().size() == 0 )           *            {@link Element} to parse
291          continue;           */
292            public static Map<Integer, AttributeMetaData> parseAttributeMetaDataMap(
293        if (name.equals( ELEM_NAME_RASTERLEGEND )) {                          final Element element) {
294          final String valueAttr = childElement.getAttributeValue("value");                  HashMap<Integer, AttributeMetaData> metaData = new HashMap<Integer, AttributeMetaData>();
295          if ( valueAttr == null )                  List<Element> attributesElements = element
296            throw new UnsupportedOperationException("Attribute 'value' missing for definition of <"+ELEM_NAME_RASTERLEGEND+">");                                  .getChildren(ELEM_NAME_ATTRIBUTE);
297          final double value = Double.valueOf(valueAttr);                  for (Element attibuteElement : attributesElements) {
298                            AttributeMetaData attrMetaData = parseAttributeMetaData(attibuteElement);
299          // first and only item should be the label                          metaData.put(attrMetaData.getColIdx(), attrMetaData);
300          final Element labelElement = childElement.getChild("label");                  }
301          // id label element is missing, the translation is searched directly                  return metaData;
302          // as childs of the rasterLegendItem element          }
303          Translation label = parseTranslation( labelElement != null ? labelElement : childElement );  
304          rld.put(value, label);          /**
305        }           * Loads a {@link AttributeMetaData} object from an URL.
306      }           *
307             * @param documentUrl
308      return rld;           *            {@link URL} to parse
309    }           * @see #parseAttributeMetaData(Element)
310             */
311    /**          public static Map<Integer, AttributeMetaData> loadAttributeMetaDataMap(
312     * Loads a {@link RasterLegendData} object from an URL.                          final URL documentUrl) throws Exception {
313     * @param documentUrl {@link URL} to parse                  Document document = SAX_BUILDER.build(documentUrl);
314     * @see #parseAttributeMetaData(Element)                  return parseAttributeMetaDataMap(document.getRootElement());
315     */          }
316    public static RasterLegendData loadRasterLegendData(final URL documentUrl) throws Exception {  
317      Document document = SAX_BUILDER.build(documentUrl);          /**
318      return parseRasterLegendData( document.getRootElement() );           * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}
319    }           * object.
320             *
321   /**           * @param amd
322     * Creates an JDOM {@link Element} for the given {@link RasterLegendData}           *            meta data for one attribute
323     * map.           */
324     * @param rld raster legend data          public static Element createAttributeMetaDataElement(
325     */                          final AttributeMetaData amd) {
326    public static Element createRasterLegendDataElement(final RasterLegendData rld) {                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);
327      final Element element = new Element( ELEM_NAME_RLD , AMLURI);                  element.setAttribute("col", String.valueOf(amd.getColIdx()));
328      element.setAttribute("paintGaps", rld.isPaintGaps().toString());                  element.setAttribute("visible", String.valueOf(amd.isVisible()));
329      for (Double key : rld.getSortedKeys()) {                  element.setAttribute("unit", amd.getUnit());
330        Element item = new Element( ELEM_NAME_RASTERLEGEND, AMLURI);                  // Creating a aml:name tag...
331        item.setAttribute("value", key.toString());                  element.addContent(createTranslationElement("name", amd.getTitle()));
332        item.addContent( createTranslationElement("label", rld.get(key)) );                  // Creating a aml:desc tag...
333        element.addContent(item);                  element.addContent(createTranslationElement("desc", amd.getDesc()));
334      }                  return element;
335      return element;          }
336    }  
337            /**
338    /**           * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}
339     * Creates {@link RasterLegendData} from a {@link ColorMap}.           * map.
340     * @param colorMap  a color map           *
341     * @param paintGaps indicated whether gaps are painted between the legend items           * @param amdMap
342     * @param digits    number of digits the grid value classes (and legend) are           *            map of attribute meta data
343     *                  rounded to (null means no round; >= 0 means digits after comma;           */
344     *                  < 0 means digits before comma)    */          public static Element createAttributeMetaDataMapElement(
345    public static RasterLegendData generateRasterLegendData(ColorMap colorMap, boolean paintGaps, Integer digits) {                          final Map<Integer, AttributeMetaData> amdMap) {
346      DecimalFormat    decFormat = digits != null ? new DecimalFormat( SwingUtil.getNumberFormatPattern(digits) ) : null;                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
347      RasterLegendData rld       = new RasterLegendData(paintGaps);                  for (AttributeMetaData amd : amdMap.values())
348      for (ColorMapEntry cme : colorMap.getColorMapEntries())                          element.addContent(createAttributeMetaDataElement(amd));
349      {                  return element;
350        double value = StylingUtil.getQuantityFromColorMapEntry(cme);          }
351        String label = cme.getLabel();  
352        // if no label is set (e.g. quantitative style),          /**
353        // use the value as label           * Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL.
354        if ( label == null || label.equals("") )           *
355          if ( digits == null )           * @param amdMap
356            label = String.valueOf(value);           *            map of {@link AttributeMetaData}
357          else           * @param documentUrl
358            label = decFormat.format( LangUtil.round(value, digits) );           *            {@link URL} to store the XML
359        rld.put( value, new Translation("   "+label) );           */
360      }          public static void saveAttributeMetaDataMap(
361      return rld;                          final Map<Integer, AttributeMetaData> amdMap, final URL documentUrl)
362    }                          throws Exception {
363                    // Create XML-Document
364    /**                  final FileWriter out = new FileWriter(new File(documentUrl.toURI()));
365     * Creates {@link RasterLegendData} from the {@link ColorMap} of a style.                  XML_OUTPUTTER.output(createAttributeMetaDataMapElement(amdMap), out);
366     * @param style     a raster style (must contain a  {@link RasterSymbolizer})                  out.flush();
367     * @param paintGaps indicated whether gaps are painted between the legend items                  out.close();
368     * @param digits    number of digits the grid value classes (and legend) are          }
369     *                  rounded to (null means no round; >= 0 means digits after comma;  
370     *                  < 0 means digits before comma)    */          /**
371    public static RasterLegendData generateRasterLegendData(Style style, boolean paintGaps, Integer digits) {           * Parses a {@link RasterLegendData} object from an JDOM-{@link Element}.
372      ColorMap colorMap = StylingUtil.getColorMapFromStyle(style);           * This method works like {@link
373      if ( colorMap == null)           * AMLImport#parseRasterLegendData(org.w3c.dom.Node}, but for JDOM.
374        throw new IllegalArgumentException("Color map can not be determined from style!");           *
375      return generateRasterLegendData(colorMap, paintGaps, digits);           * @param element
376    }           *            {@link Element} to parse
377             */
378    /**          public static RasterLegendData parseRasterLegendData(Element element) {
379     * Saves a {@link RasterLegendData} to an URL.  
380     * @param rld raster legend data                  final boolean paintGaps = Boolean.valueOf(element
381     * @param documentUrl {@link URL} to store the XML                                  .getAttributeValue("paintGaps"));
382     */  
383    public static void saveRasterLegendData(final RasterLegendData rld, final URL documentUrl) throws Exception {                  RasterLegendData rld = new RasterLegendData(paintGaps);
384      // Create XML-Document  
385      final FileWriter out = new FileWriter( new File(documentUrl.toURI()) );                  for (Element childElement : (List<Element>) element.getChildren()) {
386      XML_OUTPUTTER.output(                          final String name = childElement.getName();
387        createRasterLegendDataElement(rld),                          // Cancel if it's an attribute
388        out                          if (childElement.getChildren().size() == 0)
389      );                                  continue;
390      out.flush();  
391      out.close();                          if (name.equals(ELEM_NAME_RASTERLEGEND)) {
392    }                                  final String valueAttr = childElement
393                                                    .getAttributeValue("value");
394    /**                                  if (valueAttr == null)
395     * Parses a {@link Translation} object from an JDOM-{@link Element}.                                          throw new UnsupportedOperationException(
396     * This method works like {@link AMLImport#parseTranslation(org.w3c.dom.Node},                                                          "Attribute 'value' missing for definition of <"
397     * but for JDOM.                                                                          + ELEM_NAME_RASTERLEGEND + ">");
398     * @param element {@link Element} to parse                                  final double value = Double.valueOf(valueAttr);
399     */  
400    public final static Translation parseTranslation(final Element element) {                                  // first and only item should be the label
401      Translation trans = new Translation();                                  final Element labelElement = childElement.getChild("label");
402                                    // id label element is missing, the translation is searched
403      if (element == null)                                  // directly
404       return trans;                                  // as childs of the rasterLegendItem element
405                                    Translation label = parseTranslation(labelElement != null ? labelElement
406      for (final Element translationElement : (List<Element>)element.getChildren()) {                                                  : childElement);
407        final String name = translationElement.getName();                                  rld.put(value, label);
408        if (name == null)                          }
409          continue;                  }
410    
411        // lang attribute                  return rld;
412        String lang = translationElement.getAttributeValue("lang");          }
413        // set the default, if no language code is set  
414        if ( lang == null )          /**
415          lang = Translation.DEFAULT_KEY;           * Loads a {@link RasterLegendData} object from an URL.
416             *
417        final String translationText = translationElement.getValue();           * @param documentUrl
418        if (translationText == null)           *            {@link URL} to parse
419          trans.put(lang, "");           * @see #parseAttributeMetaData(Element)
420        else           */
421          trans.put(lang, translationText);          public static RasterLegendData loadRasterLegendData(final URL documentUrl)
422      }                          throws Exception {
423                    Document document = SAX_BUILDER.build(documentUrl);
424      // if no <translation> is given, the value of the node should                  return parseRasterLegendData(document.getRootElement());
425      // be used as a default translation          }
426      if (trans.size() == 0)  
427        trans.put( Translation.DEFAULT_KEY, element.getValue() );          /**
428      //     trans = new Translation( ((List<Element>)element.getChildren()).get(0).getValue() );           * Creates an JDOM {@link Element} for the given {@link RasterLegendData}
429             * map.
430      return trans;           *
431    }           * @param rld
432             *            raster legend data
433    /**           */
434     * Creates an JDOM {@link Element} for the given {@link Translation}.          public static Element createRasterLegendDataElement(
435     * @param tagname Name of the Element                          final RasterLegendData rld) {
436     * @param translation Translation to store in the Element                  final Element element = new Element(ELEM_NAME_RLD, AMLURI);
437     */                  element.setAttribute("paintGaps", rld.isPaintGaps().toString());
438    public final static Element createTranslationElement(String tagname, Translation translation) {                  for (Double key : rld.getSortedKeys()) {
439      Element element = new Element(tagname, AMLURI);                          Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI);
440      if ( translation == null )                          item.setAttribute("value", key.toString());
441        throw new UnsupportedOperationException("Translation element can not be created from null!");                          item.addContent(createTranslationElement("label", rld.get(key)));
442                            element.addContent(item);
443      // If only a default translation is set, the <translation lang="..">..</tranlation>                  }
444      // part is not used                  return element;
445      if (translation.keySet().size() == 1 && translation.get(Translation.DEFAULT_KEY) != null) {          }
446        element.addContent( translation.get(Translation.DEFAULT_KEY) );  
447        return element;          /**
448      }           * Creates {@link RasterLegendData} from a {@link ColorMap}.
449             *
450      // add a <translation lang="..">..</tranlation> part to the element for           * @param colorMap
451      // all languages           *            a color map
452      for (String lang : translation.keySet()) {           * @param paintGaps
453        Element translationElement = new Element( ELEM_NAME_TRANSLATION , AMLURI);           *            indicated whether gaps are painted between the legend items
454        translationElement.setAttribute("lang", lang);           * @param digits
455        String translationString = translation.get(lang);           *            number of digits the grid value classes (and legend) are
456        if (translationString == null)           *            rounded to (null means no round; >= 0 means digits after
457         translationString = "";           *            comma; < 0 means digits before comma)
458        translationElement.addContent( translationString );           */
459        element.addContent(translationElement);          public static RasterLegendData generateRasterLegendData(ColorMap colorMap,
460      }                          boolean paintGaps, Integer digits) {
461                    DecimalFormat decFormat = digits != null ? new DecimalFormat(SwingUtil
462      return element;                                  .getNumberFormatPattern(digits)) : null;
463    }                  RasterLegendData rld = new RasterLegendData(paintGaps);
464                    for (ColorMapEntry cme : colorMap.getColorMapEntries()) {
465                            double value = StylingUtil.getQuantityFromColorMapEntry(cme);
466    /**                          String label = cme.getLabel();
467     * Sets a style to {@link StyledLayerInterface}.                          // if no label is set (e.g. quantitative style),
468     * @param styledObject a styled object                          // use the value as label
469     * @param style a Style                          if (label == null || label.equals(""))
470     */                                  if (digits == null)
471    public static void setStyledLayerStyle(StyledLayerInterface styledObject, StyledLayerStyle<?> style) {                                          label = String.valueOf(value);
472      // set SLD style                                  else
473      styledObject.setStyle( style.getGeoObjectStyle() );                                          label = decFormat.format(LangUtil.round(value, digits));
474      // set meta data                          rld.put(value, new Translation("   " + label));
475      if ( styledObject        instanceof StyledGridCoverageInterface &&                  }
476           (style.getMetaData() instanceof RasterLegendData || style.getMetaData() == null) ) {                  return rld;
477        RasterLegendData sourceRld = (RasterLegendData)style.getMetaData();          }
478        RasterLegendData destRld = ((StyledGridCoverageInterface)styledObject).getLegendMetaData();  
479        if ( destRld != null && sourceRld != null ) {          /**
480          destRld.setPaintGaps(sourceRld.isPaintGaps());           * Creates {@link RasterLegendData} from the {@link ColorMap} of a style.
481          destRld.clear();           *
482          destRld.putAll( sourceRld );           * @param style
483        }           *            a raster style (must contain a {@link RasterSymbolizer})
484        return;           * @param paintGaps
485      }           *            indicated whether gaps are painted between the legend items
486      if ( styledObject        instanceof StyledFeatureCollectionInterface &&           * @param digits
487           (style.getMetaData() instanceof Map || style.getMetaData() == null) ) {           *            number of digits the grid value classes (and legend) are
488        Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>)style.getMetaData();           *            rounded to (null means no round; >= 0 means digits after
489        Map<Integer, AttributeMetaData> destAmd   = ((StyledFeatureCollectionInterface)styledObject).getAttributeMetaDataMap();           *            comma; < 0 means digits before comma)
490        if ( destAmd != null && sourceAmd != null ) {           */
491          destAmd.clear();          public static RasterLegendData generateRasterLegendData(Style style,
492          destAmd.putAll( sourceAmd );                          boolean paintGaps, Integer digits) {
493        }                  ColorMap colorMap = StylingUtil.getColorMapFromStyle(style);
494        return;                  if (colorMap == null)
495      }                          throw new IllegalArgumentException(
496                                            "Color map can not be determined from style!");
497      throw new UnsupportedOperationException("Style is not compatible to object: " +                  return generateRasterLegendData(colorMap, paintGaps, digits);
498                                              (style.getMetaData() == null ? null : style.getMetaData().getClass().getSimpleName()) +          }
499                                              " <-> " +  
500                                              (styledObject == null ? null : styledObject.getClass().getSimpleName()));          /**
501    }           * Saves a {@link RasterLegendData} to an URL.
502             *
503    /**           * @param rld
504     * Returns the style a {@link StyledLayerInterface} as a {@link StyledLayerStyle}.           *            raster legend data
505     * @param styledObject a styled object           * @param documentUrl
506     * @return {@code StyledLayerStyle<RasterLegendData>} for {@link StyledGridCoverageInterface}           *            {@link URL} to store the XML
507     *         or {@code StyledLayerStyle<Map<Integer,AttributeMetaData>>} for           */
508     *         {@link StyledFeatureCollectionInterface}          public static void saveRasterLegendData(final RasterLegendData rld,
509     */                          final URL documentUrl) throws Exception {
510    public static StyledLayerStyle<?> getStyledLayerStyle(StyledLayerInterface styledObject) {                  // Create XML-Document
511      if ( styledObject instanceof StyledGridCoverageInterface )                  final FileWriter out = new FileWriter(new File(documentUrl.toURI()));
512        return getStyledLayerStyle( (StyledGridCoverageInterface)styledObject );                  XML_OUTPUTTER.output(createRasterLegendDataElement(rld), out);
513      if ( styledObject instanceof StyledFeatureCollectionInterface )                  out.flush();
514        return getStyledLayerStyle( (StyledFeatureCollectionInterface)styledObject );                  out.close();
515      throw new UnsupportedOperationException("Unknown type of StyledLayerInterface: "+(styledObject == null ? null : styledObject.getClass().getSimpleName()));          }
516    }  
517            /**
518    /**           * Parses a {@link Translation} object from an JDOM-{@link Element}. This
519     * Returns the style and raster meta data of a {@link StyledGridCoverageInterface}           * method works like {@link AMLImport#parseTranslation(org.w3c.dom.Node},
520     * as a {@link StyledLayerStyle}.           * but for JDOM.
521     * @param styledGC a styled grid coverage           *
522     */           * @param element
523    public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle(StyledGridCoverageInterface styledGC) {           *            {@link Element} to parse
524      return new StyledLayerStyle<RasterLegendData>(           */
525        styledGC.getStyle(),          public final static Translation parseTranslation(final Element element) {
526        styledGC.getLegendMetaData()                  Translation trans = new Translation();
527      );  
528    }                  if (element == null)
529                            return trans;
530    /**  
531     * Returns the style and attribute meta data of a {@link StyledFeatureCollectionInterface}                  for (final Element translationElement : (List<Element>) element
532     * as a {@link StyledLayerStyle}.                                  .getChildren()) {
533     * @param styledFC a styled feature collection                          final String name = translationElement.getName();
534     */                          if (name == null)
535    public static StyledLayerStyle<Map<Integer,AttributeMetaData>> getStyledLayerStyle(StyledFeatureCollectionInterface styledFC) {                                  continue;
536      return new StyledLayerStyle<Map<Integer,AttributeMetaData>>(  
537        styledFC.getStyle(),                          // lang attribute
538        styledFC.getAttributeMetaDataMap()                          String lang = translationElement.getAttributeValue("lang");
539      );                          // set the default, if no language code is set
540    }                          if (lang == null)
541                                    lang = Translation.DEFAULT_KEY;
542    /**  
543     * Loads a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData Raster-LegendData}                          final String translationText = translationElement.getValue();
544     * for a given geo-object (raster) source. The SLD file must be present. A missing                          if (translationText == null)
545     * raster legend-data file is tolerated.                                  trans.put(lang, "");
546     * @param geoObjectURL URL of the (already read) raster object                          else
547     * @param sldExt file extention for the SLD file                                  trans.put(lang, translationText);
548     * @param rldExt file extention for the raster legend-data file                  }
549     * @return {@code null} in case of any error  
550     */                  // if no <translation> is given, the value of the node should
551    public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL, String sldExt, String rldExt) {                  // be used as a default translation
552      RasterLegendData metaData = null;                  if (trans.size() == 0)
553      Style sldStyle = null;                          trans.put(Translation.DEFAULT_KEY, element.getValue());
554      try {                  // trans = new Translation(
555        Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt));                  // ((List<Element>)element.getChildren()).get(0).getValue() );
556        // SLD must be present  
557        if ( styles == null || styles.length == 0 )                  return trans;
558          return null;          }
559        sldStyle = styles[0];  
560      }          /**
561      catch (Exception err) {           * Creates an JDOM {@link Element} for the given {@link Translation}.
562        // SLD must be present           *
563        LangUtil.logDebugError(LOGGER,err);           * @param tagname
564        return null;           *            Name of the Element
565      }           * @param translation
566             *            Translation to store in the Element
567      try {           */
568        metaData = StyledLayerUtil.loadRasterLegendData( IOUtil.changeUrlExt(geoObjectURL,rldExt) );          public final static Element createTranslationElement(String tagname,
569      } catch (FileNotFoundException err) {                          Translation translation) {
570        // ignore missing raster legend data                  Element element = new Element(tagname, AMLURI);
571      } catch (Exception err) {                  if (translation == null)
572        // any other error during legend data creation leads to error                          throw new UnsupportedOperationException(
573        LangUtil.logDebugError(LOGGER,err);                                          "Translation element can not be created from null!");
574        return null;  
575      }                  // If only a default translation is set, the <translation
576      return new StyledLayerStyle<RasterLegendData>(sldStyle, metaData);                  // lang="..">..</tranlation>
577    }                  // part is not used
578                    if (translation.keySet().size() == 1
579    /**                                  && translation.get(Translation.DEFAULT_KEY) != null) {
580     * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and                          element.addContent(translation.get(Translation.DEFAULT_KEY));
581     * {@linkplain RasterLegendData Raster-LegendData} from a {@code .rld} file                          return element;
582     * for a given geo-object (raster) source. The SLD file must be present. A missing                  }
583     * raster legend-data file is tolerated.  
584     * @param geoObjectURL URL of the (already read) raster object                  // add a <translation lang="..">..</tranlation> part to the element for
585     * @param sldExt file extention for the SLD file                  // all languages
586     * @param rldExt file extention for the raster legend-data file                  for (String lang : translation.keySet()) {
587     * @return {@code null} in case of any error                          Element translationElement = new Element(ELEM_NAME_TRANSLATION,
588     */                                          AMLURI);
589    public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL) {                          translationElement.setAttribute("lang", lang);
590      return loadStyledRasterStyle(geoObjectURL, "sld", "rld");                          String translationString = translation.get(lang);
591    }                          if (translationString == null)
592                                    translationString = "";
593    /**                          translationElement.addContent(translationString);
594     * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData AttributeMetaData-Map}                          element.addContent(translationElement);
595     * for a given geo-object (feature) source. The SLD file must be present. A missing                  }
596     * attribute meta-data file is tolerated.  
597     * @param geoObjectURL URL of the (already read) feature object                  return element;
598     * @param sldExt file extention for the SLD file          }
599     * @param rldExt file extention for the raster legend-data file  
600     * @return {@code null} in case of any error          /**
601     */           * Sets a style to {@link StyledLayerInterface}.
602    public static StyledLayerStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL, String sldExt, String rldExt) {           *
603      Map<Integer,AttributeMetaData> metaData = null;           * @param styledObject
604      Style                          sldStyle = null;           *            a styled object
605      try {           * @param style
606        Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt));           *            a Style
607        // SLD must be present           */
608        if ( styles == null || styles.length == 0 )          public static void setStyledLayerStyle(StyledLayerInterface styledObject,
609          return null;                          StyledLayerStyle<?> style) {
610        sldStyle = styles[0];                  // set SLD style
611      } catch (Exception err) {                  styledObject.setStyle(style.getGeoObjectStyle());
612        // SLD must be present                  // set meta data
613        LangUtil.logDebugError(LOGGER,err);                  if (styledObject instanceof StyledGridCoverageInterface
614        return null;                                  && (style.getMetaData() instanceof RasterLegendData || style
615      }                                                  .getMetaData() == null)) {
616                            RasterLegendData sourceRld = (RasterLegendData) style.getMetaData();
617      try {                          RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject)
618        metaData = StyledLayerUtil.loadAttributeMetaDataMap( IOUtil.changeUrlExt(geoObjectURL,rldExt) );                                          .getLegendMetaData();
619      } catch (FileNotFoundException err) {                          if (destRld != null && sourceRld != null) {
620        // ignore missing attribute meta data                                  destRld.setPaintGaps(sourceRld.isPaintGaps());
621      } catch (Exception err) {                                  destRld.clear();
622        // any other error during meta data creation leads to error                                  destRld.putAll(sourceRld);
623        LangUtil.logDebugError(LOGGER,err);                          }
624        return null;                          return;
625      }                  }
626                    if (styledObject instanceof StyledFeatureCollectionInterface
627      return new StyledLayerStyle<Map<Integer,AttributeMetaData>>(sldStyle, metaData);                                  && (style.getMetaData() instanceof Map || style.getMetaData() == null)) {
628    }                          Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>) style
629                                            .getMetaData();
630    /**                          Map<Integer, AttributeMetaData> destAmd = ((StyledFeatureCollectionInterface) styledObject)
631     * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and                                          .getAttributeMetaDataMap();
632     * {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd} file                          if (destAmd != null && sourceAmd != null) {
633     * for a given geo-object (feature) source. The SLD file must be present. A missing                                  destAmd.clear();
634     * attribute meta-data file is tolerated.                                  destAmd.putAll(sourceAmd);
635     * @param geoObjectURL URL of the (already read) feature object                          }
636     * @param sldExt file extention for the SLD file                          return;
637     * @param rldExt file extention for the raster legend-data file                  }
638     * @return {@code null} in case of any error  
639     */                  throw new UnsupportedOperationException(
640    public static StyledLayerStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL) {                                  "Style is not compatible to object: "
641      return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");                                                  + (style.getMetaData() == null ? null : style
642    }                                                                  .getMetaData().getClass().getSimpleName())
643                                                    + " <-> "
644    /**                                                  + (styledObject == null ? null : styledObject
645     * Stores a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData Raster-LegendData}                                                                  .getClass().getSimpleName()));
646     * for a given geo-object (raster) source.          }
647     * @param style  style to save  
648     * @param geoObjectURL URL of the raster object          /**
649     * @param sldExt file extention for the SLD file           * Returns the style a {@link StyledLayerInterface} as a
650     * @param mdExt file extention for the meta-data file           * {@link StyledLayerStyle}.
651     */           *
652    public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style, URL geoObjectURL, String sldExt, String mdExt) throws Exception {           * @param styledObject
653      // Store the SLD           *            a styled object
654      Style sldStyle = style.getGeoObjectStyle();           * @return {@code StyledLayerStyle<RasterLegendData>} for
655      if ( sldStyle != null ) {           *         {@link StyledGridCoverageInterface} or {@code
656        StylingUtil.saveStyleToSLD(           *         StyledLayerStyle<Map<Integer,AttributeMetaData>>} for
657           sldStyle,           *         {@link StyledFeatureCollectionInterface}
658           IOUtil.changeFileExt(           */
659                new File(geoObjectURL.toURI()),          public static StyledLayerStyle<?> getStyledLayerStyle(
660                sldExt                          StyledLayerInterface styledObject) {
661           )                  if (styledObject instanceof StyledGridCoverageInterface)
662        );                          return getStyledLayerStyle((StyledGridCoverageInterface) styledObject);
663      }                  if (styledObject instanceof StyledFeatureCollectionInterface)
664                            return getStyledLayerStyle((StyledFeatureCollectionInterface) styledObject);
665      // Store the meta data                  throw new UnsupportedOperationException(
666      T metaData = style.getMetaData();                                  "Unknown type of StyledLayerInterface: "
667      if ( metaData != null ) {                                                  + (styledObject == null ? null : styledObject
668        if ( metaData instanceof RasterLegendData ) {                                                                  .getClass().getSimpleName()));
669          saveRasterLegendData(          }
670              (RasterLegendData)metaData,  
671              IOUtil.changeUrlExt(geoObjectURL,mdExt)          /**
672          );           * Returns the style and raster meta data of a
673  //      } else if ( metaData instanceof Map<Integer,AttributeMetaData> ) { // LEIDER NICHT KOMPILIERBAR!!           * {@link StyledGridCoverageInterface} as a {@link StyledLayerStyle}.
674        } else if ( metaData instanceof Map ) {           *
675          saveAttributeMetaDataMap(           * @param styledGC
676              (Map<Integer,AttributeMetaData>)metaData,           *            a styled grid coverage
677              IOUtil.changeUrlExt(geoObjectURL,mdExt)           */
678          );          public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle(
679        } else                          StyledGridCoverageInterface styledGC) {
680          throw new UnsupportedOperationException("Export for meta data not yet supported: "+metaData.getClass().getSimpleName());                  return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(),
681      }                                  styledGC.getLegendMetaData());
682    }          }
683    
684    /**          /**
685     * Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and           * Returns the style and attribute meta data of a
686     * the meta data ({@link RasterLegendData} or {@link AttributeMetaData})           * {@link StyledFeatureCollectionInterface} as a {@link StyledLayerStyle}.
687     * to a {@code .rld} or {@code .amd} file.           *
688     * for a given geo-object source.           * @param styledFC
689     * @param style  style to save           *            a styled feature collection
690     * @param geoObjectURL URL of the (already read) raster object           */
691     */          public static StyledLayerStyle<Map<Integer, AttributeMetaData>> getStyledLayerStyle(
692    public static void saveStyledLayerStyle(StyledLayerStyle<?> style, URL geoObjectURL) throws Exception {                          StyledFeatureCollectionInterface styledFC) {
693      if ( style.getMetaData() instanceof RasterLegendData )                  return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(styledFC
694        saveStyledLayerStyle(style,geoObjectURL, "sld", "rld");                                  .getStyle(), styledFC.getAttributeMetaDataMap());
695      else          }
696        saveStyledLayerStyle(style,geoObjectURL, "sld", "amd");  
697    }          /**
698             * Loads a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData
699  }           * Raster-LegendData} for a given geo-object (raster) source. The SLD file
700             * must be present. A missing raster legend-data file is tolerated.
701             *
702             * @param geoObjectURL
703             *            URL of the (already read) raster object
704             * @param sldExt
705             *            file extention for the SLD file
706             * @param rldExt
707             *            file extention for the raster legend-data file
708             * @return {@code null} in case of any error
709             */
710            public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(
711                            URL geoObjectURL, String sldExt, String rldExt) {
712                    RasterLegendData metaData = null;
713                    Style sldStyle = null;
714                    try {
715                            Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(
716                                            geoObjectURL, sldExt));
717                            // SLD must be present
718                            if (styles == null || styles.length == 0)
719                                    return null;
720                            sldStyle = styles[0];
721                    } catch (Exception err) {
722                            // SLD must be present
723                            LangUtil.logDebugError(LOGGER, err);
724                            return null;
725                    }
726    
727                    try {
728                            metaData = StyledLayerUtil.loadRasterLegendData(IOUtil
729                                            .changeUrlExt(geoObjectURL, rldExt));
730                    } catch (FileNotFoundException err) {
731                            // ignore missing raster legend data
732                    } catch (Exception err) {
733                            // any other error during legend data creation leads to error
734                            LangUtil.logDebugError(LOGGER, err);
735                            return null;
736                    }
737                    return new StyledLayerStyle<RasterLegendData>(sldStyle, metaData);
738            }
739    
740            /**
741             * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
742             * {@linkplain RasterLegendData Raster-LegendData} from a {@code .rld} file
743             * for a given geo-object (raster) source. The SLD file must be present. A
744             * missing raster legend-data file is tolerated.
745             *
746             * @param geoObjectURL
747             *            URL of the (already read) raster object
748             * @param sldExt
749             *            file extention for the SLD file
750             * @param rldExt
751             *            file extention for the raster legend-data file
752             * @return {@code null} in case of any error
753             */
754            public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(
755                            URL geoObjectURL) {
756                    return loadStyledRasterStyle(geoObjectURL, "sld", "rld");
757            }
758    
759            /**
760             * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData
761             * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD
762             * file must be present. A missing attribute meta-data file is tolerated.
763             *
764             * @param geoObjectURL
765             *            URL of the (already read) feature object
766             * @param sldExt
767             *            file extention for the SLD file
768             * @param rldExt
769             *            file extention for the raster legend-data file
770             * @return {@code null} in case of any error
771             */
772            public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle(
773                            URL geoObjectURL, String sldExt, String rldExt) {
774                    Map<Integer, AttributeMetaData> metaData = null;
775                    Style sldStyle = null;
776                    try {
777                            Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(
778                                            geoObjectURL, sldExt));
779                            // SLD must be present
780                            if (styles == null || styles.length == 0)
781                                    return null;
782                            sldStyle = styles[0];
783                    } catch (Exception err) {
784                            // SLD must be present
785                            LangUtil.logDebugError(LOGGER, err);
786                            return null;
787                    }
788    
789                    try {
790                            metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil
791                                            .changeUrlExt(geoObjectURL, rldExt));
792                    } catch (FileNotFoundException err) {
793                            // ignore missing attribute meta data
794                    } catch (Exception err) {
795                            // any other error during meta data creation leads to error
796                            LangUtil.logDebugError(LOGGER, err);
797                            return null;
798                    }
799    
800                    return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(sldStyle,
801                                    metaData);
802            }
803    
804            /**
805             * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
806             * {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd}
807             * file for a given geo-object (feature) source. The SLD file must be
808             * present. A missing attribute meta-data file is tolerated.
809             *
810             * @param geoObjectURL
811             *            URL of the (already read) feature object
812             * @param sldExt
813             *            file extention for the SLD file
814             * @param rldExt
815             *            file extention for the raster legend-data file
816             * @return {@code null} in case of any error
817             */
818            public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle(
819                            URL geoObjectURL) {
820                    return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");
821            }
822    
823            /**
824             * Stores a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData
825             * Raster-LegendData} for a given geo-object (raster) source.
826             *
827             * @param style
828             *            style to save
829             * @param geoObjectURL
830             *            URL of the raster object
831             * @param sldExt
832             *            file extention for the SLD file
833             * @param mdExt
834             *            file extention for the meta-data file
835             */
836            public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style,
837                            URL geoObjectURL, String sldExt, String mdExt) throws Exception {
838                    // Store the SLD
839                    Style sldStyle = style.getGeoObjectStyle();
840                    if (sldStyle != null) {
841                            StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File(
842                                            geoObjectURL.toURI()), sldExt));
843                    }
844    
845                    // Store the meta data
846                    T metaData = style.getMetaData();
847                    if (metaData != null) {
848                            if (metaData instanceof RasterLegendData) {
849                                    saveRasterLegendData((RasterLegendData) metaData, IOUtil
850                                                    .changeUrlExt(geoObjectURL, mdExt));
851                                    // } else if ( metaData instanceof
852                                    // Map<Integer,AttributeMetaData> ) { // LEIDER NICHT
853                                    // KOMPILIERBAR!!
854                            } else if (metaData instanceof Map) {
855                                    saveAttributeMetaDataMap(
856                                                    (Map<Integer, AttributeMetaData>) metaData, IOUtil
857                                                                    .changeUrlExt(geoObjectURL, mdExt));
858                            } else
859                                    throw new UnsupportedOperationException(
860                                                    "Export for meta data not yet supported: "
861                                                                    + metaData.getClass().getSimpleName());
862                    }
863            }
864    
865            /**
866             * Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and the
867             * meta data ({@link RasterLegendData} or {@link AttributeMetaData}) to a
868             * {@code .rld} or {@code .amd} file. for a given geo-object source.
869             *
870             * @param style
871             *            style to save
872             * @param geoObjectURL
873             *            URL of the (already read) raster object
874             */
875            public static void saveStyledLayerStyle(StyledLayerStyle<?> style,
876                            URL geoObjectURL) throws Exception {
877                    if (style.getMetaData() instanceof RasterLegendData)
878                            saveStyledLayerStyle(style, geoObjectURL, "sld", "rld");
879                    else
880                            saveStyledLayerStyle(style, geoObjectURL, "sld", "amd");
881            }
882    
883            /**
884             * Creates a {@link Box} that shows a legend for a list of
885             * {@link FeatureTypeStyle}s and a targeted featureType
886             *
887             * @param featureType
888             *            If this a legend for Point, Polygon or Line?
889             * @param list
890             *            The Styles to presented in this legend
891             *
892             * @author <a href="mailto:[email protected]">Stefan Alfons
893             *         Kr&uuml;ger</a>
894             */
895            public static Box createLegendPanel(List<FeatureTypeStyle> list,
896                            SimpleFeatureType featureType, int iconWidth, int iconHeight) {
897    
898                    Box box = new Box(BoxLayout.Y_AXIS) {
899    
900                            /**
901                             * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot
902                             * gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS
903                             * gesetzt.
904                             *
905                             * @author <a href="mailto:[email protected]">Stefan Alfons
906                             *         Kr&uuml;ger</a>
907                             */
908                            @Override
909                            public void print(Graphics g) {
910                                    final Color orig = getBackground();
911                                    setBackground(Color.WHITE);
912                                    // wrap in try/finally so that we always restore the state
913                                    try {
914                                            super.print(g);
915                                    } finally {
916                                            setBackground(orig);
917                                    }
918                            }
919                    };
920    
921                    for (FeatureTypeStyle ftStyle : list) {
922    
923                            // One child-node for every rule
924                            List<Rule> rules = ftStyle.rules();
925                            for (Rule rule : rules) {
926    
927                                    /**
928                                     * Let's not create a hbox for Rules that only contain
929                                     * TextSymbolizers
930                                     */
931                                    if (StylingUtil.getTextSymbolizers(rule.getSymbolizers())
932                                                    .size() == rule.getSymbolizers().length)
933                                            continue;
934    
935                                    Box hbox = new Box(BoxLayout.X_AXIS) {
936    
937                                            /**
938                                             * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein
939                                             * Screenshot gemacht wird) wird. Dann werden wird der
940                                             * Hintergrund auf WEISS gesetzt.
941                                             */
942                                            @Override
943                                            public void print(Graphics g) {
944                                                    final Color orig = getBackground();
945                                                    setBackground(Color.WHITE);
946                                                    // wrap in try/finally so that we always restore the
947                                                    // state
948                                                    try {
949                                                            super.print(g);
950                                                    } finally {
951                                                            setBackground(orig);
952                                                    }
953                                            }
954                                    };
955    
956                                    /**
957                                     * The size of the legend Symbol is dependent on the size of the
958                                     * font.
959                                     */
960                                    final int fontHeight = new JLabel().getFontMetrics(
961                                                    new JLabel().getFont()).getHeight();
962    
963                                    final Dimension ICON_SIZE = new Dimension(iconWidth,
964                                                    fontHeight > 5 ? fontHeight : iconHeight);
965    
966                                    // ****************************************************************************
967                                    // Create the actual icon
968                                    // ****************************************************************************
969                                    final BufferedImage imageForRule = LegendIconFeatureRenderer
970                                                    .getInstance().createImageForRule(rule, featureType,
971                                                                    ICON_SIZE);
972    
973                                    // LOGGER.debug("Creating a new Legend Image for RUle name =
974                                    // "+rule.getName());
975    
976                                    ImageIcon legendIcon = new ImageIcon(imageForRule);
977    
978                                    final JLabel iconLabel = new JLabel(legendIcon);
979                                    hbox.setAlignmentX(0f);
980                                    hbox.add(iconLabel);
981                                    hbox.add(Box.createHorizontalStrut(3));
982    
983                                    Translation labelT = new Translation();
984                                    labelT.fromOneLine(rule.getDescription().getTitle());
985    
986                                    final JLabel classTitleLabel = new JLabel(labelT.toString());
987                                    hbox.add(classTitleLabel);
988                                    classTitleLabel.setLabelFor(iconLabel);
989    
990                                    box.add(hbox);
991    
992                            }
993                    }
994    
995                    return box;
996            }
997    
998            /**
999             * Creates a
1000             *
1001             * @param styledRaster
1002             * @param iconHeight
1003             * @param iconWidth
1004             * @return
1005             */
1006            public static Box createLegendPanel(StyledRasterInterface<?> styledRaster,
1007                            int iconWidth, int iconHeight) {
1008    
1009                    /**
1010                     * Determine whether a Style is responsible for the coloring
1011                     */
1012                    ColorModel colorModel = null;
1013                    if (!isStyleable(styledRaster)
1014                                    || (isStyleable(styledRaster) && styledRaster.getStyle() == null)) {
1015                            colorModel = getColorModel(styledRaster);
1016                    }
1017    
1018                    RasterLegendData rasterLegendData = styledRaster.getLegendMetaData();
1019                    List<Double> legendRasterValues = rasterLegendData.getSortedKeys();
1020                    Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
1021                                    .createSampleRasters();
1022    
1023                    Box box = new Box(BoxLayout.Y_AXIS) {
1024    
1025                            /**
1026                             * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot
1027                             * gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS
1028                             * gesetzt.
1029                             */
1030                            @Override
1031                            public void print(Graphics g) {
1032                                    final Color orig = getBackground();
1033                                    setBackground(Color.WHITE);
1034                                    // wrap in try/finally so that we always restore the state
1035                                    try {
1036                                            super.print(g);
1037                                    } finally {
1038                                            setBackground(orig);
1039                                    }
1040                            }
1041                    };
1042    
1043                    for (Double rValue : legendRasterValues) {
1044    
1045                            /**
1046                             *
1047                             */
1048                            Box hbox = new Box(BoxLayout.X_AXIS) {
1049    
1050                                    /**
1051                                     * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein
1052                                     * Screenshot gemacht wird) wird. Dann werden wird der
1053                                     * Hintergrund auf WEISS gesetzt.
1054                                     */
1055                                    @Override
1056                                    public void print(Graphics g) {
1057                                            final Color orig = getBackground();
1058                                            setBackground(Color.WHITE);
1059                                            // wrap in try/finally so that we always restore the
1060                                            // state
1061                                            try {
1062                                                    super.print(g);
1063                                            } finally {
1064                                                    setBackground(orig);
1065                                            }
1066                                    }
1067                            };
1068    
1069                            final Dimension ICON_SIZE = new Dimension(iconWidth,
1070                                            new JLabel().getFontMetrics(new JLabel().getFont())
1071                                                            .getHeight() > 5 ? new JLabel().getFontMetrics(
1072                                                            new JLabel().getFont()).getHeight() : iconHeight);
1073    
1074                            // ****************************************************************************
1075                            // Create the actual icon
1076                            // ****************************************************************************
1077                            BufferedImage buffImage = new BufferedImage(ICON_SIZE.width,
1078                                            ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);
1079                            Graphics2D graphics = buffImage.createGraphics();
1080    
1081                            if (colorModel != null) {
1082    
1083                                    try {
1084                                            Object inData = null;
1085                                            switch (colorModel.getTransferType()) {
1086                                            case DataBuffer.TYPE_BYTE:
1087                                                    inData = new byte[] { rValue.byteValue() };
1088                                                    break;
1089                                            case DataBuffer.TYPE_USHORT:
1090                                                    inData = new short[] { rValue.shortValue() };
1091                                                    break;
1092                                            case DataBuffer.TYPE_INT:
1093                                                    inData = new int[] { rValue.intValue() };
1094                                                    break;
1095                                            case DataBuffer.TYPE_SHORT:
1096                                                    inData = new short[] { rValue.shortValue() };
1097                                                    break;
1098                                            case DataBuffer.TYPE_FLOAT:
1099                                                    inData = new float[] { rValue.floatValue() };
1100                                                    break;
1101                                            case DataBuffer.TYPE_DOUBLE:
1102                                                    inData = new double[] { rValue.doubleValue() };
1103                                                    break;
1104                                            default:
1105                                                    inData = rValue.intValue();
1106                                            }
1107                                            final Color color = new Color(colorModel.getRGB(inData));
1108                                            graphics.setBackground(color);
1109                                            graphics.setColor(color);
1110                                            graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height);
1111                                    } catch (Exception e) {
1112                                            LOGGER.debug("Dann nehmen wir halt den GridCoverageRenderer", e);
1113                                            colorModel = null;
1114                                    }
1115                            }
1116    
1117                            if (colorModel == null) {
1118    
1119                                    /**
1120                                     * The coverage contains only one value of value rValue
1121                                     */
1122                                    GridCoverage2D sampleCov = sampleRasters.get(rValue);
1123                                    GridCoverageRenderer renderer;
1124                                    try {
1125                                            renderer = new GridCoverageRenderer(sampleCov
1126                                                            .getCoordinateReferenceSystem(), JTSUtil
1127                                                            .createEnvelope(sampleCov.getEnvelope()),
1128                                                            new Rectangle(iconWidth, iconHeight),
1129                                                            (AffineTransform) null);
1130                                    } catch (Exception e1) {
1131                                            throw new RuntimeException(
1132                                                            "Creating the GridCoverageRenderer:", e1);
1133                                    }
1134    
1135                                    /**
1136                                     * Iterate over all FeatureTypeStyles.
1137                                     */
1138                                    // for (FeatureTypeStyle ftStyle : styledGrid.getStyle()
1139                                    // .featureTypeStyles()) {
1140                                    // One child-node for every rule
1141                                    // List<Rule> rules = ftStyle.rules();
1142                                    // for (Rule rule : rules) {
1143                                    final Style style = styledRaster.getStyle();
1144                                    List<RasterSymbolizer> rSymbols = StylingUtil
1145                                                    .getRasterSymbolizers(style);
1146    
1147                                    for (RasterSymbolizer symbolizer : rSymbols) {
1148                                            // LOGGER.debug("Creating a new Legend Image for RUle
1149                                            // name =
1150                                            // "+rule.getName());
1151                                            try {
1152                                                    renderer.paint(graphics, sampleCov, symbolizer);
1153                                            } catch (Exception ee) {
1154                                                    LOGGER.error("Unable to paint " + symbolizer
1155                                                                    + " into the legend image", ee);
1156                                            }
1157                                            // }
1158                                            // }
1159                                    }
1160                            }
1161    
1162                            ImageIcon legendIcon = new ImageIcon(buffImage);
1163    
1164                            final JLabel iconLabel = new JLabel(legendIcon);
1165                            hbox.setAlignmentX(0f);
1166                            hbox.add(iconLabel);
1167                            hbox.add(Box.createHorizontalStrut(3));
1168    
1169                            Translation labelT = rasterLegendData.get(rValue);
1170                            final JLabel classTitleLabel = new JLabel(labelT.toString());
1171                            hbox.add(classTitleLabel);
1172                            classTitleLabel.setLabelFor(iconLabel);
1173    
1174                            box.add(hbox);
1175    
1176                            if (rasterLegendData.getPaintGaps()) {
1177                                    iconLabel
1178                                                    .setBorder(BorderFactory.createLineBorder(Color.black));
1179                                    box.add(Box.createVerticalStrut(3));
1180                            }
1181    
1182                    }
1183    
1184                    return box;
1185            }
1186    
1187            /**
1188             * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May
1189             * return <code>null</code> if the geoobject can not be accessed.
1190             */
1191            public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) {
1192                    ColorModel colorModel = null;
1193                    try {
1194                            Object geoObject = styledGrid.getGeoObject();
1195                            if (geoObject instanceof GridCoverage2D) {
1196                                    GridCoverage2D cov = (GridCoverage2D) geoObject;
1197                                    colorModel = cov.getRenderedImage().getColorModel();
1198                            } else if (styledGrid instanceof StyledRasterPyramidInterface) {
1199    
1200                                    Parameter readGG = new Parameter(
1201                                                    AbstractGridFormat.READ_GRIDGEOMETRY2D);
1202    
1203                                    ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope(
1204                                                    styledGrid.getEnvelope(), styledGrid.getCrs());
1205    
1206                                    readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(
1207                                                    new Rectangle(0, 0, 10, 10)), mapExtend));
1208    
1209                                    final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) geoObject;
1210                                    GridCoverage2D cov = (GridCoverage2D) aReader
1211                                                    .read(new GeneralParameterValue[] { readGG });
1212                                    colorModel = cov.getRenderedImage().getColorModel();
1213                            }
1214                    } catch (Exception e) {
1215                            LOGGER.error("Error reading the colormodel from " + styledGrid);
1216                            return null;
1217                    }
1218                    return colorModel;
1219            }
1220    
1221            /**
1222             * @return <code>true</code> if a {@link RasterSymbolizer} can be applied
1223             *         and will have an effect. Some rasters (e.g. GeoTIFF) can come
1224             *         with their own {@link ColorModel} and will ignore any
1225             *         {@link RasterSymbolizer} = SLD.
1226             */
1227            public static boolean isStyleable(StyledRasterInterface<?> styledRaster) {
1228                    ColorModel colorModel = getColorModel(styledRaster);
1229    
1230                    LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
1231                                    + colorModel.getClass().getSimpleName());
1232    
1233                    if (colorModel == null)
1234                            return true;
1235                    if (colorModel instanceof ComponentColorModel)
1236                            return true;
1237                    return false;
1238            }
1239    }

Legend:
Removed from v.224  
changed lines
  Added in v.419

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26