X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=gis%2Fruntime%2Forg.argeo.gis.geotools%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fgeotools%2Fstyling%2FStylingUtils.java;fp=gis%2Fruntime%2Forg.argeo.gis.geotools%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fgeotools%2Fstyling%2FStylingUtils.java;h=a4811ac8763466971f60115009e8c9024ff34700;hb=7725ae1388fb3b0ff5721566bf2e9e6c4b957675;hp=0000000000000000000000000000000000000000;hpb=00ec568367442bfb51e506e73d3b9e8bd7798a1c;p=lgpl%2Fargeo-commons.git diff --git a/gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/styling/StylingUtils.java b/gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/styling/StylingUtils.java new file mode 100644 index 000000000..a4811ac87 --- /dev/null +++ b/gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/styling/StylingUtils.java @@ -0,0 +1,103 @@ +package org.argeo.geotools.styling; + +import java.awt.Color; + +import org.argeo.ArgeoException; +import org.geotools.factory.CommonFactoryFinder; +import org.geotools.filter.text.cql2.CQL; +import org.geotools.filter.text.cql2.CQLException; +import org.geotools.styling.FeatureTypeStyle; +import org.geotools.styling.LineSymbolizer; +import org.geotools.styling.Rule; +import org.geotools.styling.Stroke; +import org.geotools.styling.Style; +import org.geotools.styling.StyleFactory; +import org.opengis.filter.Filter; +import org.opengis.filter.FilterFactory; + +/** Utilities related to GeoTools styling */ +public class StylingUtils { + static StyleFactory styleFactory = CommonFactoryFinder + .getStyleFactory(null); + static FilterFactory filterFactory = CommonFactoryFinder + .getFilterFactory(null); + + /** + * Style for a line + * + * @param color + * the AWT color in upper case + * @param width + * the width of the line + * @param cqlFilter + * filter in CQL formqt restricting the feqture upon which the + * style will apply + */ + public static Style createLineStyle(String color, Integer width) { + Rule rule = styleFactory.createRule(); + rule.symbolizers().add(createLineSymbolizer(color, width)); + FeatureTypeStyle fts = styleFactory + .createFeatureTypeStyle(new Rule[] { rule }); + Style style = styleFactory.createStyle(); + style.featureTypeStyles().add(fts); + return style; + } + + public static Style createFilteredLineStyle(String cqlFilter, + String matchedColor, Integer matchedWidth, String unmatchedColor, + Integer unmatchedWidth) { + // selection filter + Filter filter; + try { + filter = CQL.toFilter(cqlFilter); + } catch (CQLException e) { + throw new ArgeoException("Cannot parse CQL filter: " + cqlFilter, e); + } + + Rule[] rules; + // matched + Rule ruleMatched = styleFactory.createRule(); + ruleMatched.symbolizers().add( + createLineSymbolizer(matchedColor, matchedWidth)); + ruleMatched.setFilter(filter); + + // unmatched + if (unmatchedColor != null) { + Rule ruleUnMatched = styleFactory.createRule(); + ruleUnMatched.symbolizers().add( + createLineSymbolizer(unmatchedColor, + unmatchedWidth != null ? unmatchedWidth + : matchedWidth)); + ruleUnMatched.setFilter(filterFactory.not(filter)); + rules = new Rule[] { ruleMatched, ruleUnMatched }; + } else { + rules = new Rule[] { ruleMatched }; + } + + FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(rules); + Style style = styleFactory.createStyle(); + style.featureTypeStyles().add(fts); + return style; + } + + public static LineSymbolizer createLineSymbolizer(String color, + Integer width) { + Stroke stroke = styleFactory.createStroke( + filterFactory.literal(stringToColor(color)), + filterFactory.literal(width)); + return styleFactory.createLineSymbolizer(stroke, null); + } + + /** + * Converts a string to a color, using reflection, so that other methods + * don't need AWT dependencies in their signature. Package protected and not + * public so that it has less impact on the overall signature. + */ + static Color stringToColor(String color) { + try { + return (Color) Color.class.getField(color).get(null); + } catch (Exception e) { + throw new ArgeoException("Color " + color + " not found", e); + } + } +}