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

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

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

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

Legend:
Removed from v.52  
changed lines
  Added in v.403

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26