001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.ui.graphics;
018:
019: import org.eclipse.swt.graphics.FontData;
020: import org.geotools.filter.Expression;
021: import org.geotools.filter.Filters;
022: import org.geotools.styling.FeatureTypeStyle;
023: import org.geotools.styling.Font;
024: import org.geotools.styling.PointPlacement;
025: import org.geotools.styling.Rule;
026: import org.geotools.styling.SLD;
027: import org.geotools.styling.Style;
028: import org.geotools.styling.StyledLayerDescriptor;
029: import org.geotools.styling.TextSymbolizer;
030:
031: /**
032: * Utility class for working with Geotools SLD objects.
033: * <p>
034: * This class assumes a subset of the SLD specification:
035: * <ul>
036: * <li>Single Rule - matching Filter.NONE
037: * <li>Symbolizer lookup by name
038: * </ul>
039: * </p>
040: * <p>
041: * When you start to branch out to SLD information that contains
042: * multiple rules you will need to modify this class.
043: * </p>
044: * @author Jody Garnett, Refractions Research.
045: * @since 0.7.0
046: */
047: public final class SLDs extends SLD {
048:
049: public static final double ALIGN_LEFT = 1.0;
050: public static final double ALIGN_CENTER = 0.5;
051: public static final double ALIGN_RIGHT = 0.0;
052: public static final double ALIGN_BOTTOM = 1.0;
053: public static final double ALIGN_MIDDLE = 0.5;
054: public static final double ALIGN_TOP = 0.0;
055:
056: /**
057: * Grabs the font from the first TextSymbolizer.
058: * <p>
059: * If you are using something fun like symbols you
060: * will need to do your own thing.
061: * </p>
062: * @param symbolizer Text symbolizer information.
063: * @return FontData[] of the font's fill, or null if unavailable.
064: */
065: public static FontData[] textFont(TextSymbolizer symbolizer) {
066:
067: Font font = font(symbolizer);
068: if (font == null)
069: return null;
070: //FIXME: font style isn't being set properly here...seems screwy so leaving till later
071: // String fontStyle = font[0].getFontStyle().toString();
072: // if(fontStyle == null) return null;
073: // else if(fontStyle.equalsIgnoreCase("italic"))
074:
075: FontData[] tempFD = new FontData[1];
076: Expression fontFamily = font.getFontFamily();
077: if (font.getFontSize() == null || fontFamily == null)
078: return null;
079:
080: Number size = (Number) Filters.asType(font.getFontSize(),
081: Number.class);
082:
083: Object asType = Filters.asType(fontFamily, String.class);
084: tempFD[0] = new FontData((String) asType, size.intValue(), 1);
085:
086: if (tempFD[0] != null)
087: return tempFD;
088: return null;
089: }
090:
091: public static Font font(TextSymbolizer symbolizer) {
092: if (symbolizer == null)
093: return null;
094: Font[] font = symbolizer.getFonts();
095: if (font == null || font[0] == null)
096: return null;
097: return font[0];
098: }
099:
100: public static Style getDefaultStyle(StyledLayerDescriptor sld) {
101: Style[] styles = styles(sld);
102: for (int i = 0; i < styles.length; i++) {
103: if (styles[i].isDefault()) {
104: return styles[i];
105: }
106: }
107: //no default, so just grab the first one
108: return styles[0];
109: }
110:
111: public static boolean isSemanticTypeMatch(FeatureTypeStyle fts,
112: String regex) {
113: String[] identifiers = fts.getSemanticTypeIdentifiers();
114: for (int i = 0; i < identifiers.length; i++) {
115: if (identifiers[i].matches(regex))
116: return true;
117: }
118: return false;
119: }
120:
121: /**
122: * Returns the min scale of the default rule, or 0 if none is set
123: */
124: public static double minScale(FeatureTypeStyle fts) {
125: if (fts == null || fts.getRules().length == 0)
126: return 0.0;
127:
128: Rule r = fts.getRules()[0];
129: return r.getMinScaleDenominator();
130: }
131:
132: /**
133: * Returns the max scale of the default rule, or {@linkplain Double#NaN} if none is set
134: */
135: public static double maxScale(FeatureTypeStyle fts) {
136: if (fts == null || fts.getRules().length == 0)
137: return Double.NaN;
138:
139: Rule r = fts.getRules()[0];
140: return r.getMaxScaleDenominator();
141: }
142:
143: public static PointPlacement getPlacement(double horizAlign,
144: double vertAlign, double rotation) {
145: return builder.createPointPlacement(horizAlign, vertAlign,
146: rotation);
147: }
148:
149: //TODO: port these methods to the geotools parent class
150:
151: }
|