/[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

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

Legend:
Removed from v.397  
changed lines
  Added in v.516

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26