/[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 403 by alfonx, Mon Sep 14 13:29:42 2009 UTC revision 464 by alfonx, Tue Oct 13 13:22:31 2009 UTC
# Line 37  import java.awt.Rectangle; Line 37  import java.awt.Rectangle;
37  import java.awt.geom.AffineTransform;  import java.awt.geom.AffineTransform;
38  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
39  import java.awt.image.ColorModel;  import java.awt.image.ColorModel;
40    import java.awt.image.ComponentColorModel;
41    import java.awt.image.DataBuffer;
42  import java.io.File;  import java.io.File;
43  import java.io.FileNotFoundException;  import java.io.FileNotFoundException;
44  import java.io.FileWriter;  import java.io.FileWriter;
 import java.io.IOException;  
45  import java.net.URL;  import java.net.URL;
46  import java.text.DecimalFormat;  import java.text.DecimalFormat;
 import java.util.HashMap;  
47  import java.util.List;  import java.util.List;
48  import java.util.Map;  import java.util.Map;
 import java.util.SortedMap;  
 import java.util.TreeMap;  
49    
50  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
51  import javax.swing.Box;  import javax.swing.Box;
# Line 60  import org.geotools.coverage.grid.Genera Line 58  import org.geotools.coverage.grid.Genera
58  import org.geotools.coverage.grid.GridCoverage2D;  import org.geotools.coverage.grid.GridCoverage2D;
59  import org.geotools.coverage.grid.GridGeometry2D;  import org.geotools.coverage.grid.GridGeometry2D;
60  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
61    import org.geotools.coverage.grid.io.AbstractGridFormat;
62  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
63  import org.geotools.gce.imagepyramid.ImagePyramidReader;  import org.geotools.feature.NameImpl;
64  import org.geotools.geometry.jts.ReferencedEnvelope;  import org.geotools.geometry.jts.ReferencedEnvelope;
65  import org.geotools.map.DefaultMapLayer;  import org.geotools.map.DefaultMapLayer;
66  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
67    import org.geotools.parameter.Parameter;
68  import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer;  import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer;
69  import org.geotools.styling.ColorMap;  import org.geotools.styling.ColorMap;
70  import org.geotools.styling.ColorMapEntry;  import org.geotools.styling.ColorMapEntry;
# Line 76  import org.jdom.Document; Line 76  import org.jdom.Document;
76  import org.jdom.Element;  import org.jdom.Element;
77  import org.jdom.input.SAXBuilder;  import org.jdom.input.SAXBuilder;
78  import org.jdom.output.XMLOutputter;  import org.jdom.output.XMLOutputter;
79    import org.opengis.feature.simple.SimpleFeature;
80  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
81    import org.opengis.feature.type.Name;
82  import org.opengis.parameter.GeneralParameterValue;  import org.opengis.parameter.GeneralParameterValue;
83    
84  import schmitzm.geotools.JTSUtil;  import schmitzm.geotools.JTSUtil;
85    import schmitzm.geotools.feature.FeatureUtil;
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.SwingUtil;  import schmitzm.swing.SwingUtil;
90  import skrueger.AttributeMetaData;  import skrueger.AttributeMetadata;
91  import skrueger.RasterLegendData;  import skrueger.RasterLegendData;
92  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
93    
# Line 241  public class StyledLayerUtil { Line 244  public class StyledLayerUtil {
244           *            indicated whether the visible or invisible entries are           *            indicated whether the visible or invisible entries are
245           *            returned           *            returned
246           */           */
247          public static SortedMap<Integer, AttributeMetaData> getVisibleAttributeMetaData(          public static AttributeMetadataMap getVisibleAttributeMetaData(
248                          Map<Integer, AttributeMetaData> amdMap, boolean visible) {                          AttributeMetadataMap amdMap, boolean visible) {
249                  SortedMap<Integer, AttributeMetaData> filteredMap = new TreeMap<Integer, AttributeMetaData>();                  
250                  for (AttributeMetaData amd : amdMap.values())                  AttributeMetadataMap filteredMap = new AttributeMetadataMap();
251                          if (amd.isVisible())                  for (AttributeMetadata amd : amdMap.values())
252                                  filteredMap.put(amd.getColIdx(), amd);                          if (amd.isVisible() == visible)
253                                    filteredMap.put(amd.getName(), amd);
254    
255                  return filteredMap;                  return filteredMap;
256          }          }
257    
258          /**          /**
259           * Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}.           * Parses a {@link AttributeMetadata} object from an JDOM-{@link Element}.
260           * This method works like {@link           * This method works like {@link
261           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.
262           *           *
263           * @param element           * @param element
264           *            {@link Element} to parse           *            {@link Element} to parse
265           */           */
266          public static AttributeMetaData parseAttributeMetaData(final Element element) {          public static AttributeMetadata parseAttributeMetaData(final Element element) {
267                  final Integer col = Integer.valueOf(element.getAttributeValue("col"));                  final String namespace = String.valueOf(element.getAttributeValue("namespace"));
268                    final String localname = String.valueOf(element.getAttributeValue("localname"));
269                    final Name aName= new NameImpl(namespace,localname);
270                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
271                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
272                  final String unit = element.getAttributeValue("unit");                  final String unit = element.getAttributeValue("unit");
# Line 276  public class StyledLayerUtil { Line 282  public class StyledLayerUtil {
282                          else if (childElement.getName().equals("desc"))                          else if (childElement.getName().equals("desc"))
283                                  desc = parseTranslation(childElement);                                  desc = parseTranslation(childElement);
284                  }                  }
285                  return new AttributeMetaData(col, visible, name, desc, unit);                  return new AttributeMetadata(aName, visible, name, desc, unit);
286          }          }
287    
288          /**          /**
289           * Parses a {@link AttributeMetaData} map from an JDOM-{@link Element} with           * Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with
290           * {@code <attribute>}-childs.           * {@code <attribute>}-childs.
291           *           *
292           * @param element           * @param element
293           *            {@link Element} to parse           *            {@link Element} to parse
294           */           */
295          public static Map<Integer, AttributeMetaData> parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
296                          final Element element) {                          final Element element) {
297                  HashMap<Integer, AttributeMetaData> metaData = new HashMap<Integer, AttributeMetaData>();                  AttributeMetadataMap metaData = new AttributeMetadataMap();
298                  List<Element> attributesElements = element                  List<Element> attributesElements = element
299                                  .getChildren(ELEM_NAME_ATTRIBUTE);                                  .getChildren(ELEM_NAME_ATTRIBUTE);
300                  for (Element attibuteElement : attributesElements) {                  for (Element attibuteElement : attributesElements) {
301                          AttributeMetaData attrMetaData = parseAttributeMetaData(attibuteElement);                          AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);
302                          metaData.put(attrMetaData.getColIdx(), attrMetaData);                          metaData.put(attrMetaData.getName(), attrMetaData);
303                  }                  }
304                  return metaData;                  return metaData;
305          }          }
306    
307          /**          /**
308           * Loads a {@link AttributeMetaData} object from an URL.           * Loads a {@link AttributeMetadata} object from an URL.
309           *           *
310           * @param documentUrl           * @param documentUrl
311           *            {@link URL} to parse           *            {@link URL} to parse
312           * @see #parseAttributeMetaData(Element)           * @see #parseAttributeMetaData(Element)
313           */           */
314          public static Map<Integer, AttributeMetaData> loadAttributeMetaDataMap(          public static AttributeMetadataMap loadAttributeMetaDataMap(
315                          final URL documentUrl) throws Exception {                          final URL documentUrl) throws Exception {
316                  Document document = SAX_BUILDER.build(documentUrl);                  Document document = SAX_BUILDER.build(documentUrl);
317                  return parseAttributeMetaDataMap(document.getRootElement());                  return parseAttributeMetaDataMap(document.getRootElement());
318          }          }
319    
320          /**          /**
321           * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}
322           * object.           * object.
323           *           *
324           * @param amd           * @param amd
325           *            meta data for one attribute           *            meta data for one attribute
326           */           */
327          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
328                          final AttributeMetaData amd) {                          final AttributeMetadata amd) {
329                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);
330                  element.setAttribute("col", String.valueOf(amd.getColIdx()));                  element.setAttribute("namespace", String.valueOf(amd.getName().getNamespaceURI()));
331                    element.setAttribute("localname", String.valueOf(amd.getLocalName()));
332                  element.setAttribute("visible", String.valueOf(amd.isVisible()));                  element.setAttribute("visible", String.valueOf(amd.isVisible()));
333                  element.setAttribute("unit", amd.getUnit());                  element.setAttribute("unit", amd.getUnit());
334                  // Creating a aml:name tag...                  // Creating a aml:name tag...
# Line 332  public class StyledLayerUtil { Line 339  public class StyledLayerUtil {
339          }          }
340    
341          /**          /**
342           * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}
343           * map.           * map.
344           *           *
345           * @param amdMap           * @param amdMap
346           *            map of attribute meta data           *            map of attribute meta data
347           */           */
348          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
349                          final Map<Integer, AttributeMetaData> amdMap) {                          final AttributeMetadataMap amdMap) {
350                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
351                  for (AttributeMetaData amd : amdMap.values())                  for (AttributeMetadata amd : amdMap.values())
352                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
353                  return element;                  return element;
354          }          }
355    
356          /**          /**
357           * Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL.           * Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL.
358           *           *
359           * @param amdMap           * @param amdMap
360           *            map of {@link AttributeMetaData}           *            map of {@link AttributeMetadata}
361           * @param documentUrl           * @param documentUrl
362           *            {@link URL} to store the XML           *            {@link URL} to store the XML
363           */           */
364          public static void saveAttributeMetaDataMap(          public static void saveAttributeMetaDataMap(
365                          final Map<Integer, AttributeMetaData> amdMap, final URL documentUrl)                          final AttributeMetadataMap amdMap, final URL documentUrl)
366                          throws Exception {                          throws Exception {
367                  // Create XML-Document                  // Create XML-Document
368                  final FileWriter out = new FileWriter(new File(documentUrl.toURI()));                  final FileWriter out = new FileWriter(new File(documentUrl.toURI()));
# Line 622  public class StyledLayerUtil { Line 629  public class StyledLayerUtil {
629                  }                  }
630                  if (styledObject instanceof StyledFeatureCollectionInterface                  if (styledObject instanceof StyledFeatureCollectionInterface
631                                  && (style.getMetaData() instanceof Map || style.getMetaData() == null)) {                                  && (style.getMetaData() instanceof Map || style.getMetaData() == null)) {
632                          Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>) style                          AttributeMetadataMap sourceAmd = (AttributeMetadataMap) style
633                                          .getMetaData();                                          .getMetaData();
634                          Map<Integer, AttributeMetaData> destAmd = ((StyledFeatureCollectionInterface) styledObject)                          AttributeMetadataMap destAmd = ((StyledFeatureCollectionInterface) styledObject)
635                                          .getAttributeMetaDataMap();                                          .getAttributeMetaDataMap();
636                          if (destAmd != null && sourceAmd != null) {                          if (destAmd != null && sourceAmd != null) {
637                                  destAmd.clear();                                  destAmd.clear();
# Line 685  public class StyledLayerUtil { Line 692  public class StyledLayerUtil {
692           * @param styledFC           * @param styledFC
693           *            a styled feature collection           *            a styled feature collection
694           */           */
695          public static StyledLayerStyle<Map<Integer, AttributeMetaData>> getStyledLayerStyle(          public static StyledLayerStyle<AttributeMetadataMap> getStyledLayerStyle(
696                          StyledFeatureCollectionInterface styledFC) {                          StyledFeatureCollectionInterface styledFC) {
697                  return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(styledFC                  return new StyledLayerStyle<AttributeMetadataMap>(styledFC
698                                  .getStyle(), styledFC.getAttributeMetaDataMap());                                  .getStyle(), styledFC.getAttributeMetaDataMap());
699          }          }
700    
# Line 754  public class StyledLayerUtil { Line 761  public class StyledLayerUtil {
761          }          }
762    
763          /**          /**
764           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata
765           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD
766           * file must be present. A missing attribute meta-data file is tolerated.           * file must be present. A missing attribute meta-data file is tolerated.
767           *           *
# Line 766  public class StyledLayerUtil { Line 773  public class StyledLayerUtil {
773           *            file extention for the raster legend-data file           *            file extention for the raster legend-data file
774           * @return {@code null} in case of any error           * @return {@code null} in case of any error
775           */           */
776          public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle(          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(
777                          URL geoObjectURL, String sldExt, String rldExt) {                          URL geoObjectURL, String sldExt, String rldExt) {
778                  Map<Integer, AttributeMetaData> metaData = null;                  AttributeMetadataMap metaData = null;
779                  Style sldStyle = null;                  Style sldStyle = null;
780                  try {                  try {
781                          Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(                          Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(
# Line 794  public class StyledLayerUtil { Line 801  public class StyledLayerUtil {
801                          return null;                          return null;
802                  }                  }
803    
804                  return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(sldStyle,                  return new StyledLayerStyle<AttributeMetadataMap>(sldStyle,
805                                  metaData);                                  metaData);
806          }          }
807    
808          /**          /**
809           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
810           * {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd}           * {@linkplain AttributeMetadata AttributeMetaData-Map} from a {@code .amd}
811           * file for a given geo-object (feature) source. The SLD file must be           * file for a given geo-object (feature) source. The SLD file must be
812           * present. A missing attribute meta-data file is tolerated.           * present. A missing attribute meta-data file is tolerated.
813           *           *
# Line 812  public class StyledLayerUtil { Line 819  public class StyledLayerUtil {
819           *            file extention for the raster legend-data file           *            file extention for the raster legend-data file
820           * @return {@code null} in case of any error           * @return {@code null} in case of any error
821           */           */
822          public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle(          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(
823                          URL geoObjectURL) {                          URL geoObjectURL) {
824                  return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");                  return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");
825          }          }
# Line 850  public class StyledLayerUtil { Line 857  public class StyledLayerUtil {
857                                  // KOMPILIERBAR!!                                  // KOMPILIERBAR!!
858                          } else if (metaData instanceof Map) {                          } else if (metaData instanceof Map) {
859                                  saveAttributeMetaDataMap(                                  saveAttributeMetaDataMap(
860                                                  (Map<Integer, AttributeMetaData>) metaData, IOUtil                                                  (AttributeMetadataMap) metaData, IOUtil
861                                                                  .changeUrlExt(geoObjectURL, mdExt));                                                                  .changeUrlExt(geoObjectURL, mdExt));
862                          } else                          } else
863                                  throw new UnsupportedOperationException(                                  throw new UnsupportedOperationException(
# Line 861  public class StyledLayerUtil { Line 868  public class StyledLayerUtil {
868    
869          /**          /**
870           * Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and the           * Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and the
871           * meta data ({@link RasterLegendData} or {@link AttributeMetaData}) to a           * meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a
872           * {@code .rld} or {@code .amd} file. for a given geo-object source.           * {@code .rld} or {@code .amd} file. for a given geo-object source.
873           *           *
874           * @param style           * @param style
# Line 977  public class StyledLayerUtil { Line 984  public class StyledLayerUtil {
984                                  hbox.add(iconLabel);                                  hbox.add(iconLabel);
985                                  hbox.add(Box.createHorizontalStrut(3));                                  hbox.add(Box.createHorizontalStrut(3));
986    
                                 // ****************************************************************************  
                                 // The labeltext is read from the SLD. Its either the title  
                                 // directly, or interpreted as OneLine Code  
                                 // ****************************************************************************  
                                 // final String rawText =  
                                 // rule.getDescription().getTitle().toString();  
                                 final String rawText = rule.getDescription().getTitle()  
                                                 .toString();  
   
987                                  Translation labelT = new Translation();                                  Translation labelT = new Translation();
988                                  labelT.fromOneLine(rawText);                                  labelT.fromOneLine(rule.getDescription().getTitle());
989    
990                                  final JLabel classTitleLabel = new JLabel(labelT.toString());                                  final JLabel classTitleLabel = new JLabel(labelT.toString());
991                                  hbox.add(classTitleLabel);                                  hbox.add(classTitleLabel);
# Line 1004  public class StyledLayerUtil { Line 1002  public class StyledLayerUtil {
1002          /**          /**
1003           * Creates a           * Creates a
1004           *           *
1005           * @param styledGrid           * @param styledRaster
1006           * @param iconHeight           * @param iconHeight
1007           * @param iconWidth           * @param iconWidth
1008           * @return           * @return
1009           */           */
1010          public static Box createLegendPanel(StyledRasterInterface<?> styledGrid,          public static Box createLegendPanel(StyledRasterInterface<?> styledRaster,
1011                          int iconWidth, int iconHeight) {                          int iconWidth, int iconHeight) {
1012    
1013                  /**                  /**
1014                   * Determine whether a Style is responsible for the coloring                   * Determine whether a Style is responsible for the coloring
1015                   */                   */
                 Object geoObject = styledGrid.getGeoObject();  
1016                  ColorModel colorModel = null;                  ColorModel colorModel = null;
1017                  if (geoObject instanceof GridCoverage2D) {                  if (!isStyleable(styledRaster)
1018                          GridCoverage2D cov = (GridCoverage2D) geoObject;                                  || (isStyleable(styledRaster) && styledRaster.getStyle() == null)) {
1019                          colorModel = cov.getRenderedImage().getColorModel();                          colorModel = getColorModel(styledRaster);
                 } else if (geoObject instanceof ImagePyramidReader) {  
   
                         org.geotools.parameter.Parameter readGG = new org.geotools.parameter.Parameter(  
                                         org.geotools.coverage.grid.io.AbstractGridFormat.READ_GRIDGEOMETRY2D);  
   
                         ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope(  
                                         styledGrid.getEnvelope(), styledGrid.getCrs());  
   
                         readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(  
                                         new Rectangle(0, 0, 10, 10)), mapExtend));  
   
                         final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) geoObject;  
                         try {  
                                 GridCoverage2D cov = (GridCoverage2D) aReader  
                                                 .read(new GeneralParameterValue[] { readGG });  
                                 colorModel = cov.getRenderedImage().getColorModel();  
                         } catch (IllegalArgumentException e) {  
                                 e.printStackTrace();  
                         } catch (IOException e) {  
                                 e.printStackTrace();  
                         }  
1020                  }                  }
1021    
1022                  RasterLegendData rasterLegendData = styledGrid.getLegendMetaData();                  RasterLegendData rasterLegendData = styledRaster.getLegendMetaData();
1023                  List<Double> legendRasterValues = rasterLegendData.getSortedKeys();                  List<Double> legendRasterValues = rasterLegendData.getSortedKeys();
1024                  Map<Double, GridCoverage2D> sampleRasters = rasterLegendData                  Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
1025                                  .createSampleRasters();                                  .createSampleRasters();
# Line 1106  public class StyledLayerUtil { Line 1082  public class StyledLayerUtil {
1082                                          ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);                                          ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);
1083                          Graphics2D graphics = buffImage.createGraphics();                          Graphics2D graphics = buffImage.createGraphics();
1084    
1085                          if (colorModel != null)                          if (colorModel != null) {
1086    
1087                                  try {                                  try {
1088                                          final int rgb = colorModel.getRGB(rValue.intValue());                                          Object inData = null;
1089                                          final Color color = new Color(rgb);                                          switch (colorModel.getTransferType()) {
1090                                            case DataBuffer.TYPE_BYTE:
1091                                                    inData = new byte[] { rValue.byteValue() };
1092                                                    break;
1093                                            case DataBuffer.TYPE_USHORT:
1094                                                    inData = new short[] { rValue.shortValue() };
1095                                                    break;
1096                                            case DataBuffer.TYPE_INT:
1097                                                    inData = new int[] { rValue.intValue() };
1098                                                    break;
1099                                            case DataBuffer.TYPE_SHORT:
1100                                                    inData = new short[] { rValue.shortValue() };
1101                                                    break;
1102                                            case DataBuffer.TYPE_FLOAT:
1103                                                    inData = new float[] { rValue.floatValue() };
1104                                                    break;
1105                                            case DataBuffer.TYPE_DOUBLE:
1106                                                    inData = new double[] { rValue.doubleValue() };
1107                                                    break;
1108                                            default:
1109                                                    inData = rValue.intValue();
1110                                            }
1111                                            final Color color = new Color(colorModel.getRGB(inData));
1112                                          graphics.setBackground(color);                                          graphics.setBackground(color);
1113                                          graphics.setColor(color);                                          graphics.setColor(color);
1114                                          graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height);                                          graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height);
1115                                  } catch (Exception e) {                                  } catch (Exception e) {
1116                                          LOGGER.debug("Dann nehmen wir den GridCoverageRenderer", e);                                          LOGGER.debug(
1117                                                            "Dann nehmen wir halt den GridCoverageRenderer", e);
1118                                            colorModel = null;
1119                                    }
1120                            }
1121    
1122                                          /**                          if (colorModel == null) {
                                          * The coverage contains only one value of value rValue  
                                          */  
                                         GridCoverage2D sampleCov = sampleRasters.get(rValue);  
                                         GridCoverageRenderer renderer;  
                                         try {  
                                                 renderer = new GridCoverageRenderer(sampleCov  
                                                                 .getCoordinateReferenceSystem(), JTSUtil  
                                                                 .createEnvelope(sampleCov.getEnvelope()),  
                                                                 new Rectangle(iconWidth, iconHeight),  
                                                                 (AffineTransform) null);  
                                         } catch (Exception e1) {  
                                                 throw new RuntimeException(  
                                                                 "Creating the GridCoverageRenderer:", e1);  
                                         }  
1123    
1124                                          /**                                  /**
1125                                           * Iterate over all FeatureTypeStyles.                                   * The coverage contains only one value of value rValue
1126                                           */                                   */
1127                                          // for (FeatureTypeStyle ftStyle : styledGrid.getStyle()                                  GridCoverage2D sampleCov = sampleRasters.get(rValue);
1128                                          // .featureTypeStyles()) {                                  GridCoverageRenderer renderer;
1129                                          // One child-node for every rule                                  try {
1130                                          // List<Rule> rules = ftStyle.rules();                                          renderer = new GridCoverageRenderer(sampleCov
1131                                          // for (Rule rule : rules) {                                                          .getCoordinateReferenceSystem(), JTSUtil
1132                                          final Style style = styledGrid.getStyle();                                                          .createEnvelope(sampleCov.getEnvelope()),
1133                                          List<RasterSymbolizer> rSymbols = StylingUtil                                                          new Rectangle(iconWidth, iconHeight),
1134                                                          .getRasterSymbolizers(style);                                                          (AffineTransform) null);
1135                                    } catch (Exception e1) {
1136                                          for (RasterSymbolizer symbolizer : rSymbols) {                                          throw new RuntimeException(
1137                                                  // LOGGER.debug("Creating a new Legend Image for RUle                                                          "Creating the GridCoverageRenderer:", e1);
1138                                                  // name =                                  }
1139                                                  // "+rule.getName());  
1140                                                  try {                                  /**
1141                                                          renderer.paint(graphics, sampleCov, symbolizer);                                   * Iterate over all FeatureTypeStyles.
1142                                                  } catch (Exception ee) {                                   */
1143                                                          LOGGER.error("Unable to paint " + symbolizer                                  // for (FeatureTypeStyle ftStyle : styledGrid.getStyle()
1144                                                                          + " into the legend image", ee);                                  // .featureTypeStyles()) {
1145                                                  }                                  // One child-node for every rule
1146                                                  // }                                  // List<Rule> rules = ftStyle.rules();
1147                                                  // }                                  // for (Rule rule : rules) {
1148                                    final Style style = styledRaster.getStyle();
1149                                    List<RasterSymbolizer> rSymbols = StylingUtil
1150                                                    .getRasterSymbolizers(style);
1151    
1152                                    for (RasterSymbolizer symbolizer : rSymbols) {
1153                                            // LOGGER.debug("Creating a new Legend Image for RUle
1154                                            // name =
1155                                            // "+rule.getName());
1156                                            try {
1157                                                    renderer.paint(graphics, sampleCov, symbolizer);
1158                                            } catch (Exception ee) {
1159                                                    LOGGER.error("Unable to paint " + symbolizer
1160                                                                    + " into the legend image", ee);
1161                                          }                                          }
1162                                            // }
1163                                            // }
1164                                  }                                  }
1165                            }
1166    
1167                          ImageIcon legendIcon = new ImageIcon(buffImage);                          ImageIcon legendIcon = new ImageIcon(buffImage);
1168    
# Line 1184  public class StyledLayerUtil { Line 1189  public class StyledLayerUtil {
1189                  return box;                  return box;
1190          }          }
1191    
1192            /**
1193             * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May
1194             * return <code>null</code> if the geoobject can not be accessed.
1195             */
1196            @SuppressWarnings("unchecked")
1197            public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) {
1198                    ColorModel colorModel = null;
1199                    try {
1200                            Object geoObject = styledGrid.getGeoObject();
1201                            if (geoObject instanceof GridCoverage2D) {
1202                                    GridCoverage2D cov = (GridCoverage2D) geoObject;
1203                                    colorModel = cov.getRenderedImage().getColorModel();
1204                            } else if (styledGrid instanceof StyledRasterPyramidInterface) {
1205    
1206                                    Parameter readGG = new Parameter(
1207                                                    AbstractGridFormat.READ_GRIDGEOMETRY2D);
1208    
1209                                    ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope(
1210                                                    styledGrid.getEnvelope(), styledGrid.getCrs());
1211    
1212                                    readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(
1213                                                    new Rectangle(0, 0, 1, 1)), mapExtend));
1214    
1215                                    FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;
1216    
1217                                    final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil
1218                                                    .getWrappedGeoObject(rFc);
1219                                    GridCoverage2D cov = (GridCoverage2D) aReader
1220                                                    .read(new GeneralParameterValue[] { readGG });
1221                                    colorModel = cov.getRenderedImage().getColorModel();
1222                            }
1223                    } catch (Exception e) {
1224                            LOGGER.error("Error reading the colormodel from " + styledGrid, e);
1225                            return null;
1226                    }
1227                    return colorModel;
1228            }
1229    
1230            /**
1231             * @return <code>true</code> if a {@link RasterSymbolizer} can be applied
1232             *         and will have an effect. Some rasters (e.g. GeoTIFF) can come
1233             *         with their own {@link ColorModel} and will ignore any
1234             *         {@link RasterSymbolizer} = SLD.
1235             */
1236            public static boolean isStyleable(StyledRasterInterface<?> styledRaster) {
1237                    ColorModel colorModel = getColorModel(styledRaster);
1238    
1239                    LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
1240                                    + colorModel.getClass().getSimpleName());
1241    
1242                    if (colorModel == null)
1243                            return true;
1244                    if (colorModel instanceof ComponentColorModel)
1245                            return true;
1246                    return false;
1247            }
1248  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26