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

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


Author: alfonx
Date: 2009-09-14 15:29:42 +0200 (Mon, 14 Sep 2009)
New Revision: 403

Modified:
   branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java
Log:
* Moving towards a better raster legend!

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:16:11 UTC (rev 402)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java	2009-09-14 13:29:42 UTC (rev 403)
@@ -36,27 +36,33 @@
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
 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;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import javax.swing.BorderFactory;
 import javax.swing.Box;
 import javax.swing.BoxLayout;
 import javax.swing.ImageIcon;
 import javax.swing.JLabel;
 
 import org.apache.log4j.Logger;
+import org.geotools.coverage.grid.GeneralGridEnvelope;
 import org.geotools.coverage.grid.GridCoverage2D;
+import org.geotools.coverage.grid.GridGeometry2D;
 import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
 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.renderer.lite.gridcoverage2d.GridCoverageRenderer;
@@ -71,6 +77,7 @@
 import org.jdom.input.SAXBuilder;
 import org.jdom.output.XMLOutputter;
 import org.opengis.feature.simple.SimpleFeatureType;
+import org.opengis.parameter.GeneralParameterValue;
 
 import schmitzm.geotools.JTSUtil;
 import schmitzm.geotools.styling.StylingUtil;
@@ -1004,8 +1011,40 @@
 	 */
 	public static Box createLegendPanel(StyledRasterInterface<?> styledGrid,
 			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();
+			}
+		}
+
 		RasterLegendData rasterLegendData = styledGrid.getLegendMetaData();
-		Set<Double> legendRasterValues = rasterLegendData.keySet();
+		List<Double> legendRasterValues = rasterLegendData.getSortedKeys();
 		Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
 				.createSampleRasters();
 
@@ -1032,22 +1071,6 @@
 		for (Double rValue : legendRasterValues) {
 
 			/**
-			 * 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);
-			}
-
-			/**
 			 * 
 			 */
 			Box hbox = new Box(BoxLayout.X_AXIS) {
@@ -1083,30 +1106,58 @@
 					ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);
 			Graphics2D graphics = buffImage.createGraphics();
 
-			/**
-			 * Iterate over all FeatureTypeStyles.
-			 */
-			// for (FeatureTypeStyle ftStyle : styledGrid.getStyle()
-			// .featureTypeStyles()) {
-			// One child-node for every rule
-			// List<Rule> rules = ftStyle.rules();
+			if (colorModel != null)
+				try {
+					final int rgb = colorModel.getRGB(rValue.intValue());
+					final Color color = new Color(rgb);
+					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);
 
-			// for (Rule rule : rules) {
-			List<RasterSymbolizer> rSymbols = StylingUtil
-					.getRasterSymbolizers(styledGrid.getStyle());
+					/**
+					 * 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 e) {
-					LOGGER.error("Unable to paint " + symbolizer
-							+ " into the legend image", e);
+					/**
+					 * 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);
+
+					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);
 
@@ -1121,6 +1172,13 @@
 			classTitleLabel.setLabelFor(iconLabel);
 
 			box.add(hbox);
+
+			if (rasterLegendData.getPaintGaps()) {
+				iconLabel
+						.setBorder(BorderFactory.createLineBorder(Color.black));
+				box.add(Box.createVerticalStrut(3));
+			}
+
 		}
 
 		return box;



More information about the Schmitzm-commits mailing list