/[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 464 by alfonx, Tue Oct 13 13:22:31 2009 UTC revision 534 by alfonx, Fri Nov 20 10:28:01 2009 UTC
# Line 31  package skrueger.geotools; Line 31  package skrueger.geotools;
31    
32  import java.awt.Color;  import java.awt.Color;
33  import java.awt.Dimension;  import java.awt.Dimension;
 import java.awt.Graphics;  
34  import java.awt.Graphics2D;  import java.awt.Graphics2D;
35  import java.awt.Rectangle;  import java.awt.Rectangle;
36  import java.awt.geom.AffineTransform;  import java.awt.geom.AffineTransform;
# Line 48  import java.util.List; Line 47  import java.util.List;
47  import java.util.Map;  import java.util.Map;
48    
49  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
 import javax.swing.Box;  
 import javax.swing.BoxLayout;  
50  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
51    import javax.swing.JComponent;
52  import javax.swing.JLabel;  import javax.swing.JLabel;
53    
54    import net.miginfocom.swing.MigLayout;
55    
56  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
57  import org.geotools.coverage.grid.GeneralGridEnvelope;  import org.geotools.coverage.grid.GeneralGridEnvelope;
58  import org.geotools.coverage.grid.GridCoverage2D;  import org.geotools.coverage.grid.GridCoverage2D;
# Line 86  import schmitzm.geotools.feature.Feature Line 86  import schmitzm.geotools.feature.Feature
86  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
87  import schmitzm.io.IOUtil;  import schmitzm.io.IOUtil;
88  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
89    import schmitzm.swing.JPanel;
90  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
91  import skrueger.AttributeMetadata;  import skrueger.AttributeMetadata;
92  import skrueger.RasterLegendData;  import skrueger.RasterLegendData;
# Line 131  public class StyledLayerUtil { Line 132  public class StyledLayerUtil {
132           *                if {@code null} is given as object or an error occurs           *                if {@code null} is given as object or an error occurs
133           *                during layer creation           *                during layer creation
134           */           */
135          public static MapLayer createMapLayer(Object object) throws Exception {          public static MapLayer createMapLayer(final Object object) throws Exception {
136                  return createMapLayer(object, null);                  return createMapLayer(object, null);
137          }          }
138    
# Line 150  public class StyledLayerUtil { Line 151  public class StyledLayerUtil {
151           *                if {@code null} is given as object or an error occurs           *                if {@code null} is given as object or an error occurs
152           *                during layer creation           *                during layer creation
153           */           */
154          public static MapLayer createMapLayer(Object object, Style forcedStyle)          public static MapLayer createMapLayer(Object object, final Style forcedStyle)
155                          throws Exception {                          throws Exception {
156                  MapLayer layer = null;                  MapLayer layer = null;
157                  Style style = null;                  Style style = null;
# Line 191  public class StyledLayerUtil { Line 192  public class StyledLayerUtil {
192           *                if {@code null} is given as object or an error occurs           *                if {@code null} is given as object or an error occurs
193           *                during creation           *                during creation
194           */           */
195          public static StyledLayerInterface<?> createStyledLayer(Object object,          public static StyledLayerInterface<?> createStyledLayer(
196                          String title) {                          final Object object, final String title) {
197                  return createStyledLayer(object, title, null);                  return createStyledLayer(object, title, null);
198          }          }
199    
# Line 211  public class StyledLayerUtil { Line 212  public class StyledLayerUtil {
212           *                if {@code null} is given as object or an error occurs           *                if {@code null} is given as object or an error occurs
213           *                during creation           *                during creation
214           */           */
215          public static StyledLayerInterface<?> createStyledLayer(Object object,          public static StyledLayerInterface<?> createStyledLayer(
216                          String title, StyledLayerStyle style) {                          final Object object, final String title,
217                            final StyledLayerStyle style) {
218                  StyledLayerInterface<?> styledLayer = null;                  StyledLayerInterface<?> styledLayer = null;
219    
220                  String id = (title != null) ? title : "defaultID";                  final String id = (title != null) ? title : "defaultID";
221    
222                  if (object instanceof GridCoverage2D)                  if (object instanceof GridCoverage2D)
223                          styledLayer = new StyledGridCoverage((GridCoverage2D) object, id,                          styledLayer = new StyledGridCoverage((GridCoverage2D) object, id,
# Line 243  public class StyledLayerUtil { Line 245  public class StyledLayerUtil {
245           * @param visible           * @param visible
246           *            indicated whether the visible or invisible entries are           *            indicated whether the visible or invisible entries are
247           *            returned           *            returned
248             *            
249             * TODO replace with {@link AttributeMetadataMap#sortedValuesVisibleOnly()}
250           */           */
251          public static AttributeMetadataMap getVisibleAttributeMetaData(          public static AttributeMetadataMap getVisibleAttributeMetaData(
252                          AttributeMetadataMap amdMap, boolean visible) {                          final AttributeMetadataMap amdMap, final boolean visible) {
253                    
254                  AttributeMetadataMap filteredMap = new AttributeMetadataMap();                  final AttributeMetadataMap filteredMap = new AttributeMetadataMap(amdMap.getLanguages());
255                  for (AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadata amd : amdMap.values())
256                          if (amd.isVisible() == visible)                          if (amd.isVisible() == visible)
257                                  filteredMap.put(amd.getName(), amd);                                  filteredMap.put(amd.getName(), amd);
258    
# Line 260  public class StyledLayerUtil { Line 264  public class StyledLayerUtil {
264           * This method works like {@link           * This method works like {@link
265           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.
266           *           *
267             * TODO 20.11.2009, SK: There are some new attribute weight, functiona,
268             * functionX and nodata in AttributeMetaData that should be parsed/exported
269             * too. but this method is only used by ISDSS, which is not supporting that
270             * stuff anyways.
271             *
272           * @param element           * @param element
273           *            {@link Element} to parse           *            {@link Element} to parse
274           */           */
275          public static AttributeMetadata parseAttributeMetaData(final Element element) {          public static AttributeMetadata parseAttributeMetaData(final Element element) {
276                  final String namespace = String.valueOf(element.getAttributeValue("namespace"));                  final String namespace = String.valueOf(element
277                  final String localname = String.valueOf(element.getAttributeValue("localname"));                                  .getAttributeValue("namespace"));
278                  final Name aName= new NameImpl(namespace,localname);                  final String localname = String.valueOf(element
279                                    .getAttributeValue("localname"));
280                    final Name aName = new NameImpl(namespace, localname);
281                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
282                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
283                  final String unit = element.getAttributeValue("unit");                  final String unit = element.getAttributeValue("unit");
# Line 294  public class StyledLayerUtil { Line 305  public class StyledLayerUtil {
305           */           */
306          public static AttributeMetadataMap parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
307                          final Element element) {                          final Element element) {
308                  AttributeMetadataMap metaData = new AttributeMetadataMap();                  final AttributeMetadataMap metaData = new AttributeMetadataMap();
309                  List<Element> attributesElements = element                  final List<Element> attributesElements = element
310                                  .getChildren(ELEM_NAME_ATTRIBUTE);                                  .getChildren(ELEM_NAME_ATTRIBUTE);
311                  for (Element attibuteElement : attributesElements) {                  for (final Element attibuteElement : attributesElements) {
312                          AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);                          final AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);
313                          metaData.put(attrMetaData.getName(), attrMetaData);                          metaData.put(attrMetaData.getName(), attrMetaData);
314                  }                  }
315                  return metaData;                  return metaData;
# Line 313  public class StyledLayerUtil { Line 324  public class StyledLayerUtil {
324           */           */
325          public static AttributeMetadataMap loadAttributeMetaDataMap(          public static AttributeMetadataMap loadAttributeMetaDataMap(
326                          final URL documentUrl) throws Exception {                          final URL documentUrl) throws Exception {
327                  Document document = SAX_BUILDER.build(documentUrl);                  final Document document = SAX_BUILDER.build(documentUrl);
328                  return parseAttributeMetaDataMap(document.getRootElement());                  return parseAttributeMetaDataMap(document.getRootElement());
329          }          }
330    
# Line 327  public class StyledLayerUtil { Line 338  public class StyledLayerUtil {
338          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
339                          final AttributeMetadata amd) {                          final AttributeMetadata amd) {
340                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);
341                  element.setAttribute("namespace", String.valueOf(amd.getName().getNamespaceURI()));                  element.setAttribute("namespace", String.valueOf(amd.getName()
342                                    .getNamespaceURI()));
343                  element.setAttribute("localname", String.valueOf(amd.getLocalName()));                  element.setAttribute("localname", String.valueOf(amd.getLocalName()));
344                  element.setAttribute("visible", String.valueOf(amd.isVisible()));                  element.setAttribute("visible", String.valueOf(amd.isVisible()));
345                  element.setAttribute("unit", amd.getUnit());                  element.setAttribute("unit", amd.getUnit());
# Line 348  public class StyledLayerUtil { Line 360  public class StyledLayerUtil {
360          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
361                          final AttributeMetadataMap amdMap) {                          final AttributeMetadataMap amdMap) {
362                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
363                  for (AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadata amd : amdMap.values())
364                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
365                  return element;                  return element;
366          }          }
# Line 379  public class StyledLayerUtil { Line 391  public class StyledLayerUtil {
391           * @param element           * @param element
392           *            {@link Element} to parse           *            {@link Element} to parse
393           */           */
394          public static RasterLegendData parseRasterLegendData(Element element) {          public static RasterLegendData parseRasterLegendData(final Element element) {
395    
396                  final boolean paintGaps = Boolean.valueOf(element                  final boolean paintGaps = Boolean.valueOf(element
397                                  .getAttributeValue("paintGaps"));                                  .getAttributeValue("paintGaps"));
398    
399                  RasterLegendData rld = new RasterLegendData(paintGaps);                  final RasterLegendData rld = new RasterLegendData(paintGaps);
400    
401                  for (Element childElement : (List<Element>) element.getChildren()) {                  for (final Element childElement : (List<Element>) element.getChildren()) {
402                          final String name = childElement.getName();                          final String name = childElement.getName();
403                          // Cancel if it's an attribute                          // Cancel if it's an attribute
404                          if (childElement.getChildren().size() == 0)                          if (childElement.getChildren().size() == 0)
# Line 406  public class StyledLayerUtil { Line 418  public class StyledLayerUtil {
418                                  // id label element is missing, the translation is searched                                  // id label element is missing, the translation is searched
419                                  // directly                                  // directly
420                                  // as childs of the rasterLegendItem element                                  // as childs of the rasterLegendItem element
421                                  Translation label = parseTranslation(labelElement != null ? labelElement                                  final Translation label = parseTranslation(labelElement != null ? labelElement
422                                                  : childElement);                                                  : childElement);
423                                  rld.put(value, label);                                  rld.put(value, label);
424                          }                          }
# Line 424  public class StyledLayerUtil { Line 436  public class StyledLayerUtil {
436           */           */
437          public static RasterLegendData loadRasterLegendData(final URL documentUrl)          public static RasterLegendData loadRasterLegendData(final URL documentUrl)
438                          throws Exception {                          throws Exception {
439                  Document document = SAX_BUILDER.build(documentUrl);                  final Document document = SAX_BUILDER.build(documentUrl);
440                  return parseRasterLegendData(document.getRootElement());                  return parseRasterLegendData(document.getRootElement());
441          }          }
442    
# Line 439  public class StyledLayerUtil { Line 451  public class StyledLayerUtil {
451                          final RasterLegendData rld) {                          final RasterLegendData rld) {
452                  final Element element = new Element(ELEM_NAME_RLD, AMLURI);                  final Element element = new Element(ELEM_NAME_RLD, AMLURI);
453                  element.setAttribute("paintGaps", rld.isPaintGaps().toString());                  element.setAttribute("paintGaps", rld.isPaintGaps().toString());
454                  for (Double key : rld.getSortedKeys()) {                  for (final Double key : rld.getSortedKeys()) {
455                          Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI);                          final Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI);
456                          item.setAttribute("value", key.toString());                          item.setAttribute("value", key.toString());
457                          item.addContent(createTranslationElement("label", rld.get(key)));                          item.addContent(createTranslationElement("label", rld.get(key)));
458                          element.addContent(item);                          element.addContent(item);
# Line 460  public class StyledLayerUtil { Line 472  public class StyledLayerUtil {
472           *            rounded to (null means no round; >= 0 means digits after           *            rounded to (null means no round; >= 0 means digits after
473           *            comma; < 0 means digits before comma)           *            comma; < 0 means digits before comma)
474           */           */
475          public static RasterLegendData generateRasterLegendData(ColorMap colorMap,          public static RasterLegendData generateRasterLegendData(
476                          boolean paintGaps, Integer digits) {                          final ColorMap colorMap, final boolean paintGaps,
477                  DecimalFormat decFormat = digits != null ? new DecimalFormat(SwingUtil                          final Integer digits) {
478                                  .getNumberFormatPattern(digits)) : null;                  final DecimalFormat decFormat = digits != null ? new DecimalFormat(
479                  RasterLegendData rld = new RasterLegendData(paintGaps);                                  SwingUtil.getNumberFormatPattern(digits)) : null;
480                  for (ColorMapEntry cme : colorMap.getColorMapEntries()) {                  final RasterLegendData rld = new RasterLegendData(paintGaps);
481                          double value = StylingUtil.getQuantityFromColorMapEntry(cme);                  for (final ColorMapEntry cme : colorMap.getColorMapEntries()) {
482                            final double value = StylingUtil.getQuantityFromColorMapEntry(cme);
483                          String label = cme.getLabel();                          String label = cme.getLabel();
484                          // if no label is set (e.g. quantitative style),                          // if no label is set (e.g. quantitative style),
485                          // use the value as label                          // use the value as label
# Line 492  public class StyledLayerUtil { Line 505  public class StyledLayerUtil {
505           *            rounded to (null means no round; >= 0 means digits after           *            rounded to (null means no round; >= 0 means digits after
506           *            comma; < 0 means digits before comma)           *            comma; < 0 means digits before comma)
507           */           */
508          public static RasterLegendData generateRasterLegendData(Style style,          public static RasterLegendData generateRasterLegendData(final Style style,
509                          boolean paintGaps, Integer digits) {                          final boolean paintGaps, final Integer digits) {
510                  ColorMap colorMap = StylingUtil.getColorMapFromStyle(style);                  final ColorMap colorMap = StylingUtil.getColorMapFromStyle(style);
511                  if (colorMap == null)                  if (colorMap == null)
512                          throw new IllegalArgumentException(                          throw new IllegalArgumentException(
513                                          "Color map can not be determined from style!");                                          "Color map can not be determined from style!");
# Line 527  public class StyledLayerUtil { Line 540  public class StyledLayerUtil {
540           *            {@link Element} to parse           *            {@link Element} to parse
541           */           */
542          public final static Translation parseTranslation(final Element element) {          public final static Translation parseTranslation(final Element element) {
543                  Translation trans = new Translation();                  final Translation trans = new Translation();
544    
545                  if (element == null)                  if (element == null)
546                          return trans;                          return trans;
# Line 569  public class StyledLayerUtil { Line 582  public class StyledLayerUtil {
582           * @param translation           * @param translation
583           *            Translation to store in the Element           *            Translation to store in the Element
584           */           */
585          public final static Element createTranslationElement(String tagname,          public final static Element createTranslationElement(final String tagname,
586                          Translation translation) {                          final Translation translation) {
587                  Element element = new Element(tagname, AMLURI);                  final Element element = new Element(tagname, AMLURI);
588                  if (translation == null)                  if (translation == null)
589                          throw new UnsupportedOperationException(                          throw new UnsupportedOperationException(
590                                          "Translation element can not be created from null!");                                          "Translation element can not be created from null!");
# Line 587  public class StyledLayerUtil { Line 600  public class StyledLayerUtil {
600    
601                  // add a <translation lang="..">..</tranlation> part to the element for                  // add a <translation lang="..">..</tranlation> part to the element for
602                  // all languages                  // all languages
603                  for (String lang : translation.keySet()) {                  for (final String lang : translation.keySet()) {
604                          Element translationElement = new Element(ELEM_NAME_TRANSLATION,                          final Element translationElement = new Element(
605                                          AMLURI);                                          ELEM_NAME_TRANSLATION, AMLURI);
606                          translationElement.setAttribute("lang", lang);                          translationElement.setAttribute("lang", lang);
607                          String translationString = translation.get(lang);                          String translationString = translation.get(lang);
608                          if (translationString == null)                          if (translationString == null)
# Line 609  public class StyledLayerUtil { Line 622  public class StyledLayerUtil {
622           * @param style           * @param style
623           *            a Style           *            a Style
624           */           */
625          public static void setStyledLayerStyle(StyledLayerInterface styledObject,          public static void setStyledLayerStyle(
626                          StyledLayerStyle<?> style) {                          final StyledLayerInterface styledObject,
627                            final StyledLayerStyle<?> style) {
628                  // set SLD style                  // set SLD style
629                  styledObject.setStyle(style.getGeoObjectStyle());                  styledObject.setStyle(style.getGeoObjectStyle());
630                  // set meta data                  // set meta data
631                  if (styledObject instanceof StyledGridCoverageInterface                  if (styledObject instanceof StyledGridCoverageInterface
632                                  && (style.getMetaData() instanceof RasterLegendData || style                                  && (style.getMetaData() instanceof RasterLegendData || style
633                                                  .getMetaData() == null)) {                                                  .getMetaData() == null)) {
634                          RasterLegendData sourceRld = (RasterLegendData) style.getMetaData();                          final RasterLegendData sourceRld = (RasterLegendData) style
635                          RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject)                                          .getMetaData();
636                            final RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject)
637                                          .getLegendMetaData();                                          .getLegendMetaData();
638                          if (destRld != null && sourceRld != null) {                          if (destRld != null && sourceRld != null) {
639                                  destRld.setPaintGaps(sourceRld.isPaintGaps());                                  destRld.setPaintGaps(sourceRld.isPaintGaps());
# Line 629  public class StyledLayerUtil { Line 644  public class StyledLayerUtil {
644                  }                  }
645                  if (styledObject instanceof StyledFeatureCollectionInterface                  if (styledObject instanceof StyledFeatureCollectionInterface
646                                  && (style.getMetaData() instanceof Map || style.getMetaData() == null)) {                                  && (style.getMetaData() instanceof Map || style.getMetaData() == null)) {
647                          AttributeMetadataMap sourceAmd = (AttributeMetadataMap) style                          final AttributeMetadataMap sourceAmd = (AttributeMetadataMap) style
648                                          .getMetaData();                                          .getMetaData();
649                          AttributeMetadataMap destAmd = ((StyledFeatureCollectionInterface) styledObject)                          final AttributeMetadataMap destAmd = ((StyledFeatureCollectionInterface) styledObject)
650                                          .getAttributeMetaDataMap();                                          .getAttributeMetaDataMap();
651                          if (destAmd != null && sourceAmd != null) {                          if (destAmd != null && sourceAmd != null) {
652                                  destAmd.clear();                                  destAmd.clear();
# Line 661  public class StyledLayerUtil { Line 676  public class StyledLayerUtil {
676           *         {@link StyledFeatureCollectionInterface}           *         {@link StyledFeatureCollectionInterface}
677           */           */
678          public static StyledLayerStyle<?> getStyledLayerStyle(          public static StyledLayerStyle<?> getStyledLayerStyle(
679                          StyledLayerInterface styledObject) {                          final StyledLayerInterface styledObject) {
680                  if (styledObject instanceof StyledGridCoverageInterface)                  if (styledObject instanceof StyledGridCoverageInterface)
681                          return getStyledLayerStyle((StyledGridCoverageInterface) styledObject);                          return getStyledLayerStyle((StyledGridCoverageInterface) styledObject);
682                  if (styledObject instanceof StyledFeatureCollectionInterface)                  if (styledObject instanceof StyledFeatureCollectionInterface)
# Line 680  public class StyledLayerUtil { Line 695  public class StyledLayerUtil {
695           *            a styled grid coverage           *            a styled grid coverage
696           */           */
697          public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle(          public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle(
698                          StyledGridCoverageInterface styledGC) {                          final StyledGridCoverageInterface styledGC) {
699                  return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(),                  return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(),
700                                  styledGC.getLegendMetaData());                                  styledGC.getLegendMetaData());
701          }          }
# Line 693  public class StyledLayerUtil { Line 708  public class StyledLayerUtil {
708           *            a styled feature collection           *            a styled feature collection
709           */           */
710          public static StyledLayerStyle<AttributeMetadataMap> getStyledLayerStyle(          public static StyledLayerStyle<AttributeMetadataMap> getStyledLayerStyle(
711                          StyledFeatureCollectionInterface styledFC) {                          final StyledFeatureCollectionInterface styledFC) {
712                  return new StyledLayerStyle<AttributeMetadataMap>(styledFC                  return new StyledLayerStyle<AttributeMetadataMap>(styledFC.getStyle(),
713                                  .getStyle(), styledFC.getAttributeMetaDataMap());                                  styledFC.getAttributeMetaDataMap());
714          }          }
715    
716          /**          /**
# Line 712  public class StyledLayerUtil { Line 727  public class StyledLayerUtil {
727           * @return {@code null} in case of any error           * @return {@code null} in case of any error
728           */           */
729          public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(          public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(
730                          URL geoObjectURL, String sldExt, String rldExt) {                          final URL geoObjectURL, final String sldExt, final String rldExt) {
731                  RasterLegendData metaData = null;                  RasterLegendData metaData = null;
732                  Style sldStyle = null;                  Style sldStyle = null;
733                  try {                  try {
734                          Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(                          final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(
735                                          geoObjectURL, sldExt));                                          geoObjectURL, sldExt));
736                          // SLD must be present                          // SLD must be present
737                          if (styles == null || styles.length == 0)                          if (styles == null || styles.length == 0)
738                                  return null;                                  return null;
739                          sldStyle = styles[0];                          sldStyle = styles[0];
740                  } catch (Exception err) {                  } catch (final Exception err) {
741                          // SLD must be present                          // SLD must be present
742                          LangUtil.logDebugError(LOGGER, err);                          LangUtil.logDebugError(LOGGER, err);
743                          return null;                          return null;
# Line 731  public class StyledLayerUtil { Line 746  public class StyledLayerUtil {
746                  try {                  try {
747                          metaData = StyledLayerUtil.loadRasterLegendData(IOUtil                          metaData = StyledLayerUtil.loadRasterLegendData(IOUtil
748                                          .changeUrlExt(geoObjectURL, rldExt));                                          .changeUrlExt(geoObjectURL, rldExt));
749                  } catch (FileNotFoundException err) {                  } catch (final FileNotFoundException err) {
750                          // ignore missing raster legend data                          // ignore missing raster legend data
751                  } catch (Exception err) {                  } catch (final Exception err) {
752                          // any other error during legend data creation leads to error                          // any other error during legend data creation leads to error
753                          LangUtil.logDebugError(LOGGER, err);                          LangUtil.logDebugError(LOGGER, err);
754                          return null;                          return null;
# Line 756  public class StyledLayerUtil { Line 771  public class StyledLayerUtil {
771           * @return {@code null} in case of any error           * @return {@code null} in case of any error
772           */           */
773          public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(          public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(
774                          URL geoObjectURL) {                          final URL geoObjectURL) {
775                  return loadStyledRasterStyle(geoObjectURL, "sld", "rld");                  return loadStyledRasterStyle(geoObjectURL, "sld", "rld");
776          }          }
777    
# Line 774  public class StyledLayerUtil { Line 789  public class StyledLayerUtil {
789           * @return {@code null} in case of any error           * @return {@code null} in case of any error
790           */           */
791          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(
792                          URL geoObjectURL, String sldExt, String rldExt) {                          final URL geoObjectURL, final String sldExt, final String rldExt) {
793                  AttributeMetadataMap metaData = null;                  AttributeMetadataMap metaData = null;
794                  Style sldStyle = null;                  Style sldStyle = null;
795                  try {                  try {
796                          Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(                          final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(
797                                          geoObjectURL, sldExt));                                          geoObjectURL, sldExt));
798                          // SLD must be present                          // SLD must be present
799                          if (styles == null || styles.length == 0)                          if (styles == null || styles.length == 0)
800                                  return null;                                  return null;
801                          sldStyle = styles[0];                          sldStyle = styles[0];
802                  } catch (Exception err) {                  } catch (final Exception err) {
803                          // SLD must be present                          // SLD must be present
804                          LangUtil.logDebugError(LOGGER, err);                          LangUtil.logDebugError(LOGGER, err);
805                          return null;                          return null;
# Line 793  public class StyledLayerUtil { Line 808  public class StyledLayerUtil {
808                  try {                  try {
809                          metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil                          metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil
810                                          .changeUrlExt(geoObjectURL, rldExt));                                          .changeUrlExt(geoObjectURL, rldExt));
811                  } catch (FileNotFoundException err) {                  } catch (final FileNotFoundException err) {
812                          // ignore missing attribute meta data                          // ignore missing attribute meta data
813                  } catch (Exception err) {                  } catch (final Exception err) {
814                          // any other error during meta data creation leads to error                          // any other error during meta data creation leads to error
815                          LangUtil.logDebugError(LOGGER, err);                          LangUtil.logDebugError(LOGGER, err);
816                          return null;                          return null;
817                  }                  }
818    
819                  return new StyledLayerStyle<AttributeMetadataMap>(sldStyle,                  return new StyledLayerStyle<AttributeMetadataMap>(sldStyle, metaData);
                                 metaData);  
820          }          }
821    
822          /**          /**
# Line 820  public class StyledLayerUtil { Line 834  public class StyledLayerUtil {
834           * @return {@code null} in case of any error           * @return {@code null} in case of any error
835           */           */
836          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(
837                          URL geoObjectURL) {                          final URL geoObjectURL) {
838                  return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");                  return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");
839          }          }
840    
# Line 837  public class StyledLayerUtil { Line 851  public class StyledLayerUtil {
851           * @param mdExt           * @param mdExt
852           *            file extention for the meta-data file           *            file extention for the meta-data file
853           */           */
854          public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style,          public static <T> void saveStyledLayerStyle(
855                          URL geoObjectURL, String sldExt, String mdExt) throws Exception {                          final StyledLayerStyle<T> style, final URL geoObjectURL,
856                            final String sldExt, final String mdExt) throws Exception {
857                  // Store the SLD                  // Store the SLD
858                  Style sldStyle = style.getGeoObjectStyle();                  final Style sldStyle = style.getGeoObjectStyle();
859                  if (sldStyle != null) {                  if (sldStyle != null) {
860                          StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File(                          StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File(
861                                          geoObjectURL.toURI()), sldExt));                                          geoObjectURL.toURI()), sldExt));
862                  }                  }
863    
864                  // Store the meta data                  // Store the meta data
865                  T metaData = style.getMetaData();                  final T metaData = style.getMetaData();
866                  if (metaData != null) {                  if (metaData != null) {
867                          if (metaData instanceof RasterLegendData) {                          if (metaData instanceof RasterLegendData) {
868                                  saveRasterLegendData((RasterLegendData) metaData, IOUtil                                  saveRasterLegendData((RasterLegendData) metaData, IOUtil
# Line 856  public class StyledLayerUtil { Line 871  public class StyledLayerUtil {
871                                  // Map<Integer,AttributeMetaData> ) { // LEIDER NICHT                                  // Map<Integer,AttributeMetaData> ) { // LEIDER NICHT
872                                  // KOMPILIERBAR!!                                  // KOMPILIERBAR!!
873                          } else if (metaData instanceof Map) {                          } else if (metaData instanceof Map) {
874                                  saveAttributeMetaDataMap(                                  saveAttributeMetaDataMap((AttributeMetadataMap) metaData,
875                                                  (AttributeMetadataMap) metaData, IOUtil                                                  IOUtil.changeUrlExt(geoObjectURL, mdExt));
                                                                 .changeUrlExt(geoObjectURL, mdExt));  
876                          } else                          } else
877                                  throw new UnsupportedOperationException(                                  throw new UnsupportedOperationException(
878                                                  "Export for meta data not yet supported: "                                                  "Export for meta data not yet supported: "
# Line 876  public class StyledLayerUtil { Line 890  public class StyledLayerUtil {
890           * @param geoObjectURL           * @param geoObjectURL
891           *            URL of the (already read) raster object           *            URL of the (already read) raster object
892           */           */
893          public static void saveStyledLayerStyle(StyledLayerStyle<?> style,          public static void saveStyledLayerStyle(final StyledLayerStyle<?> style,
894                          URL geoObjectURL) throws Exception {                          final URL geoObjectURL) throws Exception {
895                  if (style.getMetaData() instanceof RasterLegendData)                  if (style.getMetaData() instanceof RasterLegendData)
896                          saveStyledLayerStyle(style, geoObjectURL, "sld", "rld");                          saveStyledLayerStyle(style, geoObjectURL, "sld", "rld");
897                  else                  else
# Line 885  public class StyledLayerUtil { Line 899  public class StyledLayerUtil {
899          }          }
900    
901          /**          /**
902           * Creates a {@link Box} that shows a legend for a list of           * Creates a {@link JPanel} that shows a legend for a list of
903           * {@link FeatureTypeStyle}s and a targeted featureType           * {@link FeatureTypeStyle}s and a targeted featureType
904           *           *
905           * @param featureType           * @param featureType
# Line 896  public class StyledLayerUtil { Line 910  public class StyledLayerUtil {
910           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
911           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
912           */           */
913          public static Box createLegendPanel(List<FeatureTypeStyle> list,          public static JPanel createLegendPanel(Style style,
914                          SimpleFeatureType featureType, int iconWidth, int iconHeight) {                          final SimpleFeatureType featureType, final int iconWidth,
915                            final int iconHeight) {
916    
917                  Box box = new Box(BoxLayout.Y_AXIS) {                  final List<FeatureTypeStyle> list = style.featureTypeStyles();
918    
919                          /**                  final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]"));
920                           * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot  
921                           * gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS                  if (style == null) {
922                           * gesetzt.                          // No Style => no legend
923                           *                          return panel;
924                           * @author <a href="mailto:[email protected]">Stefan Alfons                  }
                          *         Kr&uuml;ger</a>  
                          */  
                         @Override  
                         public void print(Graphics g) {  
                                 final Color orig = getBackground();  
                                 setBackground(Color.WHITE);  
                                 // wrap in try/finally so that we always restore the state  
                                 try {  
                                         super.print(g);  
                                 } finally {  
                                         setBackground(orig);  
                                 }  
                         }  
                 };  
925    
926                  for (FeatureTypeStyle ftStyle : list) {                  for (final FeatureTypeStyle ftStyle : list) {
927    
928                          // One child-node for every rule                          // One child-node for every rule
929                          List<Rule> rules = ftStyle.rules();                          final List<Rule> rules = ftStyle.rules();
930                          for (Rule rule : rules) {                          for (final Rule rule : rules) {
931    
932                                  /**                                  /**
933                                   * Let's not create a hbox for Rules that only contain                                   * Let's not create a hbox for Rules that only contain
# Line 936  public class StyledLayerUtil { Line 937  public class StyledLayerUtil {
937                                                  .size() == rule.getSymbolizers().length)                                                  .size() == rule.getSymbolizers().length)
938                                          continue;                                          continue;
939    
                                 Box hbox = new Box(BoxLayout.X_AXIS) {  
   
                                         /**  
                                          * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein  
                                          * Screenshot gemacht wird) wird. Dann werden wird der  
                                          * Hintergrund auf WEISS gesetzt.  
                                          */  
                                         @Override  
                                         public void print(Graphics g) {  
                                                 final Color orig = getBackground();  
                                                 setBackground(Color.WHITE);  
                                                 // wrap in try/finally so that we always restore the  
                                                 // state  
                                                 try {  
                                                         super.print(g);  
                                                 } finally {  
                                                         setBackground(orig);  
                                                 }  
                                         }  
                                 };  
   
                                 /**  
                                  * The size of the legend Symbol is dependent on the size of the  
                                  * font.  
                                  */  
                                 final int fontHeight = new JLabel().getFontMetrics(  
                                                 new JLabel().getFont()).getHeight();  
   
                                 final Dimension ICON_SIZE = new Dimension(iconWidth,  
                                                 fontHeight > 5 ? fontHeight : iconHeight);  
   
                                 // ****************************************************************************  
                                 // Create the actual icon  
                                 // ****************************************************************************  
940                                  final BufferedImage imageForRule = LegendIconFeatureRenderer                                  final BufferedImage imageForRule = LegendIconFeatureRenderer
941                                                  .getInstance().createImageForRule(rule, featureType,                                                  .getInstance().createImageForRule(rule, featureType,
942                                                                  ICON_SIZE);                                                                  new Dimension(iconWidth, iconHeight));
943    
944                                  // LOGGER.debug("Creating a new Legend Image for RUle name =                                  final ImageIcon legendIcon = new ImageIcon(imageForRule);
                                 // "+rule.getName());  
   
                                 ImageIcon legendIcon = new ImageIcon(imageForRule);  
945    
946                                  final JLabel iconLabel = new JLabel(legendIcon);                                  final JLabel iconLabel = new JLabel(legendIcon);
947                                  hbox.setAlignmentX(0f);                                  panel.add(iconLabel, "sgx1");
948                                  hbox.add(iconLabel);                                  // hbox.setAlignmentX(0f);
949                                  hbox.add(Box.createHorizontalStrut(3));                                  // hbox.add(iconLabel);
950                                    // hbox.add(Box.createHorizontalStrut(3));
951    
952                                  Translation labelT = new Translation();                                  final Translation labelT = new Translation();
953                                  labelT.fromOneLine(rule.getDescription().getTitle());                                  labelT.fromOneLine(rule.getDescription().getTitle());
   
954                                  final JLabel classTitleLabel = new JLabel(labelT.toString());                                  final JLabel classTitleLabel = new JLabel(labelT.toString());
                                 hbox.add(classTitleLabel);  
                                 classTitleLabel.setLabelFor(iconLabel);  
   
                                 box.add(hbox);  
955    
956                                    panel.add(classTitleLabel, "sgx2");
957                                    classTitleLabel.setLabelFor(iconLabel);
958                          }                          }
959                  }                  }
960    
961                  return box;                  return panel;
962          }          }
963    
964          /**          /**
965           * Creates a           * Creates a {@link JComponent} that contains a legend for a given
966             * rasterLayer and a given {@link Style}.
967           *           *
968           * @param styledRaster           * @param style
969           * @param iconHeight           *            if <code>null</code>, the default {@link Style} is extracetd
970           * @param iconWidth           *            from the {@link StyledRasterInterface}
          * @return  
971           */           */
972          public static Box createLegendPanel(StyledRasterInterface<?> styledRaster,          public static JPanel createLegendPanel(
973                          int iconWidth, int iconHeight) {                          final StyledRasterInterface<?> styledRaster, Style style,
974                            final int iconWidth, final int iconHeight) {
975    
976                    // If no style is given, we use the default style for this layer
977                    if (style == null)
978                            style = styledRaster.getStyle();
979    
980                  /**                  /**
981                   * Determine whether a Style is responsible for the coloring                   * Determine whether a Style is responsible for the coloring
982                   */                   */
983                  ColorModel colorModel = null;                  ColorModel colorModel = null;
984                  if (!isStyleable(styledRaster)                  if (!isStyleable(styledRaster)
985                                  || (isStyleable(styledRaster) && styledRaster.getStyle() == null)) {                                  || (isStyleable(styledRaster) && style == null)) {
986                          colorModel = getColorModel(styledRaster);                          colorModel = getColorModel(styledRaster);
987                  }                  }
988    
989                  RasterLegendData rasterLegendData = styledRaster.getLegendMetaData();                  final RasterLegendData rasterLegendData = styledRaster
990                  List<Double> legendRasterValues = rasterLegendData.getSortedKeys();                                  .getLegendMetaData();
991                  Map<Double, GridCoverage2D> sampleRasters = rasterLegendData                  final List<Double> legendRasterValues = rasterLegendData
992                                    .getSortedKeys();
993                    final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
994                                  .createSampleRasters();                                  .createSampleRasters();
995    
996                  Box box = new Box(BoxLayout.Y_AXIS) {                  final JPanel panel = new JPanel(new MigLayout("wrap 2"));
   
                         /**  
                          * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot  
                          * gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS  
                          * gesetzt.  
                          */  
                         @Override  
                         public void print(Graphics g) {  
                                 final Color orig = getBackground();  
                                 setBackground(Color.WHITE);  
                                 // wrap in try/finally so that we always restore the state  
                                 try {  
                                         super.print(g);  
                                 } finally {  
                                         setBackground(orig);  
                                 }  
                         }  
                 };  
   
                 for (Double rValue : legendRasterValues) {  
997    
998                          /**                  for (final Double rValue : legendRasterValues) {
                          *  
                          */  
                         Box hbox = new Box(BoxLayout.X_AXIS) {  
   
                                 /**  
                                  * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein  
                                  * Screenshot gemacht wird) wird. Dann werden wird der  
                                  * Hintergrund auf WEISS gesetzt.  
                                  */  
                                 @Override  
                                 public void print(Graphics g) {  
                                         final Color orig = getBackground();  
                                         setBackground(Color.WHITE);  
                                         // wrap in try/finally so that we always restore the  
                                         // state  
                                         try {  
                                                 super.print(g);  
                                         } finally {  
                                                 setBackground(orig);  
                                         }  
                                 }  
                         };  
999    
1000                          final Dimension ICON_SIZE = new Dimension(iconWidth,                          // final Dimension ICON_SIZE = new Dimension(iconWidth,
1001                                          new JLabel().getFontMetrics(new JLabel().getFont())                          // new JLabel().getFontMetrics(new JLabel().getFont())
1002                                                          .getHeight() > 5 ? new JLabel().getFontMetrics(                          // .getHeight() > 5 ? new JLabel().getFontMetrics(
1003                                                          new JLabel().getFont()).getHeight() : iconHeight);                          // new JLabel().getFont()).getHeight() : iconHeight);
1004    
1005                          // ****************************************************************************                          // ****************************************************************************
1006                          // Create the actual icon                          // Create the actual icon
1007                          // ****************************************************************************                          // ****************************************************************************
1008                          BufferedImage buffImage = new BufferedImage(ICON_SIZE.width,                          final BufferedImage buffImage = new BufferedImage(iconWidth,
1009                                          ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);                                          iconHeight, BufferedImage.TYPE_INT_ARGB);
1010                          Graphics2D graphics = buffImage.createGraphics();  
1011                            final Graphics2D graphics = buffImage.createGraphics();
1012    
1013                          if (colorModel != null) {                          if (colorModel != null) {
1014                                    // The colors come from the ColorModel!
1015    
1016                                  try {                                  try {
1017                                          Object inData = null;                                          Object inData = null;
# Line 1111  public class StyledLayerUtil { Line 1040  public class StyledLayerUtil {
1040                                          final Color color = new Color(colorModel.getRGB(inData));                                          final Color color = new Color(colorModel.getRGB(inData));
1041                                          graphics.setBackground(color);                                          graphics.setBackground(color);
1042                                          graphics.setColor(color);                                          graphics.setColor(color);
1043                                          graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height);                                          graphics.fillRect(0, 0, iconWidth, iconHeight);
1044                                  } catch (Exception e) {                                  } catch (final Exception e) {
1045                                          LOGGER.debug(                                          LOGGER.info(
1046                                                          "Dann nehmen wir halt den GridCoverageRenderer", e);                                                          "Dann nehmen wir halt den GridCoverageRenderer", e);
1047                                          colorModel = null;                                          colorModel = null;
1048                                  }                                  }
1049                          }                          } else {
1050                                    // The colors come from the Style
                         if (colorModel == null) {  
1051    
1052                                  /**                                  /**
1053                                   * The coverage contains only one value of value rValue                                   * The coverage contains only one value of value rValue
1054                                   */                                   */
1055                                  GridCoverage2D sampleCov = sampleRasters.get(rValue);                                  final GridCoverage2D sampleCov = sampleRasters.get(rValue);
1056                                  GridCoverageRenderer renderer;                                  GridCoverageRenderer renderer;
1057                                  try {                                  try {
1058                                          renderer = new GridCoverageRenderer(sampleCov                                          renderer = new GridCoverageRenderer(sampleCov
# Line 1132  public class StyledLayerUtil { Line 1060  public class StyledLayerUtil {
1060                                                          .createEnvelope(sampleCov.getEnvelope()),                                                          .createEnvelope(sampleCov.getEnvelope()),
1061                                                          new Rectangle(iconWidth, iconHeight),                                                          new Rectangle(iconWidth, iconHeight),
1062                                                          (AffineTransform) null);                                                          (AffineTransform) null);
1063                                  } catch (Exception e1) {                                  } catch (final Exception e1) {
1064                                          throw new RuntimeException(                                          throw new RuntimeException(
1065                                                          "Creating the GridCoverageRenderer:", e1);                                                          "Creating a GridCoverageRenderer failed:", e1);
1066                                  }                                  }
1067    
1068                                  /**                                  /**
1069                                   * Iterate over all FeatureTypeStyles.                                   * Iterate over all FeatureTypeStyles.
1070                                   */                                   */
1071                                  // for (FeatureTypeStyle ftStyle : styledGrid.getStyle()                                  final List<RasterSymbolizer> rSymbols = StylingUtil
                                 // .featureTypeStyles()) {  
                                 // One child-node for every rule  
                                 // List<Rule> rules = ftStyle.rules();  
                                 // for (Rule rule : rules) {  
                                 final Style style = styledRaster.getStyle();  
                                 List<RasterSymbolizer> rSymbols = StylingUtil  
1072                                                  .getRasterSymbolizers(style);                                                  .getRasterSymbolizers(style);
1073    
1074                                  for (RasterSymbolizer symbolizer : rSymbols) {                                  for (final RasterSymbolizer symbolizer : rSymbols) {
                                         // LOGGER.debug("Creating a new Legend Image for RUle  
                                         // name =  
                                         // "+rule.getName());  
1075                                          try {                                          try {
1076                                                  renderer.paint(graphics, sampleCov, symbolizer);                                                  renderer.paint(graphics, sampleCov, symbolizer);
1077                                          } catch (Exception ee) {                                          } catch (final Exception ee) {
1078                                                  LOGGER.error("Unable to paint " + symbolizer                                                  LOGGER.error("Unable to paint " + symbolizer
1079                                                                  + " into the legend image", ee);                                                                  + " into the legend image", ee);
1080                                          }                                          }
                                         // }  
                                         // }  
1081                                  }                                  }
1082                          }                          }
1083    
1084                          ImageIcon legendIcon = new ImageIcon(buffImage);                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));
1085                            // hbox.setAlignmentX(0f);
1086                          final JLabel iconLabel = new JLabel(legendIcon);                          panel.add(iconLabel, "sgx1");
1087                          hbox.setAlignmentX(0f);                          // hbox.add(Box.createHorizontalStrut(3));
                         hbox.add(iconLabel);  
                         hbox.add(Box.createHorizontalStrut(3));  
1088    
1089                          Translation labelT = rasterLegendData.get(rValue);                          final Translation labelT = rasterLegendData.get(rValue);
1090                          final JLabel classTitleLabel = new JLabel(labelT.toString());                          final JLabel classTitleLabel = new JLabel(labelT.toString());
1091                          hbox.add(classTitleLabel);                          panel.add(classTitleLabel, "sgx2"
1092                                            + (rasterLegendData.getPaintGaps() ? ", gapy 0 3" : ""));
1093                          classTitleLabel.setLabelFor(iconLabel);                          classTitleLabel.setLabelFor(iconLabel);
1094    
1095                          box.add(hbox);                          // box.add(hbox);
1096    
1097                          if (rasterLegendData.getPaintGaps()) {                          if (rasterLegendData.getPaintGaps()) {
1098                                  iconLabel                                  iconLabel
1099                                                  .setBorder(BorderFactory.createLineBorder(Color.black));                                                  .setBorder(BorderFactory.createLineBorder(Color.black));
                                 box.add(Box.createVerticalStrut(3));  
1100                          }                          }
1101    
1102                  }                  }
1103    
1104                  return box;                  return panel;
1105          }          }
1106    
1107          /**          /**
# Line 1194  public class StyledLayerUtil { Line 1109  public class StyledLayerUtil {
1109           * return <code>null</code> if the geoobject can not be accessed.           * return <code>null</code> if the geoobject can not be accessed.
1110           */           */
1111          @SuppressWarnings("unchecked")          @SuppressWarnings("unchecked")
1112          public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) {          public static ColorModel getColorModel(
1113                            final StyledRasterInterface<?> styledGrid) {
1114                  ColorModel colorModel = null;                  ColorModel colorModel = null;
1115                  try {                  try {
1116                          Object geoObject = styledGrid.getGeoObject();                          final Object geoObject = styledGrid.getGeoObject();
1117                          if (geoObject instanceof GridCoverage2D) {                          if (geoObject instanceof GridCoverage2D) {
1118                                  GridCoverage2D cov = (GridCoverage2D) geoObject;                                  final GridCoverage2D cov = (GridCoverage2D) geoObject;
1119                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1120                          } else if (styledGrid instanceof StyledRasterPyramidInterface) {                          } else if (styledGrid instanceof StyledRasterPyramidInterface) {
1121    
1122                                  Parameter readGG = new Parameter(                                  final Parameter readGG = new Parameter(
1123                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);
1124    
1125                                  ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope(                                  final ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope(
1126                                                  styledGrid.getEnvelope(), styledGrid.getCrs());                                                  styledGrid.getEnvelope(), styledGrid.getCrs());
1127    
1128                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(
1129                                                  new Rectangle(0, 0, 1, 1)), mapExtend));                                                  new Rectangle(0, 0, 1, 1)), mapExtend));
1130    
1131                                  FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;
1132    
1133                                  final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil                                  final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil
1134                                                  .getWrappedGeoObject(rFc);                                                  .getWrappedGeoObject(rFc);
1135                                  GridCoverage2D cov = (GridCoverage2D) aReader                                  final GridCoverage2D cov = (GridCoverage2D) aReader
1136                                                  .read(new GeneralParameterValue[] { readGG });                                                  .read(new GeneralParameterValue[] { readGG });
1137                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1138                          }                          }
1139                  } catch (Exception e) {                  } catch (final Exception e) {
1140                          LOGGER.error("Error reading the colormodel from " + styledGrid, e);                          LOGGER.error("Error reading the colormodel from " + styledGrid, e);
1141                          return null;                          return null;
1142                  }                  }
# Line 1233  public class StyledLayerUtil { Line 1149  public class StyledLayerUtil {
1149           *         with their own {@link ColorModel} and will ignore any           *         with their own {@link ColorModel} and will ignore any
1150           *         {@link RasterSymbolizer} = SLD.           *         {@link RasterSymbolizer} = SLD.
1151           */           */
1152          public static boolean isStyleable(StyledRasterInterface<?> styledRaster) {          public static boolean isStyleable(
1153                  ColorModel colorModel = getColorModel(styledRaster);                          final StyledRasterInterface<?> styledRaster) {
1154                    final ColorModel colorModel = getColorModel(styledRaster);
1155                  LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "                  
1156                                  + colorModel.getClass().getSimpleName());  //              LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
1157    //                              + colorModel != null ? colorModel.getClass().getSimpleName() : "NULL");
1158    
1159                  if (colorModel == null)                  if (colorModel == null)
1160                          return true;                          return true;
# Line 1245  public class StyledLayerUtil { Line 1162  public class StyledLayerUtil {
1162                          return true;                          return true;
1163                  return false;                  return false;
1164          }          }
1165    
1166            /**
1167             * Set the given Style as the Style of the {@link MapLayer}, unless the
1168             * styles are the same (not comparing selection stuff). If the
1169             * {@link MapLayer}s {@link Style} is changed, the selection FTS is kept.<br/>
1170             * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a
1171             * repaint, so only use it when needed.
1172             *
1173             * @return <code>true</code> if the {@link MapLayer}'s {@link Style} has been changed.
1174             */
1175            public static boolean updateMapLayerStyleIfChangedAndKeepSelection(MapLayer mapLayer,
1176                            Style style2) {
1177    
1178                    Style mapLayerStyleCleaned = StylingUtil
1179                                                    .removeSelectionFeatureTypeStyle(mapLayer.getStyle());
1180                    
1181                    Style newStyleCleaned = StylingUtil.removeSelectionFeatureTypeStyle(style2);
1182                    
1183                    if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned,
1184                                    newStyleCleaned)) {
1185                            
1186                            // They are different when compared without SELECTION FTS!
1187                            
1188                            // Now let's copy any SELECTION FTS to the now style
1189                            FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil.getSelectionFeatureTypeStyle( mapLayer.getStyle() );
1190                            if (selectionFeatureTypeStyle != null) {
1191                                    newStyleCleaned.featureTypeStyles().add(selectionFeatureTypeStyle);
1192                                    // newStyleCleaned is not so clean anymore... We just alled a selcetion FTS
1193                            }  
1194                            
1195                            mapLayer.setStyle(newStyleCleaned);
1196                            
1197                            return true;
1198                            
1199                    } else {
1200                            return false;
1201                    }
1202            }
1203    
1204  }  }

Legend:
Removed from v.464  
changed lines
  Added in v.534

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26