/[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 trunk/src/skrueger/geotools/StyledLayerUtil.java revision 244 by alfonx, Wed Jul 29 09:33:33 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, focussing (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.io.File;
33  import java.io.File;  import java.io.FileNotFoundException;
34  import java.io.FileNotFoundException;  import java.io.FileWriter;
35  import schmitzm.lang.LangUtil;  import java.net.URL;
36  import schmitzm.swing.SwingUtil;  import java.text.DecimalFormat;
37    import java.util.HashMap;
38  import java.io.FileWriter;  import java.util.List;
39    import java.util.Map;
40  /**  import java.util.SortedMap;
41   * This class provides static helper methods for dealing with  import java.util.TreeMap;
42   * {@link StyledMapInterface} stuff.  
43   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)  import org.apache.log4j.Logger;
44   * @version 1.0  import org.geotools.coverage.grid.GridCoverage2D;
45   */  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
46  public class StyledMapUtil {  import org.geotools.feature.FeatureCollection;
47    private static final Logger LOGGER = Logger.getLogger(StyledMapUtil.class.getName());  import org.geotools.map.DefaultMapLayer;
48    private static final SAXBuilder SAX_BUILDER = new SAXBuilder();  import org.geotools.map.MapLayer;
49    private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter();  import org.geotools.styling.ColorMap;
50    import org.geotools.styling.ColorMapEntry;
51    /** URL for Atlas XML schema */  import org.geotools.styling.RasterSymbolizer;
52    public static final String AMLURI = "http://www.wikisquare.de/AtlasML";  import org.geotools.styling.Style;
53    /** Name of the XML Element for the attribute meta data map */  import org.jdom.Document;
54    public static final String ELEM_NAME_AMD = "attributeMetaData";  import org.jdom.Element;
55    /** Name of the XML Element for the raster legend data */  import org.jdom.input.SAXBuilder;
56    public static final String ELEM_NAME_RLD = "rasterLegendData";  import org.jdom.output.XMLOutputter;
57    /** Name of the XML Element for an attribute meta data map entry */  
58    public static final String ELEM_NAME_ATTRIBUTE = "dataAttribute";  import schmitzm.geotools.styling.StylingUtil;
59    /** Name of the XML Element for an raster legend data entry */  import schmitzm.io.IOUtil;
60    public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem";  import schmitzm.lang.LangUtil;
61    /** Name of the XML Element for a translation */  import schmitzm.swing.SwingUtil;
62    public static final String ELEM_NAME_TRANSLATION = "translation";  import skrueger.AttributeMetaData;
63    import skrueger.RasterLegendData;
64    /**  import skrueger.i8n.Translation;
65     * Creates a Geotools {@link MapLayer} from an object. If the object is a  
66     * {@link StyledMapInterface} then its sytle is used. In case of direct  /**
67     * Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader},   * This class provides static helper methods for dealing with
68     * {@link FeatureCollection}) a default style is generated.   * {@link StyledLayerInterface} stuff.
69     * @param object an Object   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
70     * @exception Exception if {@code null} is given as object or an error occurs during layer creation   * @version 1.0
71     */   */
72    public static MapLayer createMapLayer(Object object) throws Exception {  public class StyledLayerUtil {
73      return createMapLayer(object,null);    private static final Logger LOGGER = Logger.getLogger(StyledLayerUtil.class.getName());
74    }    private static final SAXBuilder SAX_BUILDER = new SAXBuilder();
75      private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter();
76    /**  
77     * Creates a Geotools {@link MapLayer} from an object. If the object is a    /** URL for Atlas XML schema */
78     * {@link StyledMapInterface} then its sytle is used. In case of direct    public static final String AMLURI = "http://www.wikisquare.de/AtlasML";
79     * Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader},    /** Name of the XML Element for the attribute meta data map */
80     * {@link FeatureCollection}) a default style is generated.    public static final String ELEM_NAME_AMD = "attributeMetaData";
81     * @param object an Object    /** Name of the XML Element for the raster legend data */
82     * @param forcedStyle (SLD-)Style to force for the object    public static final String ELEM_NAME_RLD = "rasterLegendData";
83     * @exception Exception if {@code null} is given as object or an error occurs during layer creation    /** Name of the XML Element for an attribute meta data map entry */
84     */    public static final String ELEM_NAME_ATTRIBUTE = "dataAttribute";
85    public static MapLayer createMapLayer(Object object, Style forcedStyle) throws Exception {    /** Name of the XML Element for an raster legend data entry */
86      MapLayer layer     = null;    public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem";
87      Style    style     = null;    /** Name of the XML Element for a translation */
88      if ( object instanceof StyledMapInterface ) {    public static final String ELEM_NAME_TRANSLATION = "translation";
89        style =  ((StyledMapInterface<?>)object).getStyle();  
90        object = ((StyledMapInterface<?>)object).getGeoObject();    /**
91      }     * Creates a Geotools {@link MapLayer} from an object. If the object is a
92      if ( forcedStyle != null )     * {@link StyledLayerInterface} then its sytle is used. In case of direct
93        style = forcedStyle;     * Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader},
94      if ( style == null )     * {@link FeatureCollection}) a default style is generated.
95        style = StylingUtil.createDefaultStyle(object);     * @param object an Object
96       * @exception Exception if {@code null} is given as object or an error occurs during layer creation
97      if (object instanceof GridCoverage2D)     */
98        layer = new DefaultMapLayer( (GridCoverage2D) object, style);    public static MapLayer createMapLayer(Object object) throws Exception {
99      if (object instanceof AbstractGridCoverage2DReader)      return createMapLayer(object,null);
100        layer = new DefaultMapLayer( (AbstractGridCoverage2DReader) object, style);    }
101      if (object instanceof FeatureCollection)  
102        layer = new DefaultMapLayer( (FeatureCollection) object, style);    /**
103       * Creates a Geotools {@link MapLayer} from an object. If the object is a
104      if ( layer == null )     * {@link StyledLayerInterface} then its sytle is used. In case of direct
105        throw new Exception("Can not create MapLayer from "+(object == null ? "null" : object.getClass()));     * Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader},
106       * {@link FeatureCollection}) a default style is generated.
107      return layer;     * @param object an Object
108    }     * @param forcedStyle (SLD-)Style to force for the object
109       * @exception Exception if {@code null} is given as object or an error occurs during layer creation
110    /**     */
111     * Creates an default instance of {@link StyledMapInterface} for a Geotools    public static MapLayer createMapLayer(Object object, Style forcedStyle) throws Exception {
112     * object ({@link GridCoverage2D}, {@link FeatureCollection}) with a default      MapLayer layer     = null;
113     * style.      Style    style     = null;
114     * @param object an Object      if ( object instanceof StyledLayerInterface ) {
115     * @param title  title for the object        style =  ((StyledLayerInterface<?>)object).getStyle();
116     * @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation        object = ((StyledLayerInterface<?>)object).getGeoObject();
117     */      }
118    public static StyledMapInterface<?> createStyledMap(Object object, String title) {      if ( forcedStyle != null )
119       return createStyledMap(object, title, null);        style = forcedStyle;
120    }      if ( style == null )
121          style = StylingUtil.createDefaultStyle(object);
122    /**  
123     * Creates an default instance of {@link StyledMapInterface} for a Geotools      if (object instanceof GridCoverage2D)
124     * object ({@link GridCoverage2D}, {@link FeatureCollection}) with a given        layer = new DefaultMapLayer( (GridCoverage2D) object, style);
125     * style.      if (object instanceof AbstractGridCoverage2DReader)
126     * @param object an Object        layer = new DefaultMapLayer( (AbstractGridCoverage2DReader) object, style);
127     * @param title  title for the object      if (object instanceof FeatureCollection)
128     * @param style  style and meta data for the object        layer = new DefaultMapLayer( (FeatureCollection) object, style);
129     * @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation  
130     */      if ( layer == null )
131    public static StyledMapInterface<?> createStyledMap(Object object, String title, StyledMapStyle style) {        throw new Exception("Can not create MapLayer from "+(object == null ? "null" : object.getClass()));
132      StyledMapInterface<?> styledObject = null;  
133        return layer;
134      String id = (title != null) ? title : "defaultID";    }
135    
136      if ( object instanceof GridCoverage2D )    /**
137        styledObject = new StyledGridCoverage(     * Creates an default instance of {@link StyledLayerInterface} for a Geotools
138            (GridCoverage2D)object,     * object ({@link GridCoverage2D}, {@link FeatureCollection}) with a default
139            id,     * style.
140            title,     * @param object an Object
141            style     * @param title  title for the object
142        );     * @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation
143      else if ( object instanceof AbstractGridCoverage2DReader )     */
144             styledObject = new StyledGridCoverageReader(    public static StyledLayerInterface<?> createStyledLayer(Object object, String title) {
145                 (AbstractGridCoverage2DReader)object,       return createStyledLayer(object, title, null);
146                 id,    }
147                 title,  
148                 style    /**
149             );     * Creates an default instance of {@link StyledLayerInterface} for a Geotools
150      else if ( object instanceof FeatureCollection )     * object ({@link GridCoverage2D}, {@link FeatureCollection}) with a given
151        styledObject = new StyledFeatureCollection(     * style.
152            (FeatureCollection)object,     * @param object an Object
153            id,     * @param title  title for the object
154            title,     * @param style  style and meta data for the object
155            style     * @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation
156        );     */
157          public static StyledLayerInterface<?> createStyledLayer(Object object, String title, StyledLayerStyle style) {
158      if ( styledObject == null )      StyledLayerInterface<?> styledLayer = null;
159        throw new UnsupportedOperationException("Can not create StyledMapInterface object from "+(object == null ? "null" : object.getClass()));  
160        String id = (title != null) ? title : "defaultID";
161      return styledObject;  
162    }      if ( object instanceof GridCoverage2D )
163          styledLayer = new StyledGridCoverage(
164    /**            (GridCoverage2D)object,
165     * Return only the visible or invisible entries of an AttributeMetaData-Map.            id,
166     * @param amdMap AttributeMetaData-Map            title,
167     * @param visible indicated whether the visible or invisible entries are            style
168     *                returned        );
169     */      else if ( object instanceof AbstractGridCoverage2DReader )
170    public static Map<Integer,AttributeMetaData> getVisibleAttributeMetaData(Map<Integer,AttributeMetaData> amdMap, boolean visible) {             styledLayer = new StyledGridCoverageReader(
171      SortedMap<Integer,AttributeMetaData> filteredMap = new TreeMap<Integer,AttributeMetaData>();                 (AbstractGridCoverage2DReader)object,
172      for (AttributeMetaData amd : amdMap.values())                 id,
173        if ( amd.isVisible() )                 title,
174          filteredMap.put(amd.getColIdx(), amd);                 style
175                   );
176      return filteredMap;      else if ( object instanceof FeatureCollection )
177    }        styledLayer = new StyledFeatureCollection(
178                (FeatureCollection)object,
179                id,
180    /**            title,
181     * Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}.            style
182     * This method works like {@link AMLImport#parseDataAttribute(org.w3c.dom.Node},        );
183     * but for JDOM.      
184     * @param element {@link Element} to parse      if ( styledLayer == null )
185     */        throw new UnsupportedOperationException("Can not create StyledLayerInterface object from "+(object == null ? "null" : object.getClass()));
186    public static AttributeMetaData parseAttributeMetaData(final Element element) {  
187      final Integer col = Integer.valueOf(element.getAttributeValue("col"));      return styledLayer;
188      final Boolean visible = Boolean.valueOf(element.getAttributeValue("visible"));    }
189      final String unit = element.getAttributeValue("unit");  
190      /**
191      Translation name = new Translation();     * Return only the visible or invisible entries of an AttributeMetaData-Map.
192      Translation desc = new Translation();     * @param amdMap AttributeMetaData-Map
193      for (final Element childElement : (List<Element>)element.getChildren()) {     * @param visible indicated whether the visible or invisible entries are
194        if (childElement.getName() == null)     *                returned
195          continue;     */
196      public static SortedMap<Integer,AttributeMetaData> getVisibleAttributeMetaData(Map<Integer,AttributeMetaData> amdMap, boolean visible) {
197        if (childElement.getName().equals("name"))      SortedMap<Integer,AttributeMetaData> filteredMap = new TreeMap<Integer,AttributeMetaData>();
198          name = parseTranslation(childElement);      for (AttributeMetaData amd : amdMap.values())
199        else if (childElement.getName().equals("desc"))        if ( amd.isVisible() )
200          desc = parseTranslation(childElement);          filteredMap.put(amd.getColIdx(), amd);
201      }      
202      return new AttributeMetaData(col, visible, name, desc, unit);      return filteredMap;
203    }    }
204      
205    /**    
206     * Parses a {@link AttributeMetaData} map from an JDOM-{@link Element}    /**
207     * with {@code <attribute>}-childs.     * Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}.
208     * @param element {@link Element} to parse     * This method works like {@link AMLImport#parseDataAttribute(org.w3c.dom.Node},
209     */     * but for JDOM.
210    public static Map<Integer,AttributeMetaData> parseAttributeMetaDataMap(final Element element) {     * @param element {@link Element} to parse
211      HashMap<Integer,AttributeMetaData> metaData = new HashMap<Integer,AttributeMetaData>();     */
212      List<Element> attributesElements = element.getChildren( ELEM_NAME_ATTRIBUTE );    public static AttributeMetaData parseAttributeMetaData(final Element element) {
213      for (Element attibuteElement : attributesElements)      final Integer col = Integer.valueOf(element.getAttributeValue("col"));
214      {      final Boolean visible = Boolean.valueOf(element.getAttributeValue("visible"));
215        AttributeMetaData attrMetaData = parseAttributeMetaData( attibuteElement );      final String unit = element.getAttributeValue("unit");
216        metaData.put( attrMetaData.getColIdx(), attrMetaData );  
217      }      Translation name = new Translation();
218      return metaData;      Translation desc = new Translation();
219    }      for (final Element childElement : (List<Element>)element.getChildren()) {
220          if (childElement.getName() == null)
221    /**          continue;
222     * Loads a {@link AttributeMetaData} object from an URL.  
223     * @param documentUrl {@link URL} to parse        if (childElement.getName().equals("name"))
224     * @see #parseAttributeMetaData(Element)          name = parseTranslation(childElement);
225     */        else if (childElement.getName().equals("desc"))
226    public static Map<Integer,AttributeMetaData> loadAttributeMetaDataMap(final URL documentUrl) throws Exception {          desc = parseTranslation(childElement);
227      Document document = SAX_BUILDER.build(documentUrl);      }
228      return parseAttributeMetaDataMap( document.getRootElement() );      return new AttributeMetaData(col, visible, name, desc, unit);
229    }    }
230    
231    /**    /**
232     * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}     * Parses a {@link AttributeMetaData} map from an JDOM-{@link Element}
233     * object.     * with {@code <attribute>}-childs.
234     * @param amd meta data for one attribute     * @param element {@link Element} to parse
235     */     */
236    public static Element createAttributeMetaDataElement(final AttributeMetaData amd) {    public static Map<Integer,AttributeMetaData> parseAttributeMetaDataMap(final Element element) {
237      final Element element = new Element( ELEM_NAME_ATTRIBUTE , AMLURI);      HashMap<Integer,AttributeMetaData> metaData = new HashMap<Integer,AttributeMetaData>();
238      element.setAttribute("col", String.valueOf( amd.getColIdx() ) );      List<Element> attributesElements = element.getChildren( ELEM_NAME_ATTRIBUTE );
239      element.setAttribute("visible", String.valueOf( amd.isVisible() ) );      for (Element attibuteElement : attributesElements)
240      element.setAttribute("unit", amd.getUnit() );      {
241      // Creating a aml:name tag...        AttributeMetaData attrMetaData = parseAttributeMetaData( attibuteElement );
242      element.addContent( createTranslationElement("name", amd.getTitle()) );        metaData.put( attrMetaData.getColIdx(), attrMetaData );
243      // Creating a aml:desc tag...      }
244      element.addContent( createTranslationElement("desc", amd.getDesc()) );      return metaData;
245      return element;    }
246    }  
247      /**
248    /**     * Loads a {@link AttributeMetaData} object from an URL.
249     * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}     * @param documentUrl {@link URL} to parse
250     * map.     * @see #parseAttributeMetaData(Element)
251     * @param amdMap map of attribute meta data     */
252     */    public static Map<Integer,AttributeMetaData> loadAttributeMetaDataMap(final URL documentUrl) throws Exception {
253    public static Element createAttributeMetaDataMapElement(final Map<Integer,AttributeMetaData> amdMap) {      Document document = SAX_BUILDER.build(documentUrl);
254      final Element element = new Element( ELEM_NAME_AMD , AMLURI);      return parseAttributeMetaDataMap( document.getRootElement() );
255      for (AttributeMetaData amd : amdMap.values())    }
256        element.addContent( createAttributeMetaDataElement( amd ) );  
257      return element;    /**
258    }     * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}
259       * object.
260    /**     * @param amd meta data for one attribute
261     * Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL.     */
262     * @param amdMap map of {@link AttributeMetaData}    public static Element createAttributeMetaDataElement(final AttributeMetaData amd) {
263     * @param documentUrl {@link URL} to store the XML      final Element element = new Element( ELEM_NAME_ATTRIBUTE , AMLURI);
264     */      element.setAttribute("col", String.valueOf( amd.getColIdx() ) );
265    public static void saveAttributeMetaDataMap(final Map<Integer,AttributeMetaData> amdMap, final URL documentUrl) throws Exception {      element.setAttribute("visible", String.valueOf( amd.isVisible() ) );
266      // Create XML-Document      element.setAttribute("unit", amd.getUnit() );
267      final FileWriter out = new FileWriter( new File(documentUrl.toURI()) );      // Creating a aml:name tag...
268      XML_OUTPUTTER.output(      element.addContent( createTranslationElement("name", amd.getTitle()) );
269        createAttributeMetaDataMapElement(amdMap),      // Creating a aml:desc tag...
270        out      element.addContent( createTranslationElement("desc", amd.getDesc()) );
271      );      return element;
272      out.flush();    }
273      out.close();  
274    }    /**
275       * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}
276       * map.
277       * @param amdMap map of attribute meta data
278    /**     */
279     * Parses a {@link RasterLegendData} object from an JDOM-{@link Element}.    public static Element createAttributeMetaDataMapElement(final Map<Integer,AttributeMetaData> amdMap) {
280     * This method works like {@link AMLImport#parseRasterLegendData(org.w3c.dom.Node},      final Element element = new Element( ELEM_NAME_AMD , AMLURI);
281     * but for JDOM.      for (AttributeMetaData amd : amdMap.values())
282     * @param element {@link Element} to parse        element.addContent( createAttributeMetaDataElement( amd ) );
283     */      return element;
284    public static RasterLegendData parseRasterLegendData(Element element) {    }
285    
286      final boolean paintGaps = Boolean.valueOf( element.getAttributeValue("paintGaps") );    /**
287       * Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL.
288      RasterLegendData rld = new RasterLegendData(paintGaps);     * @param amdMap map of {@link AttributeMetaData}
289       * @param documentUrl {@link URL} to store the XML
290      for ( Element childElement : (List<Element>)element.getChildren() ) {     */
291        final String name = childElement.getName();    public static void saveAttributeMetaDataMap(final Map<Integer,AttributeMetaData> amdMap, final URL documentUrl) throws Exception {
292        // Cancel if it's an attribute      // Create XML-Document
293        if ( childElement.getChildren().size() == 0 )      final FileWriter out = new FileWriter( new File(documentUrl.toURI()) );
294          continue;      XML_OUTPUTTER.output(
295          createAttributeMetaDataMapElement(amdMap),
296        if (name.equals( ELEM_NAME_RASTERLEGEND )) {        out
297          final String valueAttr = childElement.getAttributeValue("value");      );
298          if ( valueAttr == null )      out.flush();
299            throw new UnsupportedOperationException("Attribute 'value' missing for definition of <"+ELEM_NAME_RASTERLEGEND+">");      out.close();
300          final double value = Double.valueOf(valueAttr);    }
301    
302          // first and only item should be the label  
303          final Element labelElement = childElement.getChild("label");  
304          // id label element is missing, the translation is searched directly    /**
305          // as childs of the rasterLegendItem element     * Parses a {@link RasterLegendData} object from an JDOM-{@link Element}.
306          Translation label = parseTranslation( labelElement != null ? labelElement : childElement );     * This method works like {@link AMLImport#parseRasterLegendData(org.w3c.dom.Node},
307          rld.put(value, label);     * but for JDOM.
308        }     * @param element {@link Element} to parse
309      }     */
310      public static RasterLegendData parseRasterLegendData(Element element) {
311      return rld;  
312    }      final boolean paintGaps = Boolean.valueOf( element.getAttributeValue("paintGaps") );
313    
314    /**      RasterLegendData rld = new RasterLegendData(paintGaps);
315     * Loads a {@link RasterLegendData} object from an URL.  
316     * @param documentUrl {@link URL} to parse      for ( Element childElement : (List<Element>)element.getChildren() ) {
317     * @see #parseAttributeMetaData(Element)        final String name = childElement.getName();
318     */        // Cancel if it's an attribute
319    public static RasterLegendData loadRasterLegendData(final URL documentUrl) throws Exception {        if ( childElement.getChildren().size() == 0 )
320      Document document = SAX_BUILDER.build(documentUrl);          continue;
321      return parseRasterLegendData( document.getRootElement() );  
322    }        if (name.equals( ELEM_NAME_RASTERLEGEND )) {
323            final String valueAttr = childElement.getAttributeValue("value");
324   /**          if ( valueAttr == null )
325     * Creates an JDOM {@link Element} for the given {@link RasterLegendData}            throw new UnsupportedOperationException("Attribute 'value' missing for definition of <"+ELEM_NAME_RASTERLEGEND+">");
326     * map.          final double value = Double.valueOf(valueAttr);
327     * @param rld raster legend data  
328     */          // first and only item should be the label
329    public static Element createRasterLegendDataElement(final RasterLegendData rld) {          final Element labelElement = childElement.getChild("label");
330      final Element element = new Element( ELEM_NAME_RLD , AMLURI);          // id label element is missing, the translation is searched directly
331      element.setAttribute("paintGaps", rld.isPaintGaps().toString());          // as childs of the rasterLegendItem element
332      for (Double key : rld.getSortedKeys()) {          Translation label = parseTranslation( labelElement != null ? labelElement : childElement );
333        Element item = new Element( ELEM_NAME_RASTERLEGEND, AMLURI);          rld.put(value, label);
334        item.setAttribute("value", key.toString());        }
335        item.addContent( createTranslationElement("label", rld.get(key)) );      }
336        element.addContent(item);  
337      }      return rld;
338      return element;    }
339    }  
340      /**
341    /**     * Loads a {@link RasterLegendData} object from an URL.
342     * Creates {@link RasterLegendData} from a {@link ColorMap}.     * @param documentUrl {@link URL} to parse
343     * @param colorMap  a color map     * @see #parseAttributeMetaData(Element)
344     * @param paintGaps indicated whether gaps are painted between the legend items     */
345     * @param digits    number of digits the grid value classes (and legend) are    public static RasterLegendData loadRasterLegendData(final URL documentUrl) throws Exception {
346     *                  rounded to (null means no round; >= 0 means digits after comma;      Document document = SAX_BUILDER.build(documentUrl);
347     *                  < 0 means digits before comma)    */      return parseRasterLegendData( document.getRootElement() );
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);   /**
351      for (ColorMapEntry cme : colorMap.getColorMapEntries())     * Creates an JDOM {@link Element} for the given {@link RasterLegendData}
352      {     * map.
353        double value = StylingUtil.getQuantityFromColorMapEntry(cme);     * @param rld raster legend data
354        String label = cme.getLabel();     */
355        // if no label is set (e.g. quantitative style),    public static Element createRasterLegendDataElement(final RasterLegendData rld) {
356        // use the value as label      final Element element = new Element( ELEM_NAME_RLD , AMLURI);
357        if ( label == null || label.equals("") )      element.setAttribute("paintGaps", rld.isPaintGaps().toString());
358          if ( digits == null )      for (Double key : rld.getSortedKeys()) {
359            label = String.valueOf(value);        Element item = new Element( ELEM_NAME_RASTERLEGEND, AMLURI);
360          else        item.setAttribute("value", key.toString());
361            label = decFormat.format( LangUtil.round(value, digits) );        item.addContent( createTranslationElement("label", rld.get(key)) );
362        rld.put( value, new Translation("   "+label) );        element.addContent(item);
363      }      }
364      return rld;      return element;
365    }    }
366    
367    /**    /**
368     * Creates {@link RasterLegendData} from the {@link ColorMap} of a style.     * Creates {@link RasterLegendData} from a {@link ColorMap}.
369     * @param style     a raster style (must contain a  {@link RasterSymbolizer})     * @param colorMap  a color map
370     * @param paintGaps indicated whether gaps are painted between the legend items     * @param paintGaps indicated whether gaps are painted between the legend items
371     * @param digits    number of digits the grid value classes (and legend) are     * @param digits    number of digits the grid value classes (and legend) are
372     *                  rounded to (null means no round; >= 0 means digits after comma;     *                  rounded to (null means no round; >= 0 means digits after comma;
373     *                  < 0 means digits before comma)    */     *                  < 0 means digits before comma)    */
374    public static RasterLegendData generateRasterLegendData(Style style, boolean paintGaps, Integer digits) {    public static RasterLegendData generateRasterLegendData(ColorMap colorMap, boolean paintGaps, Integer digits) {
375      ColorMap colorMap = StylingUtil.getColorMapFromStyle(style);      DecimalFormat    decFormat = digits != null ? new DecimalFormat( SwingUtil.getNumberFormatPattern(digits) ) : null;
376      if ( colorMap == null)      RasterLegendData rld       = new RasterLegendData(paintGaps);
377        throw new IllegalArgumentException("Color map can not be determined from style!");      for (ColorMapEntry cme : colorMap.getColorMapEntries())
378      return generateRasterLegendData(colorMap, paintGaps, digits);      {
379    }        double value = StylingUtil.getQuantityFromColorMapEntry(cme);
380          String label = cme.getLabel();
381    /**        // if no label is set (e.g. quantitative style),
382     * Saves a {@link RasterLegendData} to an URL.        // use the value as label
383     * @param rld raster legend data        if ( label == null || label.equals("") )
384     * @param documentUrl {@link URL} to store the XML          if ( digits == null )
385     */            label = String.valueOf(value);
386    public static void saveRasterLegendData(final RasterLegendData rld, final URL documentUrl) throws Exception {          else
387      // Create XML-Document            label = decFormat.format( LangUtil.round(value, digits) );
388      final FileWriter out = new FileWriter( new File(documentUrl.toURI()) );        rld.put( value, new Translation("   "+label) );
389      XML_OUTPUTTER.output(      }
390        createRasterLegendDataElement(rld),      return rld;
391        out    }
392      );  
393      out.flush();    /**
394      out.close();     * Creates {@link RasterLegendData} from the {@link ColorMap} of a style.
395    }     * @param style     a raster style (must contain a  {@link RasterSymbolizer})
396       * @param paintGaps indicated whether gaps are painted between the legend items
397    /**     * @param digits    number of digits the grid value classes (and legend) are
398     * Parses a {@link Translation} object from an JDOM-{@link Element}.     *                  rounded to (null means no round; >= 0 means digits after comma;
399     * This method works like {@link AMLImport#parseTranslation(org.w3c.dom.Node},     *                  < 0 means digits before comma)    */
400     * but for JDOM.    public static RasterLegendData generateRasterLegendData(Style style, boolean paintGaps, Integer digits) {
401     * @param element {@link Element} to parse      ColorMap colorMap = StylingUtil.getColorMapFromStyle(style);
402     */      if ( colorMap == null)
403    public final static Translation parseTranslation(final Element element) {        throw new IllegalArgumentException("Color map can not be determined from style!");
404      Translation trans = new Translation();      return generateRasterLegendData(colorMap, paintGaps, digits);
405      }
406      if (element == null)  
407       return trans;    /**
408       * Saves a {@link RasterLegendData} to an URL.
409      for (final Element translationElement : (List<Element>)element.getChildren()) {     * @param rld raster legend data
410        final String name = translationElement.getName();     * @param documentUrl {@link URL} to store the XML
411        if (name == null)     */
412          continue;    public static void saveRasterLegendData(final RasterLegendData rld, final URL documentUrl) throws Exception {
413        // Create XML-Document
414        // lang attribute      final FileWriter out = new FileWriter( new File(documentUrl.toURI()) );
415        String lang = translationElement.getAttributeValue("lang");      XML_OUTPUTTER.output(
416        // set the default, if no language code is set        createRasterLegendDataElement(rld),
417        if ( lang == null )        out
418          lang = Translation.DEFAULT_KEY;      );
419        out.flush();
420        final String translationText = translationElement.getValue();      out.close();
421        if (translationText == null)    }
422          trans.put(lang, "");  
423        else    /**
424          trans.put(lang, translationText);     * Parses a {@link Translation} object from an JDOM-{@link Element}.
425      }     * This method works like {@link AMLImport#parseTranslation(org.w3c.dom.Node},
426       * but for JDOM.
427      // if no <translation> is given, the value of the node should     * @param element {@link Element} to parse
428      // be used as a default translation     */
429      if (trans.size() == 0)    public final static Translation parseTranslation(final Element element) {
430        trans.put( Translation.DEFAULT_KEY, element.getValue() );      Translation trans = new Translation();
431      //     trans = new Translation( ((List<Element>)element.getChildren()).get(0).getValue() );  
432        if (element == null)
433      return trans;       return trans;
434    }  
435        for (final Element translationElement : (List<Element>)element.getChildren()) {
436    /**        final String name = translationElement.getName();
437     * Creates an JDOM {@link Element} for the given {@link Translation}.        if (name == null)
438     * @param tagname Name of the Element          continue;
439     * @param translation Translation to store in the Element  
440     */        // lang attribute
441    public final static Element createTranslationElement(String tagname, Translation translation) {        String lang = translationElement.getAttributeValue("lang");
442      Element element = new Element(tagname, AMLURI);        // set the default, if no language code is set
443      if ( translation == null )        if ( lang == null )
444        throw new UnsupportedOperationException("Translation element can not be created from null!");          lang = Translation.DEFAULT_KEY;
445    
446      // If only a default translation is set, the <translation lang="..">..</tranlation>        final String translationText = translationElement.getValue();
447      // part is not used        if (translationText == null)
448      if (translation.keySet().size() == 1 && translation.get(Translation.DEFAULT_KEY) != null) {          trans.put(lang, "");
449        element.addContent( translation.get(Translation.DEFAULT_KEY) );        else
450        return element;          trans.put(lang, translationText);
451      }      }
452    
453      // add a <translation lang="..">..</tranlation> part to the element for      // if no <translation> is given, the value of the node should
454      // all languages      // be used as a default translation
455      for (String lang : translation.keySet()) {      if (trans.size() == 0)
456        Element translationElement = new Element( ELEM_NAME_TRANSLATION , AMLURI);        trans.put( Translation.DEFAULT_KEY, element.getValue() );
457        translationElement.setAttribute("lang", lang);      //     trans = new Translation( ((List<Element>)element.getChildren()).get(0).getValue() );
458        String translationString = translation.get(lang);  
459        if (translationString == null)      return trans;
460         translationString = "";    }
461        translationElement.addContent( translationString );  
462        element.addContent(translationElement);    /**
463      }     * Creates an JDOM {@link Element} for the given {@link Translation}.
464       * @param tagname Name of the Element
465      return element;     * @param translation Translation to store in the Element
466    }     */
467      public final static Element createTranslationElement(String tagname, Translation translation) {
468        Element element = new Element(tagname, AMLURI);
469    /**      if ( translation == null )
470     * Sets a style to {@link StyledMapInterface}.        throw new UnsupportedOperationException("Translation element can not be created from null!");
471     * @param styledObject a styled object  
472     * @param style a Style      // If only a default translation is set, the <translation lang="..">..</tranlation>
473     */      // part is not used
474    public static void setStyledMapStyle(StyledMapInterface styledObject, StyledMapStyle<?> style) {      if (translation.keySet().size() == 1 && translation.get(Translation.DEFAULT_KEY) != null) {
475      // set SLD style        element.addContent( translation.get(Translation.DEFAULT_KEY) );
476      styledObject.setStyle( style.getGeoObjectStyle() );        return element;
477      // set meta data      }
478      if ( styledObject        instanceof StyledGridCoverageInterface &&  
479           (style.getMetaData() instanceof RasterLegendData || style.getMetaData() == null) ) {      // add a <translation lang="..">..</tranlation> part to the element for
480        RasterLegendData sourceRld = (RasterLegendData)style.getMetaData();      // all languages
481        RasterLegendData destRld = ((StyledGridCoverageInterface)styledObject).getLegendMetaData();      for (String lang : translation.keySet()) {
482        if ( destRld != null && sourceRld != null ) {        Element translationElement = new Element( ELEM_NAME_TRANSLATION , AMLURI);
483          destRld.setPaintGaps(sourceRld.isPaintGaps());        translationElement.setAttribute("lang", lang);
484          destRld.clear();        String translationString = translation.get(lang);
485          destRld.putAll( sourceRld );        if (translationString == null)
486        }         translationString = "";
487        return;        translationElement.addContent( translationString );
488      }        element.addContent(translationElement);
489      if ( styledObject        instanceof StyledFeatureCollectionInterface &&      }
490           (style.getMetaData() instanceof Map || style.getMetaData() == null) ) {  
491        Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>)style.getMetaData();      return element;
492        Map<Integer, AttributeMetaData> destAmd   = ((StyledFeatureCollectionInterface)styledObject).getAttributeMetaDataMap();    }
493        if ( destAmd != null && sourceAmd != null ) {  
494          destAmd.clear();  
495          destAmd.putAll( sourceAmd );    /**
496        }     * Sets a style to {@link StyledLayerInterface}.
497        return;     * @param styledObject a styled object
498      }     * @param style a Style
499       */
500      throw new UnsupportedOperationException("Style is not compatible to object: " +    public static void setStyledLayerStyle(StyledLayerInterface styledObject, StyledLayerStyle<?> style) {
501                                              (style.getMetaData() == null ? null : style.getMetaData().getClass().getSimpleName()) +      // set SLD style
502                                              " <-> " +      styledObject.setStyle( style.getGeoObjectStyle() );
503                                              (styledObject == null ? null : styledObject.getClass().getSimpleName()));      // set meta data
504    }      if ( styledObject        instanceof StyledGridCoverageInterface &&
505             (style.getMetaData() instanceof RasterLegendData || style.getMetaData() == null) ) {
506    /**        RasterLegendData sourceRld = (RasterLegendData)style.getMetaData();
507     * Returns the style a {@link StyledMapInterface} as a {@link StyledMapStyle}.        RasterLegendData destRld = ((StyledGridCoverageInterface)styledObject).getLegendMetaData();
508     * @param styledObject a styled object        if ( destRld != null && sourceRld != null ) {
509     * @return {@code StyledMapStyle<RasterLegendData>} for {@link StyledGridCoverageInterface}          destRld.setPaintGaps(sourceRld.isPaintGaps());
510     *         or {@code StyledMapStyle<Map<Integer,AttributeMetaData>>} for          destRld.clear();
511     *         {@link StyledFeatureCollectionInterface}          destRld.putAll( sourceRld );
512     */        }
513    public static StyledMapStyle<?> getStyledMapStyle(StyledMapInterface styledObject) {        return;
514      if ( styledObject instanceof StyledGridCoverageInterface )      }
515        return getStyledMapStyle( (StyledGridCoverageInterface)styledObject );      if ( styledObject        instanceof StyledFeatureCollectionInterface &&
516      if ( styledObject instanceof StyledFeatureCollectionInterface )           (style.getMetaData() instanceof Map || style.getMetaData() == null) ) {
517        return getStyledMapStyle( (StyledFeatureCollectionInterface)styledObject );        Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>)style.getMetaData();
518      throw new UnsupportedOperationException("Unknown type of StyledMapInterface: "+(styledObject == null ? null : styledObject.getClass().getSimpleName()));        Map<Integer, AttributeMetaData> destAmd   = ((StyledFeatureCollectionInterface)styledObject).getAttributeMetaDataMap();
519    }        if ( destAmd != null && sourceAmd != null ) {
520            destAmd.clear();
521    /**          destAmd.putAll( sourceAmd );
522     * Returns the style and raster meta data of a {@link StyledGridCoverageInterface}        }
523     * as a {@link StyledMapStyle}.        return;
524     * @param styledGC a styled grid coverage      }
525     */  
526    public static StyledMapStyle<RasterLegendData> getStyledMapStyle(StyledGridCoverageInterface styledGC) {      throw new UnsupportedOperationException("Style is not compatible to object: " +
527      return new StyledMapStyle<RasterLegendData>(                                              (style.getMetaData() == null ? null : style.getMetaData().getClass().getSimpleName()) +
528        styledGC.getStyle(),                                              " <-> " +
529        styledGC.getLegendMetaData()                                              (styledObject == null ? null : styledObject.getClass().getSimpleName()));
530      );    }
531    }  
532      /**
533    /**     * Returns the style a {@link StyledLayerInterface} as a {@link StyledLayerStyle}.
534     * Returns the style and attribute meta data of a {@link StyledFeatureCollectionInterface}     * @param styledObject a styled object
535     * as a {@link StyledMapStyle}.     * @return {@code StyledLayerStyle<RasterLegendData>} for {@link StyledGridCoverageInterface}
536     * @param styledFC a styled feature collection     *         or {@code StyledLayerStyle<Map<Integer,AttributeMetaData>>} for
537     */     *         {@link StyledFeatureCollectionInterface}
538    public static StyledMapStyle<Map<Integer,AttributeMetaData>> getStyledMapStyle(StyledFeatureCollectionInterface styledFC) {     */
539      return new StyledMapStyle<Map<Integer,AttributeMetaData>>(    public static StyledLayerStyle<?> getStyledLayerStyle(StyledLayerInterface styledObject) {
540        styledFC.getStyle(),      if ( styledObject instanceof StyledGridCoverageInterface )
541        styledFC.getAttributeMetaDataMap()        return getStyledLayerStyle( (StyledGridCoverageInterface)styledObject );
542      );      if ( styledObject instanceof StyledFeatureCollectionInterface )
543    }        return getStyledLayerStyle( (StyledFeatureCollectionInterface)styledObject );
544        throw new UnsupportedOperationException("Unknown type of StyledLayerInterface: "+(styledObject == null ? null : styledObject.getClass().getSimpleName()));
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    /**
548     * raster legend-data file is tolerated.     * Returns the style and raster meta data of a {@link StyledGridCoverageInterface}
549     * @param geoObjectURL URL of the (already read) raster object     * as a {@link StyledLayerStyle}.
550     * @param sldExt file extention for the SLD file     * @param styledGC a styled grid coverage
551     * @param rldExt file extention for the raster legend-data file     */
552     * @return {@code null} in case of any error    public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle(StyledGridCoverageInterface styledGC) {
553     */      return new StyledLayerStyle<RasterLegendData>(
554    public static StyledMapStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL, String sldExt, String rldExt) {        styledGC.getStyle(),
555      RasterLegendData metaData = null;        styledGC.getLegendMetaData()
556      Style sldStyle = null;      );
557      try {    }
558        Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt));  
559        // SLD must be present    /**
560        if ( styles == null || styles.length == 0 )     * Returns the style and attribute meta data of a {@link StyledFeatureCollectionInterface}
561          return null;     * as a {@link StyledLayerStyle}.
562        sldStyle = styles[0];     * @param styledFC a styled feature collection
563      }     */
564      catch (Exception err) {    public static StyledLayerStyle<Map<Integer,AttributeMetaData>> getStyledLayerStyle(StyledFeatureCollectionInterface styledFC) {
565        // SLD must be present      return new StyledLayerStyle<Map<Integer,AttributeMetaData>>(
566        LangUtil.logDebugError(LOGGER,err);        styledFC.getStyle(),
567        return null;        styledFC.getAttributeMetaDataMap()
568      }      );
569      }
570      try {  
571        metaData = StyledMapUtil.loadRasterLegendData( IOUtil.changeUrlExt(geoObjectURL,rldExt) );    /**
572      } catch (FileNotFoundException err) {     * Loads a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData Raster-LegendData}
573        // ignore missing raster legend data     * for a given geo-object (raster) source. The SLD file must be present. A missing
574      } catch (Exception err) {     * raster legend-data file is tolerated.
575        // any other error during legend data creation leads to error     * @param geoObjectURL URL of the (already read) raster object
576        LangUtil.logDebugError(LOGGER,err);     * @param sldExt file extention for the SLD file
577        return null;     * @param rldExt file extention for the raster legend-data file
578      }     * @return {@code null} in case of any error
579      return new StyledMapStyle<RasterLegendData>(sldStyle, metaData);     */
580    }    public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL, String sldExt, String rldExt) {
581        RasterLegendData metaData = null;
582    /**      Style sldStyle = null;
583     * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and      try {
584     * {@linkplain RasterLegendData Raster-LegendData} from a {@code .rld} file        Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt));
585     * for a given geo-object (raster) source. The SLD file must be present. A missing        // SLD must be present
586     * raster legend-data file is tolerated.        if ( styles == null || styles.length == 0 )
587     * @param geoObjectURL URL of the (already read) raster object          return null;
588     * @param sldExt file extention for the SLD file        sldStyle = styles[0];
589     * @param rldExt file extention for the raster legend-data file      }
590     * @return {@code null} in case of any error      catch (Exception err) {
591     */        // SLD must be present
592    public static StyledMapStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL) {        LangUtil.logDebugError(LOGGER,err);
593      return loadStyledRasterStyle(geoObjectURL, "sld", "rld");        return null;
594    }      }
595    
596    /**      try {
597     * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData AttributeMetaData-Map}        metaData = StyledLayerUtil.loadRasterLegendData( IOUtil.changeUrlExt(geoObjectURL,rldExt) );
598     * for a given geo-object (feature) source. The SLD file must be present. A missing      } catch (FileNotFoundException err) {
599     * attribute meta-data file is tolerated.        // ignore missing raster legend data
600     * @param geoObjectURL URL of the (already read) feature object      } catch (Exception err) {
601     * @param sldExt file extention for the SLD file        // any other error during legend data creation leads to error
602     * @param rldExt file extention for the raster legend-data file        LangUtil.logDebugError(LOGGER,err);
603     * @return {@code null} in case of any error        return null;
604     */      }
605    public static StyledMapStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL, String sldExt, String rldExt) {      return new StyledLayerStyle<RasterLegendData>(sldStyle, metaData);
606      Map<Integer,AttributeMetaData> metaData = null;    }
607      Style                          sldStyle = null;  
608      try {    /**
609        Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt));     * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
610        // SLD must be present     * {@linkplain RasterLegendData Raster-LegendData} from a {@code .rld} file
611        if ( styles == null || styles.length == 0 )     * for a given geo-object (raster) source. The SLD file must be present. A missing
612          return null;     * raster legend-data file is tolerated.
613        sldStyle = styles[0];     * @param geoObjectURL URL of the (already read) raster object
614      } catch (Exception err) {     * @param sldExt file extention for the SLD file
615        // SLD must be present     * @param rldExt file extention for the raster legend-data file
616        LangUtil.logDebugError(LOGGER,err);     * @return {@code null} in case of any error
617        return null;     */
618      }    public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL) {
619        return loadStyledRasterStyle(geoObjectURL, "sld", "rld");
620      try {    }
621        metaData = StyledMapUtil.loadAttributeMetaDataMap( IOUtil.changeUrlExt(geoObjectURL,rldExt) );  
622      } catch (FileNotFoundException err) {    /**
623        // ignore missing attribute meta data     * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData AttributeMetaData-Map}
624      } catch (Exception err) {     * for a given geo-object (feature) source. The SLD file must be present. A missing
625        // any other error during meta data creation leads to error     * attribute meta-data file is tolerated.
626        LangUtil.logDebugError(LOGGER,err);     * @param geoObjectURL URL of the (already read) feature object
627        return null;     * @param sldExt file extention for the SLD file
628      }     * @param rldExt file extention for the raster legend-data file
629       * @return {@code null} in case of any error
630      return new StyledMapStyle<Map<Integer,AttributeMetaData>>(sldStyle, metaData);     */
631    }    public static StyledLayerStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL, String sldExt, String rldExt) {
632        Map<Integer,AttributeMetaData> metaData = null;
633    /**      Style                          sldStyle = null;
634     * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and      try {
635     * {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd} file        Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt));
636     * for a given geo-object (feature) source. The SLD file must be present. A missing        // SLD must be present
637     * attribute meta-data file is tolerated.        if ( styles == null || styles.length == 0 )
638     * @param geoObjectURL URL of the (already read) feature object          return null;
639     * @param sldExt file extention for the SLD file        sldStyle = styles[0];
640     * @param rldExt file extention for the raster legend-data file      } catch (Exception err) {
641     * @return {@code null} in case of any error        // SLD must be present
642     */        LangUtil.logDebugError(LOGGER,err);
643    public static StyledMapStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL) {        return null;
644      return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");      }
645    }  
646        try {
647    /**        metaData = StyledLayerUtil.loadAttributeMetaDataMap( IOUtil.changeUrlExt(geoObjectURL,rldExt) );
648     * Stores a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData Raster-LegendData}      } catch (FileNotFoundException err) {
649     * for a given geo-object (raster) source.        // ignore missing attribute meta data
650     * @param style  style to save      } catch (Exception err) {
651     * @param geoObjectURL URL of the raster object        // any other error during meta data creation leads to error
652     * @param sldExt file extention for the SLD file        LangUtil.logDebugError(LOGGER,err);
653     * @param mdExt file extention for the meta-data file        return null;
654     */      }
655    public static <T> void saveStyledMapStyle(StyledMapStyle<T> style, URL geoObjectURL, String sldExt, String mdExt) throws Exception {  
656      // Store the SLD      return new StyledLayerStyle<Map<Integer,AttributeMetaData>>(sldStyle, metaData);
657      Style sldStyle = style.getGeoObjectStyle();    }
658      if ( sldStyle != null ) {  
659        StylingUtil.saveStyleToSLD(    /**
660           sldStyle,     * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
661           IOUtil.changeFileExt(     * {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd} file
662                new File(geoObjectURL.toURI()),     * for a given geo-object (feature) source. The SLD file must be present. A missing
663                sldExt     * attribute meta-data file is tolerated.
664           )     * @param geoObjectURL URL of the (already read) feature object
665        );     * @param sldExt file extention for the SLD file
666      }     * @param rldExt file extention for the raster legend-data file
667       * @return {@code null} in case of any error
668      // Store the meta data     */
669      T metaData = style.getMetaData();    public static StyledLayerStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL) {
670      if ( metaData != null ) {      return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");
671        if ( metaData instanceof RasterLegendData ) {    }
672          saveRasterLegendData(  
673              (RasterLegendData)metaData,    /**
674              IOUtil.changeUrlExt(geoObjectURL,mdExt)     * Stores a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData Raster-LegendData}
675          );     * for a given geo-object (raster) source.
676  //      } else if ( metaData instanceof Map<Integer,AttributeMetaData> ) { // LEIDER NICHT KOMPILIERBAR!!     * @param style  style to save
677        } else if ( metaData instanceof Map ) {     * @param geoObjectURL URL of the raster object
678          saveAttributeMetaDataMap(     * @param sldExt file extention for the SLD file
679              (Map<Integer,AttributeMetaData>)metaData,     * @param mdExt file extention for the meta-data file
680              IOUtil.changeUrlExt(geoObjectURL,mdExt)     */
681          );    public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style, URL geoObjectURL, String sldExt, String mdExt) throws Exception {
682        } else      // Store the SLD
683          throw new UnsupportedOperationException("Export for meta data not yet supported: "+metaData.getClass().getSimpleName());      Style sldStyle = style.getGeoObjectStyle();
684      }      if ( sldStyle != null ) {
685    }        StylingUtil.saveStyleToSLD(
686             sldStyle,
687    /**           IOUtil.changeFileExt(
688     * Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and                new File(geoObjectURL.toURI()),
689     * the meta data ({@link RasterLegendData} or {@link AttributeMetaData})                sldExt
690     * to a {@code .rld} or {@code .amd} file.           )
691     * for a given geo-object source.        );
692     * @param style  style to save      }
693     * @param geoObjectURL URL of the (already read) raster object  
694     */      // Store the meta data
695    public static void saveStyledMapStyle(StyledMapStyle<?> style, URL geoObjectURL) throws Exception {      T metaData = style.getMetaData();
696      if ( style.getMetaData() instanceof RasterLegendData )      if ( metaData != null ) {
697        saveStyledMapStyle(style,geoObjectURL, "sld", "rld");        if ( metaData instanceof RasterLegendData ) {
698      else          saveRasterLegendData(
699        saveStyledMapStyle(style,geoObjectURL, "sld", "amd");              (RasterLegendData)metaData,
700    }              IOUtil.changeUrlExt(geoObjectURL,mdExt)
701            );
702  }  //      } else if ( metaData instanceof Map<Integer,AttributeMetaData> ) { // LEIDER NICHT KOMPILIERBAR!!
703          } else if ( metaData instanceof Map ) {
704            saveAttributeMetaDataMap(
705                (Map<Integer,AttributeMetaData>)metaData,
706                IOUtil.changeUrlExt(geoObjectURL,mdExt)
707            );
708          } else
709            throw new UnsupportedOperationException("Export for meta data not yet supported: "+metaData.getClass().getSimpleName());
710        }
711      }
712    
713      /**
714       * Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and
715       * the meta data ({@link RasterLegendData} or {@link AttributeMetaData})
716       * to a {@code .rld} or {@code .amd} file.
717       * for a given geo-object source.
718       * @param style  style to save
719       * @param geoObjectURL URL of the (already read) raster object
720       */
721      public static void saveStyledLayerStyle(StyledLayerStyle<?> style, URL geoObjectURL) throws Exception {
722        if ( style.getMetaData() instanceof RasterLegendData )
723          saveStyledLayerStyle(style,geoObjectURL, "sld", "rld");
724        else
725          saveStyledLayerStyle(style,geoObjectURL, "sld", "amd");
726      }
727    
728    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26