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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26