1 package org
.argeo
.geotools
.styling
;
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
;
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);
29 * the AWT color in upper case
31 * the width of the line
33 * filter in CQL formqt restricting the feqture upon which the
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
);
46 public static Style
createFilteredLineStyle(String cqlFilter
,
47 String matchedColor
, Integer matchedWidth
, String unmatchedColor
,
48 Integer unmatchedWidth
) {
52 filter
= CQL
.toFilter(cqlFilter
);
53 } catch (CQLException e
) {
54 throw new ArgeoException("Cannot parse CQL filter: " + cqlFilter
, e
);
59 Rule ruleMatched
= styleFactory
.createRule();
60 ruleMatched
.symbolizers().add(
61 createLineSymbolizer(matchedColor
, matchedWidth
));
62 ruleMatched
.setFilter(filter
);
65 if (unmatchedColor
!= null) {
66 Rule ruleUnMatched
= styleFactory
.createRule();
67 ruleUnMatched
.symbolizers().add(
68 createLineSymbolizer(unmatchedColor
,
69 unmatchedWidth
!= null ? unmatchedWidth
71 ruleUnMatched
.setFilter(filterFactory
.not(filter
));
72 rules
= new Rule
[] { ruleMatched
, ruleUnMatched
};
74 rules
= new Rule
[] { ruleMatched
};
77 FeatureTypeStyle fts
= styleFactory
.createFeatureTypeStyle(rules
);
78 Style style
= styleFactory
.createStyle();
79 style
.featureTypeStyles().add(fts
);
83 public static LineSymbolizer
createLineSymbolizer(String color
,
85 Stroke stroke
= styleFactory
.createStroke(
86 filterFactory
.literal(stringToColor(color
)),
87 filterFactory
.literal(width
));
88 return styleFactory
.createLineSymbolizer(stroke
, null);
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.
96 static Color
stringToColor(String color
) {
98 return (Color
) Color
.class.getField(color
).get(null);
99 } catch (Exception e
) {
100 throw new ArgeoException("Color " + color
+ " not found", e
);