]> git.argeo.org Git - lgpl/argeo-commons.git/blob - gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/styling/StylingUtils.java
Adapt for GeoTools 2.7.2
[lgpl/argeo-commons.git] / gis / runtime / org.argeo.gis.geotools / src / main / java / org / argeo / geotools / styling / StylingUtils.java
1 package org.argeo.geotools.styling;
2
3 import java.awt.Color;
4
5 import org.argeo.ArgeoException;
6 import org.geotools.factory.CommonFactoryFinder;
7 import org.geotools.filter.text.cql2.CQL;
8 import org.geotools.filter.text.cql2.CQLException;
9 import org.geotools.styling.FeatureTypeStyle;
10 import org.geotools.styling.LineSymbolizer;
11 import org.geotools.styling.Rule;
12 import org.geotools.styling.Stroke;
13 import org.geotools.styling.Style;
14 import org.geotools.styling.StyleFactory;
15 import org.opengis.filter.Filter;
16 import org.opengis.filter.FilterFactory;
17
18 /** Utilities related to GeoTools styling */
19 public class StylingUtils {
20 static StyleFactory styleFactory = CommonFactoryFinder
21 .getStyleFactory(null);
22 static FilterFactory filterFactory = CommonFactoryFinder
23 .getFilterFactory(null);
24
25 /**
26 * Style for a line
27 *
28 * @param color
29 * the AWT color in upper case
30 * @param width
31 * the width of the line
32 * @param cqlFilter
33 * filter in CQL formqt restricting the feqture upon which the
34 * style will apply
35 */
36 public static Style createLineStyle(String color, Integer width) {
37 Rule rule = styleFactory.createRule();
38 rule.symbolizers().add(createLineSymbolizer(color, width));
39 FeatureTypeStyle fts = styleFactory
40 .createFeatureTypeStyle(new Rule[] { rule });
41 Style style = styleFactory.createStyle();
42 style.featureTypeStyles().add(fts);
43 return style;
44 }
45
46 public static Style createFilteredLineStyle(String cqlFilter,
47 String matchedColor, Integer matchedWidth, String unmatchedColor,
48 Integer unmatchedWidth) {
49 // selection filter
50 Filter filter;
51 try {
52 filter = CQL.toFilter(cqlFilter);
53 } catch (CQLException e) {
54 throw new ArgeoException("Cannot parse CQL filter: " + cqlFilter, e);
55 }
56
57 Rule[] rules;
58 // matched
59 Rule ruleMatched = styleFactory.createRule();
60 ruleMatched.symbolizers().add(
61 createLineSymbolizer(matchedColor, matchedWidth));
62 ruleMatched.setFilter(filter);
63
64 // unmatched
65 if (unmatchedColor != null) {
66 Rule ruleUnMatched = styleFactory.createRule();
67 ruleUnMatched.symbolizers().add(
68 createLineSymbolizer(unmatchedColor,
69 unmatchedWidth != null ? unmatchedWidth
70 : matchedWidth));
71 ruleUnMatched.setFilter(filterFactory.not(filter));
72 rules = new Rule[] { ruleMatched, ruleUnMatched };
73 } else {
74 rules = new Rule[] { ruleMatched };
75 }
76
77 FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(rules);
78 Style style = styleFactory.createStyle();
79 style.featureTypeStyles().add(fts);
80 return style;
81 }
82
83 public static LineSymbolizer createLineSymbolizer(String color,
84 Integer width) {
85 Stroke stroke = styleFactory.createStroke(
86 filterFactory.literal(stringToColor(color)),
87 filterFactory.literal(width));
88 return styleFactory.createLineSymbolizer(stroke, null);
89 }
90
91 /**
92 * Converts a string to a color, using reflection, so that other methods
93 * don't need AWT dependencies in their signature. Package protected and not
94 * public so that it has less impact on the overall signature.
95 */
96 static Color stringToColor(String color) {
97 try {
98 return (Color) Color.class.getField(color).get(null);
99 } catch (Exception e) {
100 throw new ArgeoException("Color " + color + " not found", e);
101 }
102 }
103 }