diff flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 4628:03ab907b6f5d

Merged.
author Raimund Renkert <rrenkert@intevation.de>
date Mon, 03 Dec 2012 17:27:08 +0100
parents 047c965ea542
children fc52ee878412
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java	Mon Dec 03 17:25:49 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java	Mon Dec 03 17:27:08 2012 +0100
@@ -1,15 +1,5 @@
 package de.intevation.flys.utils;
 
-import java.awt.Color;
-import java.awt.Font;
-
-import javax.xml.xpath.XPathConstants;
-
-import org.apache.log4j.Logger;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-
 import de.intevation.artifacts.common.utils.XMLUtils;
 import de.intevation.flys.artifacts.model.MapserverStyle;
 import de.intevation.flys.artifacts.model.MapserverStyle.Clazz;
@@ -17,6 +7,12 @@
 import de.intevation.flys.artifacts.model.MapserverStyle.Label;
 import de.intevation.flys.artifacts.model.MapserverStyle.Style;
 
+import java.awt.Color;
+import java.awt.Font;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+
 
 /**
  * Utility to deal with themes and their representations.
@@ -117,6 +113,15 @@
     public final static String XPATH_WSPLGEN_FIELDS =
             "/theme[@name='WSPLGEN']/field";
 
+    public final static String XPATH_WSPLGEN_STARTCOLOR =
+            "/theme/field[@name='startcolor']/@default";
+
+    public final static String XPATH_WSPLGEN_ENDCOLOR =
+            "/theme/field[@name='endcolor']/@default";
+
+    public final static String XPATH_WSPLGEN_NUMCLASSES =
+            "/theme/field[@name='numclasses']/@default";
+
     /** XPATH to bandwidth field. */
     public final static String XPATH_BANDWIDTH =
             "/theme/field[@name='bandwidth']/@default";
@@ -619,38 +624,39 @@
 
 
     public static String createWSPLGENStyle(Document theme) {
-        NodeList categories = (NodeList) XMLUtils.xpath(
-                theme,
-                XPATH_WSPLGEN_FIELDS,
-                XPathConstants.NODESET);
-
-        return createWSPLGENStyle(categories).toString();
-    }
-
-    protected static MapserverStyle createWSPLGENStyle(NodeList categories) {
         MapserverStyle ms = new MapserverStyle();
 
-        for (int i = 0, n = categories.getLength(); i < n; i++) {
-            Element  cat = (Element) categories.item(i);
-            String color = cat.getAttribute("default");
-            String name  = cat.getAttribute("name");
-
-            String expr;
+        String strStartColor = XMLUtils.xpathString(theme, XPATH_WSPLGEN_STARTCOLOR, null);
+        Color startColor = strStartColor != null ? parseColor(strStartColor) : new Color(178, 201, 215);
+        String strEndColor = XMLUtils.xpathString(theme, XPATH_WSPLGEN_ENDCOLOR, null);
+        Color endColor = strEndColor != null? parseColor(strEndColor) : new Color(2, 27, 42);
+        String strNumClasses = XMLUtils.xpathString(theme, XPATH_WSPLGEN_NUMCLASSES, null);
+        int numClasses = strNumClasses != null ? Integer.parseInt(strNumClasses) : 5;
 
-            try {
-                int length = name.length();
-                int idx    = Integer.parseInt(name.substring(length-1, length));
+        if (numClasses < 5) {
+            numClasses = 5;
+        }
+        else if (numClasses > 20) {
+            numClasses = 20;
+        }
 
-                expr = createWSPLGENExpression(idx);
-            }
-            catch (NumberFormatException nfe) {
-                logger.warn("Error while parsing WSPLGEN category.", nfe);
-                continue;
-            }
+        float rd = (endColor.getRed()   - startColor.getRed())   / (float)numClasses;
+        float gd = (endColor.getGreen() - startColor.getGreen()) / (float)numClasses;
+        float bd = (endColor.getBlue()  - startColor.getBlue())  / (float)numClasses;
 
-            Clazz      c = new Clazz(expr);
-            Style      s = new Style();
-            s.setColor(color.replace(",", ""));
+        for (int n = 0; n < numClasses; n++) {
+            StringBuilder newColor = new StringBuilder();
+            newColor.append(startColor.getRed()   + Math.round(n * rd));
+            newColor.append(' ');
+            newColor.append(startColor.getGreen() + Math.round(n * gd));
+            newColor.append(' ');
+            newColor.append(startColor.getBlue()  + Math.round(n * bd));
+
+            String expr = createWSPLGENExpression(n + 1, numClasses);
+
+            Clazz c = new Clazz(expr);
+            Style s = new Style();
+            s.setColor(newColor.toString());
             s.setSize(5);
 
             c.addItem(new Expression("(" + expr + ")"));
@@ -659,17 +665,17 @@
             ms.addClazz(c);
         }
 
-        return ms;
+        return ms.toString();
     }
 
 
-    protected static String createWSPLGENExpression(int idx) {
-        if (idx < 5) {
+    protected static String createWSPLGENExpression(int idx, int maxIdx) {
+        if (idx < maxIdx) {
             int lower = idx - 1;
             return "[DIFF] >= " + lower + " AND  [DIFF] < " + idx;
         }
         else {
-            return "[DIFF] >= 4";
+            return "[DIFF] >= " + (maxIdx - 1);
         }
     }
 

http://dive4elements.wald.intevation.org