[Schmitzm-commits] r1530 - trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Mar 20 15:35:41 CET 2011


Author: alfonx
Date: 2011-03-20 15:35:41 +0100 (Sun, 20 Mar 2011)
New Revision: 1530

Modified:
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java
Log:
* Preparing the legends to be scale dependent
* BUGFix: Order or RulesLists was reverted each time the AtlasStyler dialog opened them


Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java	2011-03-20 12:15:06 UTC (rev 1529)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java	2011-03-20 14:35:41 UTC (rev 1530)
@@ -519,17 +519,18 @@
 		final RasterLegendData rld = new RasterLegendData(paintGaps);
 		int idx = 0;
 		for (final ColorMapEntry cme : colorMap.getColorMapEntries()) {
-			
-			if (idx == colorMap.getColorMapEntries().length-1 && colorMap.getType() != ColorMap.TYPE_VALUES) {
+
+			if (idx == colorMap.getColorMapEntries().length - 1
+					&& colorMap.getType() != ColorMap.TYPE_VALUES) {
 				// Ignore the last CME label if not in VLAUES mode
 				continue;
 			}
-			
+
 			final double value = StylingUtil.getQuantityFromColorMapEntry(cme);
 			String label = cme.getLabel();
 			// if no label is set (e.g. quantitative style),
 			// use the value as label
-			if (label == null || label.equals("")){
+			if (label == null || label.equals("")) {
 				if (digits == null)
 					label = String.valueOf(value);
 				else
@@ -955,12 +956,15 @@
 	 *            The Style to presented in this legend
 	 * @param featureType
 	 *            If this a legend for Point, Polygon or Line? Or ANY or NONE?
+	 * @param scaleDenominator
+	 *            <code>null</code> of a Scale Denominator to determine whether
+	 *            a Rule is actually visible in that scale.
 	 * 
 	 * @author <a href="mailto:tzeggai at wikisquare.de">Stefan Alfons Tzeggai</a>
 	 */
 	public static JPanel createLegendSwingPanel(Style style,
 			final SimpleFeatureType featureType, final int iconWidth,
-			final int iconHeight) {
+			final int iconHeight, Double scaleDenominator) {
 
 		if (featureType == null) {
 			ExceptionDialog.show(new IllegalStateException(
@@ -989,6 +993,32 @@
 			final List<Rule> rules = ftStyle.rules();
 			for (final Rule rule : rules) {
 
+				/*
+				 * The MinScaleDenominator and MaxScaleDenominator elements of a
+				 * Rule define the range of map-rendering scales for which the
+				 * rule should be applied. The schema is: <xsd:element
+				 * name="MinScaleDenominator" type="xsd:double"/> <xsd:element
+				 * name="MaxScaleDenominator" type="xsd:double"/> The values
+				 * used are actually the scale denominators relative to a
+				 * “standardized rendering pixel size” (below). For example, an
+				 * element-content value of “10000000” means a scale of
+				 * 1:10-million. Scientific notation is also allowed here (and
+				 * for all non-integer numbers in SE), so a more convenient
+				 * value of “10e6” could also be used for the element content
+				 * for this example. The MinScaleDenominator and
+				 * MaxScaleDenominator elements, as their names suggest, are
+				 * simply the minimum and maximum ranges of scale (denominators)
+				 * of maps for which a rule should apply. The minimum scale is
+				 * inclusive and the maximum scale is exclusive. So, for
+				 * example, the following scale range:
+				 */
+				if (scaleDenominator != null
+						&& scaleDenominator < rule.getMinScaleDenominator())
+					continue;
+				if (scaleDenominator != null
+						&& scaleDenominator >= rule.getMaxScaleDenominator())
+					continue;
+
 				// Check if this RULE shall actually appear in the legend
 				if (rule.getName() != null
 						&& rule.getName().contains(HIDE_IN_LAYER_LEGEND_HINT))
@@ -1034,9 +1064,160 @@
 	 * @param style
 	 *            if <code>null</code>, the default {@link Style} is extracetd
 	 *            from the {@link StyledRasterInterface}
+	 * @param scaleDenominator
+	 *            <code>null</code> of a Scale Denominator to determine whether
+	 *            a Rule is actually visible in that scale.
 	 */
 	public static JPanel createLegendSwingPanel(
 			final StyledRasterInterface<?> styledRaster, Style style,
+			final int iconWidth, final int iconHeight, Double scaleDenominator) {
+
+		// If no style is given, we use the default style for this layer
+		if (style == null)
+			style = styledRaster.getStyle();
+
+		/**
+		 * Determine whether a Style is responsible for the coloring
+		 */
+		ColorModel colorModel = null;
+		if (!isStyleable(styledRaster)
+				|| (isStyleable(styledRaster) && style == null)) {
+			colorModel = getColorModel(styledRaster);
+		}
+
+		final RasterLegendData rasterLegendData = styledRaster
+				.getLegendMetaData();
+		final List<Double> legendRasterValues = rasterLegendData
+				.getSortedKeys();
+		final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
+				.createSampleRasters();
+
+		final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0"));
+
+		for (Double iteratorValue : legendRasterValues) {
+
+			Double rValue = iteratorValue;
+			final Translation labelT = rasterLegendData.get(rValue);
+
+			// EMpty labels will not appear in a raster legend
+			if (I18NUtil.isEmpty(labelT))
+				continue;
+
+			// final Dimension ICON_SIZE = new Dimension(iconWidth,
+			// new JLabel().getFontMetrics(new JLabel().getFont())
+			// .getHeight() > 5 ? new JLabel().getFontMetrics(
+			// new JLabel().getFont()).getHeight() : iconHeight);
+
+			// ****************************************************************************
+			// Create the actual icon
+			// ****************************************************************************
+			final BufferedImage buffImage = new BufferedImage(iconWidth,
+					iconHeight, BufferedImage.TYPE_INT_ARGB);
+
+			final Graphics2D graphics = buffImage.createGraphics();
+
+			if (colorModel != null) {
+				// The colors come from the ColorModel!
+
+				try {
+					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, iconWidth, iconHeight);
+				} catch (final Exception e) {
+					LOGGER.info(
+							"Dann nehmen wir halt den GridCoverageRenderer", e);
+					colorModel = null;
+				}
+			} else {
+				// The colors come from the Style. Here we have to check, which
+				// Labels to show. A Ruleslist may be disabled or out of the
+				// Min/Max Scale.
+
+				/**
+				 * The coverage contains only one value of value rValue
+				 */
+				final GridCoverage2D sampleCov = sampleRasters.get(rValue);
+				GridCoverageRenderer renderer;
+				try {
+					renderer = new GridCoverageRenderer(
+							sampleCov.getCoordinateReferenceSystem(),
+							JTSUtil.createEnvelope(sampleCov.getEnvelope()),
+							new Rectangle(iconWidth, iconHeight),
+							(AffineTransform) null);
+				} catch (final Exception e1) {
+					throw new RuntimeException(
+							"Creating a GridCoverageRenderer failed:", e1);
+				}
+
+				/**
+				 * Iterate over all FeatureTypeStyles.
+				 */
+				final List<RasterSymbolizer> rSymbols = StylingUtil
+						.getRasterSymbolizers(style);
+
+				for (final RasterSymbolizer symbolizer : rSymbols) {
+					try {
+						renderer.paint(graphics, sampleCov, symbolizer);
+					} catch (final Exception ee) {
+						LOGGER.error("Unable to paint " + symbolizer
+								+ " into the legend image", ee);
+					}
+				}
+			}
+
+			final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));
+			panel.add(iconLabel, "sgx1");
+
+			final JLabel classTitleLabel = new JLabel(labelT.toString());
+			panel.add(classTitleLabel, "sgx2"
+					+ (rasterLegendData.isPaintGaps() ? ", gapy 0:0:0 5:5:5"
+							: ""));
+			classTitleLabel.setLabelFor(iconLabel);
+
+			if (rasterLegendData.isPaintGaps()) {
+				iconLabel
+						.setBorder(BorderFactory.createLineBorder(Color.black));
+			}
+
+		}
+
+		return panel;
+	}
+
+	/**
+	 * Creates a {@link JComponent} that contains a legend for a given
+	 * {@link StyledRasterInterface} and a given {@link Style}.
+	 * 
+	 * @param style
+	 *            if <code>null</code>, the default {@link Style} is extracetd
+	 *            from the {@link StyledRasterInterface}
+	 */
+	public static JPanel OLDcreateLegendSwingPanel(
+			final StyledRasterInterface<?> styledRaster, Style style,
 			final int iconWidth, final int iconHeight) {
 
 		// If no style is given, we use the default style for this layer
@@ -1062,7 +1243,7 @@
 		final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0"));
 
 		for (Double iteratorValue : legendRasterValues) {
-			
+
 			Double rValue = iteratorValue;
 			final Translation labelT = rasterLegendData.get(rValue);
 
@@ -1082,8 +1263,6 @@
 					iconHeight, BufferedImage.TYPE_INT_ARGB);
 
 			final Graphics2D graphics = buffImage.createGraphics();
-			
-			
 
 			if (colorModel != null) {
 				// The colors come from the ColorModel!
@@ -1122,7 +1301,9 @@
 					colorModel = null;
 				}
 			} else {
-				// The colors come from the Style
+				// The colors come from the Style. Here we have to check, which
+				// Labels to show. A Ruleslist may be disabled or out of the
+				// Min/Max Scale.
 
 				/**
 				 * The coverage contains only one value of value rValue



More information about the Schmitzm-commits mailing list