[Schmitzm-commits] r405 - branches/1.0-gt2-2.6/src/skrueger/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Sep 14 17:43:28 CEST 2009


Author: alfonx
Date: 2009-09-14 17:43:28 +0200 (Mon, 14 Sep 2009)
New Revision: 405

Modified:
   branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java
   branches/1.0-gt2-2.6/src/skrueger/geotools/StyledRasterInterface.java
Log:
* StyledLayerUtil now has methods getColorModel and isStyleable

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java	2009-09-14 13:49:30 UTC (rev 404)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java	2009-09-14 15:43:28 UTC (rev 405)
@@ -37,10 +37,11 @@
 import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.awt.image.ColorModel;
+import java.awt.image.ComponentColorModel;
+import java.awt.image.DataBuffer;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
-import java.io.IOException;
 import java.net.URL;
 import java.text.DecimalFormat;
 import java.util.HashMap;
@@ -60,11 +61,13 @@
 import org.geotools.coverage.grid.GridCoverage2D;
 import org.geotools.coverage.grid.GridGeometry2D;
 import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
+import org.geotools.coverage.grid.io.AbstractGridFormat;
 import org.geotools.feature.FeatureCollection;
 import org.geotools.gce.imagepyramid.ImagePyramidReader;
 import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.map.DefaultMapLayer;
 import org.geotools.map.MapLayer;
+import org.geotools.parameter.Parameter;
 import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer;
 import org.geotools.styling.ColorMap;
 import org.geotools.styling.ColorMapEntry;
@@ -1004,46 +1007,24 @@
 	/**
 	 * Creates a
 	 * 
-	 * @param styledGrid
+	 * @param styledRaster
 	 * @param iconHeight
 	 * @param iconWidth
 	 * @return
 	 */
-	public static Box createLegendPanel(StyledRasterInterface<?> styledGrid,
+	public static Box createLegendPanel(StyledRasterInterface<?> styledRaster,
 			int iconWidth, int iconHeight) {
 
 		/**
 		 * Determine whether a Style is responsible for the coloring
 		 */
-		Object geoObject = styledGrid.getGeoObject();
 		ColorModel colorModel = null;
-		if (geoObject instanceof GridCoverage2D) {
-			GridCoverage2D cov = (GridCoverage2D) geoObject;
-			colorModel = cov.getRenderedImage().getColorModel();
-		} 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();
-			}
+		if (!isStyleable(styledRaster)
+				|| (isStyleable(styledRaster) && styledRaster.getStyle() != null)) {
+			colorModel = getColorModel(styledRaster);
 		}
 
-		RasterLegendData rasterLegendData = styledGrid.getLegendMetaData();
+		RasterLegendData rasterLegendData = styledRaster.getLegendMetaData();
 		List<Double> legendRasterValues = rasterLegendData.getSortedKeys();
 		Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
 				.createSampleRasters();
@@ -1106,58 +1087,86 @@
 					ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);
 			Graphics2D graphics = buffImage.createGraphics();
 
-			if (colorModel != null)
+			if (colorModel != null) {
+
 				try {
-					final int rgb = colorModel.getRGB(rValue.intValue());
-					final Color color = new Color(rgb);
+					Object inData = null;
+					switch (colorModel.getTransferType()) {
+					case DataBuffer.TYPE_BYTE:
+						inData = new byte[] { rValue.byteValue() };
+						break;
+					case DataBuffer.TYPE_USHORT:
+						inData = new short[] { rValue.shortValue() };
+						break;
+					case DataBuffer.TYPE_INT:
+						inData = new int[] { rValue.intValue() };
+						break;
+					case DataBuffer.TYPE_SHORT:
+						inData = new short[] { rValue.shortValue() };
+						break;
+					case DataBuffer.TYPE_FLOAT:
+						inData = new float[] { rValue.floatValue() };
+						break;
+					case DataBuffer.TYPE_DOUBLE:
+						inData = new double[] { rValue.doubleValue() };
+						break;
+					default:
+						inData = rValue.intValue();
+					}
+					final Color color = new Color(colorModel.getRGB(inData));
 					graphics.setBackground(color);
 					graphics.setColor(color);
 					graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height);
 				} catch (Exception e) {
-					LOGGER.debug("Dann nehmen wir den GridCoverageRenderer", e);
+					LOGGER.debug("Dann nehmen wir halt den GridCoverageRenderer", e);
+					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);
-					}
+			if (colorModel == null) {
 
-					/**
-					 * Iterate over all FeatureTypeStyles.
-					 */
-					// for (FeatureTypeStyle ftStyle : styledGrid.getStyle()
-					// .featureTypeStyles()) {
-					// One child-node for every rule
-					// List<Rule> rules = ftStyle.rules();
-					// for (Rule rule : rules) {
-					final Style style = styledGrid.getStyle();
-					List<RasterSymbolizer> rSymbols = StylingUtil
-							.getRasterSymbolizers(style);
+				/**
+				 * 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);
+				}
 
-					for (RasterSymbolizer symbolizer : rSymbols) {
-						// LOGGER.debug("Creating a new Legend Image for RUle
-						// name =
-						// "+rule.getName());
-						try {
-							renderer.paint(graphics, sampleCov, symbolizer);
-						} catch (Exception ee) {
-							LOGGER.error("Unable to paint " + symbolizer
-									+ " into the legend image", ee);
-						}
-						// }
-						// }
+				/**
+				 * Iterate over all FeatureTypeStyles.
+				 */
+				// for (FeatureTypeStyle ftStyle : styledGrid.getStyle()
+				// .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
+						.getRasterSymbolizers(style);
+
+				for (RasterSymbolizer symbolizer : rSymbols) {
+					// LOGGER.debug("Creating a new Legend Image for RUle
+					// name =
+					// "+rule.getName());
+					try {
+						renderer.paint(graphics, sampleCov, symbolizer);
+					} catch (Exception ee) {
+						LOGGER.error("Unable to paint " + symbolizer
+								+ " into the legend image", ee);
 					}
+					// }
+					// }
 				}
+			}
 
 			ImageIcon legendIcon = new ImageIcon(buffImage);
 
@@ -1184,4 +1193,56 @@
 		return box;
 	}
 
+	/**
+	 * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May
+	 * return <code>null</code> if the geoobject can not be accessed.
+	 */
+	public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) {
+		ColorModel colorModel = null;
+		try {
+			Object geoObject = styledGrid.getGeoObject();
+			if (geoObject instanceof GridCoverage2D) {
+				GridCoverage2D cov = (GridCoverage2D) geoObject;
+				colorModel = cov.getRenderedImage().getColorModel();
+			} else if (geoObject instanceof ImagePyramidReader) {
+
+				Parameter readGG = new Parameter(
+						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;
+				GridCoverage2D cov = (GridCoverage2D) aReader
+						.read(new GeneralParameterValue[] { readGG });
+				colorModel = cov.getRenderedImage().getColorModel();
+			}
+		} catch (Exception e) {
+			LOGGER.error("Error reading the colormodel from " + styledGrid);
+			return null;
+		}
+		return colorModel;
+	}
+
+	/**
+	 * @return <code>true</code> if a {@link RasterSymbolizer} can be applied
+	 *         and will have an effect. Some rasters (e.g. GeoTIFF) can come
+	 *         with their own {@link ColorModel} and will ignore any
+	 *         {@link RasterSymbolizer} = SLD.
+	 */
+	public static boolean isStyleable(StyledRasterInterface<?> styledRaster) {
+		ColorModel colorModel = getColorModel(styledRaster);
+
+		LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
+				+ colorModel.getClass().getSimpleName());
+
+		if (colorModel == null)
+			return true;
+		if (colorModel instanceof ComponentColorModel)
+			return true;
+		return false;
+	}
 }

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/StyledRasterInterface.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/StyledRasterInterface.java	2009-09-14 13:49:30 UTC (rev 404)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/StyledRasterInterface.java	2009-09-14 15:43:28 UTC (rev 405)
@@ -32,7 +32,8 @@
 import skrueger.RasterLegendData;
 
 /**
- * A {@link StyledLayerInterface} that is associated to a {@link RasterLegendData}. The datatype is not yet defined.
+ * A {@link StyledLayerInterface} that is associated to a
+ * {@link RasterLegendData}. The datatype is not yet defined.
  * 
  * @see StyledGridCoverageInterface
  * 
@@ -41,7 +42,8 @@
 public interface StyledRasterInterface<E> extends StyledLayerInterface<E> {
 
 	/**
-	 * @return A {@link RasterLegendData} object with pairs of value / label information
+	 * @return A {@link RasterLegendData} object with pairs of value / label
+	 *         information
 	 */
 	RasterLegendData getLegendMetaData();
 



More information about the Schmitzm-commits mailing list