comparison 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
comparison
equal deleted inserted replaced
4627:d5821c6f0ab0 4628:03ab907b6f5d
1 package de.intevation.flys.utils; 1 package de.intevation.flys.utils;
2
3 import java.awt.Color;
4 import java.awt.Font;
5
6 import javax.xml.xpath.XPathConstants;
7
8 import org.apache.log4j.Logger;
9 import org.w3c.dom.Document;
10 import org.w3c.dom.Element;
11 import org.w3c.dom.NodeList;
12 2
13 import de.intevation.artifacts.common.utils.XMLUtils; 3 import de.intevation.artifacts.common.utils.XMLUtils;
14 import de.intevation.flys.artifacts.model.MapserverStyle; 4 import de.intevation.flys.artifacts.model.MapserverStyle;
15 import de.intevation.flys.artifacts.model.MapserverStyle.Clazz; 5 import de.intevation.flys.artifacts.model.MapserverStyle.Clazz;
16 import de.intevation.flys.artifacts.model.MapserverStyle.Expression; 6 import de.intevation.flys.artifacts.model.MapserverStyle.Expression;
17 import de.intevation.flys.artifacts.model.MapserverStyle.Label; 7 import de.intevation.flys.artifacts.model.MapserverStyle.Label;
18 import de.intevation.flys.artifacts.model.MapserverStyle.Style; 8 import de.intevation.flys.artifacts.model.MapserverStyle.Style;
19 9
10 import java.awt.Color;
11 import java.awt.Font;
12
13 import org.apache.log4j.Logger;
14 import org.w3c.dom.Document;
15
20 16
21 /** 17 /**
22 * Utility to deal with themes and their representations. 18 * Utility to deal with themes and their representations.
23 */ 19 */
24 public class ThemeUtil { 20 public class ThemeUtil {
114 public final static String XPATH_SHOW_MAXIMUM = 110 public final static String XPATH_SHOW_MAXIMUM =
115 "/theme/field[@name='showmaximum']/@default"; 111 "/theme/field[@name='showmaximum']/@default";
116 112
117 public final static String XPATH_WSPLGEN_FIELDS = 113 public final static String XPATH_WSPLGEN_FIELDS =
118 "/theme[@name='WSPLGEN']/field"; 114 "/theme[@name='WSPLGEN']/field";
115
116 public final static String XPATH_WSPLGEN_STARTCOLOR =
117 "/theme/field[@name='startcolor']/@default";
118
119 public final static String XPATH_WSPLGEN_ENDCOLOR =
120 "/theme/field[@name='endcolor']/@default";
121
122 public final static String XPATH_WSPLGEN_NUMCLASSES =
123 "/theme/field[@name='numclasses']/@default";
119 124
120 /** XPATH to bandwidth field. */ 125 /** XPATH to bandwidth field. */
121 public final static String XPATH_BANDWIDTH = 126 public final static String XPATH_BANDWIDTH =
122 "/theme/field[@name='bandwidth']/@default"; 127 "/theme/field[@name='bandwidth']/@default";
123 128
617 return parseBoolean(getShowMaximum(theme), false); 622 return parseBoolean(getShowMaximum(theme), false);
618 } 623 }
619 624
620 625
621 public static String createWSPLGENStyle(Document theme) { 626 public static String createWSPLGENStyle(Document theme) {
622 NodeList categories = (NodeList) XMLUtils.xpath(
623 theme,
624 XPATH_WSPLGEN_FIELDS,
625 XPathConstants.NODESET);
626
627 return createWSPLGENStyle(categories).toString();
628 }
629
630 protected static MapserverStyle createWSPLGENStyle(NodeList categories) {
631 MapserverStyle ms = new MapserverStyle(); 627 MapserverStyle ms = new MapserverStyle();
632 628
633 for (int i = 0, n = categories.getLength(); i < n; i++) { 629 String strStartColor = XMLUtils.xpathString(theme, XPATH_WSPLGEN_STARTCOLOR, null);
634 Element cat = (Element) categories.item(i); 630 Color startColor = strStartColor != null ? parseColor(strStartColor) : new Color(178, 201, 215);
635 String color = cat.getAttribute("default"); 631 String strEndColor = XMLUtils.xpathString(theme, XPATH_WSPLGEN_ENDCOLOR, null);
636 String name = cat.getAttribute("name"); 632 Color endColor = strEndColor != null? parseColor(strEndColor) : new Color(2, 27, 42);
637 633 String strNumClasses = XMLUtils.xpathString(theme, XPATH_WSPLGEN_NUMCLASSES, null);
638 String expr; 634 int numClasses = strNumClasses != null ? Integer.parseInt(strNumClasses) : 5;
639 635
640 try { 636 if (numClasses < 5) {
641 int length = name.length(); 637 numClasses = 5;
642 int idx = Integer.parseInt(name.substring(length-1, length)); 638 }
643 639 else if (numClasses > 20) {
644 expr = createWSPLGENExpression(idx); 640 numClasses = 20;
645 } 641 }
646 catch (NumberFormatException nfe) { 642
647 logger.warn("Error while parsing WSPLGEN category.", nfe); 643 float rd = (endColor.getRed() - startColor.getRed()) / (float)numClasses;
648 continue; 644 float gd = (endColor.getGreen() - startColor.getGreen()) / (float)numClasses;
649 } 645 float bd = (endColor.getBlue() - startColor.getBlue()) / (float)numClasses;
650 646
651 Clazz c = new Clazz(expr); 647 for (int n = 0; n < numClasses; n++) {
652 Style s = new Style(); 648 StringBuilder newColor = new StringBuilder();
653 s.setColor(color.replace(",", "")); 649 newColor.append(startColor.getRed() + Math.round(n * rd));
650 newColor.append(' ');
651 newColor.append(startColor.getGreen() + Math.round(n * gd));
652 newColor.append(' ');
653 newColor.append(startColor.getBlue() + Math.round(n * bd));
654
655 String expr = createWSPLGENExpression(n + 1, numClasses);
656
657 Clazz c = new Clazz(expr);
658 Style s = new Style();
659 s.setColor(newColor.toString());
654 s.setSize(5); 660 s.setSize(5);
655 661
656 c.addItem(new Expression("(" + expr + ")")); 662 c.addItem(new Expression("(" + expr + ")"));
657 c.addItem(s); 663 c.addItem(s);
658 664
659 ms.addClazz(c); 665 ms.addClazz(c);
660 } 666 }
661 667
662 return ms; 668 return ms.toString();
663 } 669 }
664 670
665 671
666 protected static String createWSPLGENExpression(int idx) { 672 protected static String createWSPLGENExpression(int idx, int maxIdx) {
667 if (idx < 5) { 673 if (idx < maxIdx) {
668 int lower = idx - 1; 674 int lower = idx - 1;
669 return "[DIFF] >= " + lower + " AND [DIFF] < " + idx; 675 return "[DIFF] >= " + lower + " AND [DIFF] < " + idx;
670 } 676 }
671 else { 677 else {
672 return "[DIFF] >= 4"; 678 return "[DIFF] >= " + (maxIdx - 1);
673 } 679 }
674 } 680 }
675 681
676 682
677 public static String createMapserverStyle(Document theme) { 683 public static String createMapserverStyle(Document theme) {

http://dive4elements.wald.intevation.org