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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26