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

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26