0001: /*
0002: * GeoTools - OpenSource mapping toolkit
0003: * http://geotools.org
0004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation;
0009: * version 2.1 of the License.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: */
0016: package org.geotools.styling;
0017:
0018: import java.awt.Color;
0019: import java.net.MalformedURLException;
0020: import java.net.URL;
0021: import java.util.ArrayList;
0022: import java.util.HashSet;
0023: import java.util.List;
0024: import java.util.Set;
0025:
0026: import org.geotools.event.GTComponent;
0027: import org.geotools.event.GTRoot;
0028: import org.geotools.feature.FeatureType;
0029: import org.opengis.filter.Filter;
0030: import org.opengis.filter.expression.Expression;
0031: import org.opengis.filter.expression.Literal;
0032: import org.geotools.filter.Filters;
0033:
0034: /**
0035: * Utility class for working with Geotools SLD objects.
0036: *
0037: * <p>
0038: * This class assumes a subset of the SLD specification:
0039: *
0040: * <ul>
0041: * <li>
0042: * Single Rule - matching Filter.INCLUDE
0043: * </li>
0044: * <li>
0045: * Symbolizer lookup by name
0046: * </li>
0047: * </ul>
0048: * </p>
0049: *
0050: * <p>
0051: * When you start to branch out to SLD information that contains multiple rules
0052: * you will need to modify this class.
0053: * </p>
0054: *
0055: * @since 0.7.0
0056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/styling/SLD.java $
0057: */
0058: public class SLD {
0059: /** <code>NOTFOUND</code> indicates int value was unavailable */
0060: public static final int NOTFOUND = Filters.NOTFOUND;
0061: public static final StyleBuilder builder = new StyleBuilder();
0062:
0063: /**
0064: * Retrieve linestring color from linesymbolizer if available.
0065: *
0066: * @param symbolizer Line symbolizer information.
0067: *
0068: * @return Color of linestring, or null if unavailable.
0069: */
0070: public static Color lineColor(LineSymbolizer symbolizer) {
0071: if (symbolizer == null) {
0072: return null;
0073: }
0074:
0075: Stroke stroke = symbolizer.getStroke();
0076:
0077: return strokeColor(stroke);
0078: }
0079:
0080: public static Color strokeColor(Stroke stroke) {
0081: if (stroke == null) {
0082: return null;
0083: }
0084:
0085: return color(stroke.getColor());
0086: }
0087:
0088: public static Color color(Fill fill) {
0089: if (fill == null) {
0090: return null;
0091: }
0092:
0093: return color(fill.getColor());
0094: }
0095:
0096: /**
0097: * Sets the colour for a line symbolizer
0098: *
0099: * @param style
0100: * @param colour
0101: */
0102: public static void setLineColour(Style style, Color colour) {
0103: if (style == null) {
0104: return;
0105: }
0106:
0107: setLineColour(lineSymbolizer(style), colour);
0108: }
0109:
0110: /**
0111: * Sets the Colour for the given Line symbolizer
0112: *
0113: * @param symbolizer
0114: * @param colour
0115: */
0116: public static void setLineColour(LineSymbolizer symbolizer,
0117: Color colour) {
0118: if (symbolizer == null) {
0119: return;
0120: }
0121:
0122: Stroke stroke = symbolizer.getStroke();
0123:
0124: if (stroke == null) {
0125: stroke = builder.createStroke(colour);
0126: symbolizer.setStroke(stroke);
0127: }
0128:
0129: if (colour != null) {
0130: stroke.setColor(builder.colorExpression(colour));
0131: }
0132: }
0133:
0134: /**
0135: * Retrieve color from linesymbolizer if available.
0136: *
0137: * @param symbolizer Line symbolizer information.
0138: *
0139: * @return Color of linestring, or null if unavailable.
0140: */
0141: public static Color color(LineSymbolizer symbolizer) {
0142: return lineColor(symbolizer);
0143: }
0144:
0145: /**
0146: * Retrieve linestring width from symbolizer if available.
0147: *
0148: * @param symbolizer Line symbolizer information.
0149: *
0150: * @return width of linestring, or NOTFOUND
0151: */
0152: public static int lineWidth(LineSymbolizer symbolizer) {
0153: if (symbolizer == null) {
0154: return NOTFOUND;
0155: }
0156:
0157: Stroke stroke = symbolizer.getStroke();
0158:
0159: return width(stroke);
0160: }
0161:
0162: public static int width(Stroke stroke) {
0163: if (stroke == null) {
0164: return NOTFOUND;
0165: }
0166:
0167: return intValue(stroke.getWidth());
0168: }
0169:
0170: public static int size(Mark mark) {
0171: if (mark == null) {
0172: return NOTFOUND;
0173: }
0174:
0175: return intValue(mark.getSize());
0176: }
0177:
0178: /**
0179: * Retrieve linestring width from symbolizer if available.
0180: *
0181: * @param symbolizer Line symbolizer information.
0182: *
0183: * @return width of linestring, or NOTFOUND
0184: */
0185: public static int width(LineSymbolizer symbolizer) {
0186: return lineWidth(symbolizer);
0187: }
0188:
0189: /**
0190: * Grabs the opacity from the first LineSymbolizer.
0191: *
0192: * @param symbolizer Line symbolizer information.
0193: *
0194: * @return double of the line stroke's opacity, or NaN if unavailable.
0195: */
0196: public static double lineOpacity(LineSymbolizer symbolizer) {
0197: if (symbolizer == null) {
0198: return Double.NaN;
0199: }
0200:
0201: Stroke stroke = symbolizer.getStroke();
0202:
0203: return opacity(stroke);
0204: }
0205:
0206: public static double opacity(Stroke stroke) {
0207: if (stroke == null) {
0208: return Double.NaN;
0209: }
0210: return opacity(stroke.getOpacity());
0211: }
0212:
0213: public static double opacity(RasterSymbolizer rasterSymbolizer) {
0214: if (rasterSymbolizer == null) {
0215: return 1.0;
0216: }
0217: return opacity(rasterSymbolizer.getOpacity());
0218: }
0219:
0220: private static double opacity(Expression opacity) {
0221: if (opacity == null) {
0222: return 1.0;
0223: }
0224: Double numeric = (Double) opacity.evaluate(null, Double.class);
0225: return numeric.doubleValue();
0226: }
0227:
0228: /**
0229: * Grabs the linejoin from the first LineSymbolizer.
0230: *
0231: * @param symbolizer Line symbolizer information.
0232: *
0233: * @return String of the line stroke's linejoin, or null if unavailable.
0234: */
0235: public static String lineLinejoin(LineSymbolizer symbolizer) {
0236: if (symbolizer == null) {
0237: return null;
0238: }
0239:
0240: Stroke stroke = symbolizer.getStroke();
0241:
0242: if (stroke == null) {
0243: return null;
0244: }
0245:
0246: Expression linejoinExp = stroke.getLineJoin();
0247:
0248: return linejoinExp.toString();
0249: }
0250:
0251: /**
0252: * Grabs the linecap from the first LineSymbolizer.
0253: *
0254: * @param symbolizer Line symbolizer information.
0255: *
0256: * @return String of the line stroke's linecap, or null if unavailable.
0257: */
0258: public static String lineLinecap(LineSymbolizer symbolizer) {
0259: if (symbolizer == null) {
0260: return null;
0261: }
0262:
0263: Stroke stroke = symbolizer.getStroke();
0264:
0265: if (stroke == null) {
0266: return null;
0267: }
0268:
0269: Expression linecapExp = stroke.getLineCap();
0270:
0271: return linecapExp.toString();
0272: }
0273:
0274: /**
0275: * Grabs the dashes array from the first LineSymbolizer.
0276: *
0277: * @param symbolizer Line symbolizer information.
0278: *
0279: * @return float[] of the line dashes array, or null if unavailable.
0280: */
0281: public static float[] lineDash(LineSymbolizer symbolizer) {
0282: if (symbolizer == null) {
0283: return null;
0284: }
0285:
0286: Stroke stroke = symbolizer.getStroke();
0287:
0288: if (stroke == null) {
0289: return null;
0290: }
0291:
0292: float[] linedash = stroke.getDashArray();
0293:
0294: return linedash;
0295: }
0296:
0297: /**
0298: * Grabs the location of the first external graphic.
0299: *
0300: * @param style SLD style information.
0301: *
0302: * @return Location of the first external graphic, or null
0303: */
0304: public static URL pointGraphic(Style style) {
0305: PointSymbolizer point = pointSymbolizer(style);
0306:
0307: if (point == null) {
0308: return null;
0309: }
0310:
0311: Graphic graphic = point.getGraphic();
0312:
0313: if (graphic == null) {
0314: return null;
0315: }
0316:
0317: ExternalGraphic[] graphicList = graphic.getExternalGraphics();
0318:
0319: for (int i = 0; i < graphicList.length; i++) {
0320: ExternalGraphic externalGraphic = graphicList[i];
0321:
0322: if (externalGraphic == null) {
0323: continue;
0324: }
0325:
0326: URL location;
0327:
0328: try {
0329: location = externalGraphic.getLocation(); // Should check format is supported by SWT
0330:
0331: if (location != null) {
0332: return location;
0333: }
0334: } catch (MalformedURLException e) {
0335: // ignore, try the next one
0336: }
0337: }
0338:
0339: return null;
0340: }
0341:
0342: public static Mark pointMark(Style style) {
0343: if (style == null) {
0344: return null;
0345: }
0346:
0347: return mark(pointSymbolizer(style));
0348: }
0349:
0350: public static Mark mark(PointSymbolizer sym) {
0351: return mark(graphic(sym));
0352: }
0353:
0354: public static Mark mark(Graphic graphic) {
0355: if (graphic == null) {
0356: return null;
0357: }
0358:
0359: return ((graphic.getMarks() != null) && (graphic.getMarks().length > 0)) ? graphic
0360: .getMarks()[0]
0361: : null;
0362: }
0363:
0364: public static Graphic graphic(PointSymbolizer sym) {
0365: if (sym == null) {
0366: return null;
0367: }
0368:
0369: return sym.getGraphic();
0370: }
0371:
0372: /**
0373: * Grabs the size of the points graphic, if found.
0374: *
0375: * <p>
0376: * If you are using something fun like symbols you will need to do your
0377: * own thing.
0378: * </p>
0379: *
0380: * @param symbolizer Point symbolizer information.
0381: *
0382: * @return size of the graphic
0383: */
0384: public static int pointSize(PointSymbolizer symbolizer) {
0385: if (symbolizer == null) {
0386: return NOTFOUND;
0387: }
0388:
0389: Graphic g = symbolizer.getGraphic();
0390:
0391: if (g == null) {
0392: return NOTFOUND;
0393: }
0394:
0395: Expression exp = g.getSize();
0396: int size = intValue(exp);
0397:
0398: return size;
0399: }
0400:
0401: /**
0402: * Grabs the well known name of the first Mark that has one.
0403: *
0404: * <p>
0405: * If you are using something fun like symbols you will need to do your
0406: * own thing.
0407: * </p>
0408: *
0409: * @param symbolizer Point symbolizer information.
0410: *
0411: * @return well known name of the first Mark
0412: */
0413: public static String pointWellKnownName(PointSymbolizer symbolizer) {
0414: if (symbolizer == null) {
0415: return null;
0416: }
0417:
0418: Graphic g = symbolizer.getGraphic();
0419:
0420: if (g == null) {
0421: return null;
0422: }
0423:
0424: Mark[] markList = g.getMarks();
0425:
0426: for (int i = 0; i < markList.length; i++) {
0427: Mark mark = markList[i];
0428:
0429: if (mark == null) {
0430: continue;
0431: }
0432:
0433: String string = wellKnownName(mark);
0434:
0435: if (string == null) {
0436: continue;
0437: }
0438:
0439: return string;
0440: }
0441:
0442: return null;
0443: }
0444:
0445: public static String wellKnownName(Mark mark) {
0446: if (mark == null) {
0447: return null;
0448: }
0449:
0450: Expression exp = mark.getWellKnownName();
0451:
0452: if (exp == null) {
0453: return null;
0454: }
0455:
0456: String string = stringValue(exp);
0457:
0458: return string;
0459: }
0460:
0461: /**
0462: * Grabs the color from the first Mark.
0463: *
0464: * <p>
0465: * If you are using something fun like symbols you will need to do your
0466: * own thing.
0467: * </p>
0468: *
0469: * @param symbolizer Point symbolizer information.
0470: *
0471: * @return Color of the point's mark, or null if unavailable.
0472: */
0473: public static Color pointColor(PointSymbolizer symbolizer) {
0474: return color(symbolizer);
0475: }
0476:
0477: /**
0478: * Sets the Colour for the point symbolizer
0479: *
0480: * @param style
0481: * @param colour
0482: */
0483: public static void setPointColour(Style style, Color colour) {
0484: if (style == null) {
0485: return;
0486: }
0487:
0488: setPointColour(pointSymbolizer(style), colour);
0489: }
0490:
0491: /**
0492: * Sets the Colour for the given point symbolizer
0493: *
0494: * @param symbolizer
0495: * @param colour
0496: */
0497: public static void setPointColour(PointSymbolizer symbolizer,
0498: Color colour) {
0499: if (symbolizer == null) {
0500: return;
0501: }
0502:
0503: Graphic graphic = symbolizer.getGraphic();
0504:
0505: if (graphic == null) {
0506: graphic = builder.createGraphic();
0507: }
0508:
0509: Mark[] markList = graphic.getMarks();
0510:
0511: for (int i = 0; i < markList.length; i++) {
0512: Mark mark = markList[i];
0513:
0514: if (mark == null) {
0515: continue;
0516: }
0517:
0518: Stroke stroke = mark.getStroke();
0519:
0520: if (stroke == null) {
0521: stroke = builder.createStroke(Color.BLACK); //pretty black outline
0522: mark.setStroke(stroke);
0523: }
0524:
0525: if (colour != null) {
0526: Fill fill = mark.getFill();
0527:
0528: if (fill == null) {
0529: continue;
0530: }
0531:
0532: fill.setColor(builder.colorExpression(colour));
0533: }
0534: }
0535: }
0536:
0537: /**
0538: * Grabs the color from the first Mark.
0539: *
0540: * <p>
0541: * If you are using something fun like symbols you will need to do your
0542: * own thing.
0543: * </p>
0544: *
0545: * @param symbolizer Point symbolizer information.
0546: *
0547: * @return Color of the point's mark, or null if unavailable.
0548: */
0549: public static Color color(PointSymbolizer symbolizer) {
0550: if (symbolizer == null) {
0551: return null;
0552: }
0553:
0554: Graphic graphic = symbolizer.getGraphic();
0555:
0556: if (graphic == null) {
0557: return null;
0558: }
0559:
0560: Mark[] markList = graphic.getMarks();
0561:
0562: for (int i = 0; i < markList.length; i++) {
0563: Mark mark = markList[i];
0564:
0565: if (mark == null) {
0566: continue;
0567: }
0568:
0569: Stroke stroke = mark.getStroke();
0570:
0571: if (stroke == null) {
0572: continue;
0573: }
0574:
0575: Color colour = color(stroke.getColor());
0576:
0577: if (colour != null) {
0578: return colour;
0579: }
0580: }
0581:
0582: return null;
0583: }
0584:
0585: /**
0586: * Grabs the width of the first Mark with a Stroke that has a non-null
0587: * width.
0588: *
0589: * <p>
0590: * If you are using something fun like symbols you will need to do your
0591: * own thing.
0592: * </p>
0593: *
0594: * @param symbolizer Point symbolizer information.
0595: *
0596: * @return width of the points border
0597: */
0598: public static int pointWidth(PointSymbolizer symbolizer) {
0599: if (symbolizer == null) {
0600: return NOTFOUND;
0601: }
0602:
0603: Graphic g = symbolizer.getGraphic();
0604:
0605: if (g == null) {
0606: return NOTFOUND;
0607: }
0608:
0609: Mark[] markList = g.getMarks();
0610:
0611: for (int i = 0; i < markList.length; i++) {
0612: Mark mark = markList[i];
0613:
0614: if (mark == null) {
0615: continue;
0616: }
0617:
0618: Stroke stroke = mark.getStroke();
0619:
0620: if (stroke == null) {
0621: continue;
0622: }
0623:
0624: Expression exp = stroke.getWidth();
0625:
0626: if (exp == null) {
0627: continue;
0628: }
0629:
0630: int width = intValue(exp);
0631:
0632: if (width == NOTFOUND) {
0633: continue;
0634: }
0635:
0636: return width;
0637: }
0638:
0639: return NOTFOUND;
0640: }
0641:
0642: /**
0643: * Grabs the point border opacity from the first PointSymbolizer.
0644: *
0645: * <p>
0646: * If you are using something fun like rules you will need to do your own
0647: * thing.
0648: * </p>
0649: *
0650: * @param symbolizer Point symbolizer information.
0651: *
0652: * @return double of the point's border opacity, or NaN if unavailable.
0653: */
0654: public static double pointBorderOpacity(PointSymbolizer symbolizer) {
0655: if (symbolizer == null) {
0656: return Double.NaN;
0657: }
0658:
0659: Graphic graphic = symbolizer.getGraphic();
0660:
0661: if (graphic == null) {
0662: return Double.NaN;
0663: }
0664:
0665: Mark[] markList = graphic.getMarks();
0666:
0667: for (int i = 0; i < markList.length; i++) {
0668: Mark mark = markList[i];
0669:
0670: if (mark == null) {
0671: continue;
0672: }
0673:
0674: Stroke stroke = mark.getStroke();
0675:
0676: if (stroke == null) {
0677: continue;
0678: }
0679:
0680: Expression opacityExp = stroke.getOpacity();
0681:
0682: return Double.parseDouble(opacityExp.toString());
0683: }
0684:
0685: return Double.NaN;
0686: }
0687:
0688: /**
0689: * Grabs the point opacity from the first PointSymbolizer.
0690: *
0691: * <p>
0692: * If you are using something fun like rules you will need to do your own
0693: * thing.
0694: * </p>
0695: *
0696: * @param symbolizer Point symbolizer information.
0697: *
0698: * @return double of the point's opacity, or NaN if unavailable.
0699: */
0700: public static double pointOpacity(PointSymbolizer symbolizer) {
0701: if (symbolizer == null) {
0702: return Double.NaN;
0703: }
0704:
0705: Graphic graphic = symbolizer.getGraphic();
0706:
0707: if (graphic == null) {
0708: return Double.NaN;
0709: }
0710:
0711: Mark[] markList = graphic.getMarks();
0712:
0713: for (int i = 0; i < markList.length; i++) {
0714: Mark mark = markList[i];
0715:
0716: if (mark == null) {
0717: continue;
0718: }
0719:
0720: Fill fill = mark.getFill();
0721:
0722: if (fill == null) {
0723: continue;
0724: }
0725:
0726: Expression opacityExp = fill.getOpacity();
0727:
0728: return Double.parseDouble(opacityExp.toString());
0729: }
0730:
0731: return Double.NaN;
0732: }
0733:
0734: /**
0735: * Grabs the fill from the first Mark.
0736: *
0737: * <p>
0738: * If you are using something fun like symbols you will need to do your
0739: * own thing.
0740: * </p>
0741: *
0742: * @param symbolizer Point symbolizer information.
0743: *
0744: * @return Color of the point's fill, or null if unavailable.
0745: */
0746: public static Color pointFill(PointSymbolizer symbolizer) {
0747: if (symbolizer == null) {
0748: return null;
0749: }
0750:
0751: Graphic graphic = symbolizer.getGraphic();
0752:
0753: if (graphic == null) {
0754: return null;
0755: }
0756:
0757: Mark[] markList = graphic.getMarks();
0758:
0759: for (int i = 0; i < markList.length; i++) {
0760: Mark mark = markList[i];
0761:
0762: if (mark == null) {
0763: continue;
0764: }
0765:
0766: Fill fill = mark.getFill();
0767:
0768: if (fill == null) {
0769: continue;
0770: }
0771:
0772: Color colour = color(fill.getColor());
0773:
0774: if (colour != null) {
0775: return colour;
0776: }
0777: }
0778:
0779: return null;
0780: }
0781:
0782: /**
0783: * Grabs the color from the first PolygonSymbolizer.
0784: *
0785: * <p>
0786: * If you are using something fun like rules you will need to do your own
0787: * thing.
0788: * </p>
0789: *
0790: * @param symbolizer Polygon symbolizer information.
0791: *
0792: * @return Color of the polygon's stroke, or null if unavailable.
0793: */
0794: public static int polyWidth(PolygonSymbolizer symbolizer) {
0795: if (symbolizer == null) {
0796: return NOTFOUND;
0797: }
0798:
0799: Stroke stroke = symbolizer.getStroke();
0800:
0801: if (stroke == null) {
0802: return NOTFOUND;
0803: }
0804:
0805: int width = intValue(stroke.getWidth());
0806:
0807: return width;
0808: }
0809:
0810: /**
0811: * Grabs the color from the first PolygonSymbolizer.
0812: *
0813: * <p>
0814: * If you are using something fun like rules you will need to do your own
0815: * thing.
0816: * </p>
0817: *
0818: * @param symbolizer Polygon symbolizer information.
0819: *
0820: * @return Color of the polygon's stroke, or null if unavailable.
0821: */
0822: public static Color polyColor(PolygonSymbolizer symbolizer) {
0823: if (symbolizer == null) {
0824: return null;
0825: }
0826:
0827: Stroke stroke = symbolizer.getStroke();
0828:
0829: if (stroke == null) {
0830: return null;
0831: }
0832:
0833: Color colour = color(stroke.getColor());
0834:
0835: if (colour != null) {
0836: return colour;
0837: }
0838:
0839: return null;
0840: }
0841:
0842: /**
0843: * Sets the colour for a polygon symbolizer
0844: *
0845: * @param style
0846: * @param colour
0847: */
0848: public static void setPolyColour(Style style, Color colour) {
0849: if (style == null) {
0850: return;
0851: }
0852:
0853: setPolyColour(polySymbolizer(style), colour);
0854: }
0855:
0856: /**
0857: * Sets the Colour for the given polygon symbolizer
0858: *
0859: * @param symbolizer
0860: * @param colour
0861: */
0862: public static void setPolyColour(PolygonSymbolizer symbolizer,
0863: Color colour) {
0864: if (symbolizer == null) {
0865: return;
0866: }
0867:
0868: Stroke stroke = symbolizer.getStroke();
0869:
0870: if (stroke == null) {
0871: stroke = builder.createStroke(colour);
0872: symbolizer.setStroke(stroke);
0873: }
0874:
0875: if (colour != null) {
0876: stroke.setColor(builder.colorExpression(colour));
0877:
0878: Fill fill = symbolizer.getFill();
0879:
0880: if (fill != null) {
0881: fill.setColor(builder.colorExpression(colour));
0882: }
0883: }
0884: }
0885:
0886: /**
0887: * Grabs the fill from the first PolygonSymbolizer.
0888: *
0889: * <p>
0890: * If you are using something fun like rules you will need to do your own
0891: * thing.
0892: * </p>
0893: *
0894: * @param symbolizer Polygon symbolizer information.
0895: *
0896: * @return Color of the polygon's fill, or null if unavailable.
0897: */
0898: public static Color polyFill(PolygonSymbolizer symbolizer) {
0899: if (symbolizer == null) {
0900: return null;
0901: }
0902:
0903: Fill fill = symbolizer.getFill();
0904:
0905: if (fill == null) {
0906: return null;
0907: }
0908:
0909: Color colour = color(fill.getColor());
0910:
0911: if (colour != null) {
0912: return colour;
0913: }
0914:
0915: return null;
0916: }
0917:
0918: /**
0919: * Grabs the border opacity from the first PolygonSymbolizer.
0920: *
0921: * <p>
0922: * If you are using something fun like rules you will need to do your own
0923: * thing.
0924: * </p>
0925: *
0926: * @param symbolizer Polygon symbolizer information.
0927: *
0928: * @return double of the polygon's border opacity, or NaN if unavailable.
0929: */
0930: public static double polyBorderOpacity(PolygonSymbolizer symbolizer) {
0931: if (symbolizer == null) {
0932: return Double.NaN;
0933: }
0934:
0935: Stroke stroke = symbolizer.getStroke();
0936:
0937: if (stroke == null) {
0938: return Double.NaN;
0939: }
0940:
0941: Expression opacityExp = stroke.getOpacity();
0942: double opacity = Double.parseDouble(opacityExp.toString());
0943:
0944: return opacity;
0945: }
0946:
0947: /**
0948: * Grabs the fill opacity from the first PolygonSymbolizer.
0949: *
0950: * <p>
0951: * If you are using something fun like rules you will need to do your own
0952: * thing.
0953: * </p>
0954: *
0955: * @param symbolizer Polygon symbolizer information.
0956: *
0957: * @return double of the polygon's fill opacity, or NaN if unavailable.
0958: */
0959: public static double polyFillOpacity(PolygonSymbolizer symbolizer) {
0960: if (symbolizer == null) {
0961: return Double.NaN;
0962: }
0963:
0964: Fill fill = symbolizer.getFill();
0965:
0966: return opacity(fill);
0967: }
0968:
0969: public static double opacity(Fill fill) {
0970: if (fill == null) {
0971: return Double.NaN;
0972: }
0973:
0974: Expression opacityExp = fill.getOpacity();
0975: double opacity = Double.parseDouble(opacityExp.toString());
0976:
0977: return opacity;
0978: }
0979:
0980: /**
0981: * Grabs the opacity from the first RasterSymbolizer.
0982: *
0983: * <p>
0984: * If you are using something fun like rules you will need to do your own
0985: * thing.
0986: * </p>
0987: *
0988: * @param symbolizer Raster symbolizer information.
0989: *
0990: * @return opacity of the first RasterSymbolizer
0991: */
0992: public static double rasterOpacity(RasterSymbolizer symbolizer) {
0993: if (symbolizer == null) {
0994: return Double.NaN;
0995: }
0996:
0997: return doubleValue(symbolizer.getOpacity());
0998: }
0999:
1000: public static double rasterOpacity(Style style) {
1001: return rasterOpacity(rasterSymbolizer(style));
1002: }
1003:
1004: /**
1005: * Retrieve the first TextSymbolizer from the provided Style.
1006: *
1007: * @param fts SLD featureTypeStyle information.
1008: *
1009: * @return TextSymbolizer, or null if not found.
1010: */
1011: public static TextSymbolizer textSymbolizer(FeatureTypeStyle fts) {
1012: return (TextSymbolizer) symbolizer(fts, TextSymbolizer.class);
1013: }
1014:
1015: /**
1016: * Retrieve the first TextSymbolizer from the provided Style.
1017: *
1018: * @param style SLD style information.
1019: *
1020: * @return TextSymbolizer, or null if not found.
1021: */
1022: public static TextSymbolizer textSymbolizer(Style style) {
1023: return (TextSymbolizer) symbolizer(style, TextSymbolizer.class);
1024: }
1025:
1026: /**
1027: * Grabs the label from the first TextSymbolizer.
1028: *
1029: * <p>
1030: * If you are using something fun like symbols you will need to do your
1031: * own thing.
1032: * </p>
1033: *
1034: * @param symbolizer Text symbolizer information.
1035: *
1036: * @return Expression of the label's text, or null if unavailable.
1037: */
1038: public static Expression textLabel(TextSymbolizer symbolizer) {
1039: if (symbolizer == null) {
1040: return null;
1041: }
1042:
1043: Expression exp = symbolizer.getLabel();
1044:
1045: if (exp == null) {
1046: return null;
1047: }
1048:
1049: return exp;
1050: }
1051:
1052: public static String textLabelString(TextSymbolizer sym) {
1053: Expression exp = textLabel(sym);
1054:
1055: return (exp == null) ? null : exp.toString();
1056: }
1057:
1058: /**
1059: * Grabs the fontFill from the first TextSymbolizer.
1060: *
1061: * <p>
1062: * If you are using something fun like symbols you will need to do your
1063: * own thing.
1064: * </p>
1065: *
1066: * @param symbolizer Text symbolizer information.
1067: *
1068: * @return Color of the font's fill, or null if unavailable.
1069: */
1070: public static Color textFontFill(TextSymbolizer symbolizer) {
1071: if (symbolizer == null) {
1072: return null;
1073: }
1074:
1075: Fill fill = symbolizer.getFill();
1076:
1077: if (fill == null) {
1078: return null;
1079: }
1080:
1081: Color colour = color(fill.getColor());
1082:
1083: if (colour != null) {
1084: return colour;
1085: }
1086:
1087: return null;
1088: }
1089:
1090: public static Font font(TextSymbolizer symbolizer) {
1091: if (symbolizer == null) {
1092: return null;
1093: }
1094:
1095: Font[] font = symbolizer.getFonts();
1096:
1097: if ((font == null) || (font[0] == null)) {
1098: return null;
1099: }
1100:
1101: return font[0];
1102: }
1103:
1104: /**
1105: * Grabs the haloFill from the first TextSymbolizer.
1106: *
1107: * <p>
1108: * If you are using something fun like symbols you will need to do your
1109: * own thing.
1110: * </p>
1111: *
1112: * @param symbolizer Text symbolizer information.
1113: *
1114: * @return Color of the halo's fill, or null if unavailable.
1115: */
1116: public static Color textHaloFill(TextSymbolizer symbolizer) {
1117: Halo halo = symbolizer.getHalo();
1118:
1119: if (halo == null) {
1120: return null;
1121: }
1122:
1123: Fill fill = halo.getFill();
1124:
1125: if (fill == null) {
1126: return null;
1127: }
1128:
1129: Color colour = color(fill.getColor());
1130:
1131: if (colour != null) {
1132: return colour;
1133: }
1134:
1135: return null;
1136: }
1137:
1138: /**
1139: * Grabs the halo width from the first TextSymbolizer.
1140: *
1141: * <p>
1142: * If you are using something fun like symbols you will need to do your
1143: * own thing.
1144: * </p>
1145: *
1146: * @param symbolizer Text symbolizer information.
1147: *
1148: * @return float of the halo's width, or null if unavailable.
1149: */
1150: public static int textHaloWidth(TextSymbolizer symbolizer) {
1151: Halo halo = symbolizer.getHalo();
1152:
1153: if (halo == null) {
1154: return 0;
1155: }
1156:
1157: Expression exp = halo.getRadius();
1158:
1159: if (exp == null) {
1160: return 0;
1161: }
1162:
1163: int width = (int) Float.parseFloat(exp.toString());
1164:
1165: if (width != 0) {
1166: return width;
1167: }
1168:
1169: return 0;
1170: }
1171:
1172: /**
1173: * Grabs the halo opacity from the first TextSymbolizer.
1174: *
1175: * <p>
1176: * If you are using something fun like symbols you will need to do your
1177: * own thing.
1178: * </p>
1179: *
1180: * @param symbolizer Text symbolizer information.
1181: *
1182: * @return double of the halo's opacity, or NaN if unavailable.
1183: */
1184: public static double textHaloOpacity(TextSymbolizer symbolizer) {
1185: if (symbolizer == null) {
1186: return Double.NaN;
1187: }
1188:
1189: Halo halo = symbolizer.getHalo();
1190:
1191: if (halo == null) {
1192: return Double.NaN;
1193: }
1194:
1195: Fill fill = halo.getFill();
1196:
1197: if (fill == null) {
1198: return Double.NaN;
1199: }
1200:
1201: Expression opacityExp = fill.getOpacity();
1202: double opacity = Double.parseDouble(opacityExp.toString());
1203:
1204: return opacity;
1205: }
1206:
1207: /**
1208: * This method is here for backwards compatability.
1209: *
1210: * @param expr DOCUMENT ME!
1211: * @param TYPE DOCUMENT ME!
1212: *
1213: * @return DOCUMENT ME!
1214: *
1215: * @see org.geotools.filter.Filters#value(Expression, Class)
1216: * @deprecated
1217: */
1218: public static Object value(Expression expr, Class TYPE) {
1219: return Filters.asType(expr, TYPE);
1220: }
1221:
1222: /**
1223: * Navigate through the expression finding the first mentioned Color.
1224: *
1225: * <p>
1226: * If you have a specific Feature in mind please use:
1227: * <pre><code>
1228: * Object value = expr.getValue( feature );
1229: * return value instanceof Color ? (Color) value : null;
1230: * </code></pre>
1231: * </p>
1232: *
1233: * @param expr
1234: *
1235: * @return First available color, or null.
1236: */
1237: public static Color color(Expression expr) {
1238: if (expr == null) {
1239: return null;
1240: }
1241:
1242: Color color = (Color) value(expr, Color.class);
1243:
1244: if (color != null) {
1245: return color;
1246: }
1247:
1248: String rgba = (String) value(expr, String.class);
1249:
1250: try {
1251: color = Color.decode(rgba);
1252:
1253: if (color != null) {
1254: return color;
1255: }
1256: } catch (NumberFormatException badRGB) {
1257: // unavailable
1258: }
1259:
1260: return null;
1261: }
1262:
1263: /**
1264: * This method is here for backward compatability.
1265: *
1266: * @param expr DOCUMENT ME!
1267: *
1268: * @return DOCUMENT ME!
1269: *
1270: * @see Filters#intValue(Expression)
1271: * @deprecated
1272: */
1273: public static int intValue(Expression expr) {
1274: return Filters.asInt(expr);
1275: }
1276:
1277: /**
1278: * This method is here for backward compatability.
1279: *
1280: * @param expr DOCUMENT ME!
1281: *
1282: * @return DOCUMENT ME!
1283: *
1284: * @see Filters#stringValue(Expression)
1285: * @deprecated
1286: */
1287: public static String stringValue(Expression expr) {
1288: return Filters.asString(expr);
1289: }
1290:
1291: /**
1292: * This method is here for backward compatability.
1293: *
1294: * @param expr DOCUMENT ME!
1295: *
1296: * @return DOCUMENT ME!
1297: *
1298: * @see Filters#doubleValue(Expression)
1299: * @deprecated
1300: */
1301: public static double doubleValue(Expression expr) {
1302: return Filters.asDouble(expr);
1303: }
1304:
1305: /**
1306: * This method is here for backward compatability.
1307: *
1308: * @param expr DOCUMENT ME!
1309: *
1310: * @return DOCUMENT ME!
1311: *
1312: * @see Filters#number(Expression)
1313: * @deprecated
1314: */
1315: public static Number number(Expression expr) {
1316: return (Number) Filters.asType(expr, Number.class);
1317: }
1318:
1319: /**
1320: * Retrieve the first RasterSymbolizer from the provided Style.
1321: *
1322: * @param fts SLD featureTypeStyle information.
1323: *
1324: * @return RasterSymbolizer, or null if not found.
1325: */
1326: public static RasterSymbolizer rasterSymbolizer(FeatureTypeStyle fts) {
1327: return (RasterSymbolizer) symbolizer(fts,
1328: RasterSymbolizer.class);
1329: }
1330:
1331: /**
1332: * Retrieve the first RasterSymbolizer from the provided Style.
1333: *
1334: * @param style SLD style information.
1335: *
1336: * @return RasterSymbolizer, or null if not found.
1337: */
1338: public static RasterSymbolizer rasterSymbolizer(Style style) {
1339: return (RasterSymbolizer) symbolizer(style,
1340: RasterSymbolizer.class);
1341: }
1342:
1343: /**
1344: * Retrieve the first LineSymbolizer from the provided Style.
1345: *
1346: * @param fts SLD featureTypeStyle information.
1347: *
1348: * @return LineSymbolizer, or null if not found.
1349: */
1350: public static LineSymbolizer lineSymbolizer(FeatureTypeStyle fts) {
1351: return (LineSymbolizer) symbolizer(fts, LineSymbolizer.class);
1352: }
1353:
1354: /**
1355: * Retrieve the first LineSymbolizer from the provided Style.
1356: *
1357: * @param style SLD style information.
1358: *
1359: * @return LineSymbolizer, or null if not found.
1360: */
1361: public static LineSymbolizer lineSymbolizer(Style style) {
1362: return (LineSymbolizer) symbolizer(style, LineSymbolizer.class);
1363: }
1364:
1365: public static Stroke stroke(LineSymbolizer sym) {
1366: if (sym == null) {
1367: return null;
1368: }
1369:
1370: return sym.getStroke();
1371: }
1372:
1373: public static Stroke stroke(PolygonSymbolizer sym) {
1374: if (sym == null) {
1375: return null;
1376: }
1377:
1378: return sym.getStroke();
1379: }
1380:
1381: public static Stroke stroke(PointSymbolizer sym) {
1382: Mark mark = mark(sym);
1383:
1384: return (mark == null) ? null : mark.getStroke();
1385: }
1386:
1387: public static Fill fill(PolygonSymbolizer sym) {
1388: if (sym == null) {
1389: return null;
1390: }
1391:
1392: return sym.getFill();
1393: }
1394:
1395: public static Fill fill(PointSymbolizer sym) {
1396: Mark mark = mark(sym);
1397:
1398: return (mark == null) ? null : mark.getFill();
1399: }
1400:
1401: /**
1402: * Retrieve the first PointSymbolizer from the provided FeatureTypeStyle.
1403: *
1404: * @param fts SLD featureTypeStyle information.
1405: *
1406: * @return PointSymbolizer, or null if not found.
1407: */
1408: public static PointSymbolizer pointSymbolizer(FeatureTypeStyle fts) {
1409: return (PointSymbolizer) symbolizer(fts, PointSymbolizer.class);
1410: }
1411:
1412: /**
1413: * Retrieve the first PointSymbolizer from the provided Style.
1414: *
1415: * @param style SLD style information.
1416: *
1417: * @return PointSymbolizer, or null if not found.
1418: */
1419: public static PointSymbolizer pointSymbolizer(Style style) {
1420: return (PointSymbolizer) symbolizer(style,
1421: PointSymbolizer.class);
1422: }
1423:
1424: /**
1425: * Retrieve the first PolygonSymbolizer from the provided Style.
1426: *
1427: * @param fts SLD featureTypeStyle information.
1428: *
1429: * @return PolygonSymbolizer, or null if not found.
1430: */
1431: public static PolygonSymbolizer polySymbolizer(FeatureTypeStyle fts) {
1432: return (PolygonSymbolizer) symbolizer(fts,
1433: PolygonSymbolizer.class);
1434: }
1435:
1436: /**
1437: * Retrieve the first PolygonSymbolizer from the provided Style.
1438: *
1439: * @param style SLD style information.
1440: *
1441: * @return PolygonSymbolizer, or null if not found.
1442: */
1443: public static PolygonSymbolizer polySymbolizer(Style style) {
1444: return (PolygonSymbolizer) symbolizer(style,
1445: PolygonSymbolizer.class);
1446: }
1447:
1448: /**
1449: * Returns the feature type style in the style which matched a particular
1450: * name.
1451: *
1452: * @param style The style in question.
1453: * @param type The feature type must be non-null.
1454: *
1455: * @return Teh FeatureTypeStyle object if it exists, otherwise false.
1456: */
1457: public static FeatureTypeStyle featureTypeStyle(Style style,
1458: FeatureType type) {
1459: if (style == null) {
1460: return null;
1461: }
1462:
1463: if ((type == null) || (type.getTypeName() == null)) {
1464: return null;
1465: }
1466:
1467: FeatureTypeStyle[] styles = style.getFeatureTypeStyles();
1468:
1469: if (styles == null) {
1470: return null;
1471: }
1472:
1473: for (int i = 0; i < styles.length; i++) {
1474: FeatureTypeStyle ftStyle = styles[i];
1475:
1476: if (type.getTypeName().equals(ftStyle.getName())) {
1477: return ftStyle;
1478: }
1479: }
1480:
1481: return null;
1482: }
1483:
1484: /**
1485: * Returns the first style object which matches a given schema.
1486: *
1487: * @param styles Array of style objects.
1488: * @param schema Feature schema.
1489: *
1490: * @return The first object to match the feature type, otherwise null if no
1491: * match.
1492: */
1493: public static Style matchingStyle(Style[] styles, FeatureType schema) {
1494: if ((styles == null) || (styles.length == 0)) {
1495: return null;
1496: }
1497:
1498: for (int i = 0; i < styles.length; i++) {
1499: Style style = styles[i];
1500:
1501: if (featureTypeStyle(style, schema) != null) {
1502: return style;
1503: }
1504: }
1505:
1506: return null;
1507: }
1508:
1509: /**
1510: * Retrieve the first SYMBOLIZER from the provided Style.
1511: *
1512: * @param style SLD style information.
1513: * @param SYMBOLIZER LineSymbolizer.class, PointSymbolizer.class,
1514: * PolygonSymbolizer.class, RasterSymbolizer.class, or TextSymbolizer.class
1515: *
1516: * @return symbolizer instance from style, or null if not found.
1517: */
1518: protected static Symbolizer symbolizer(Style style,
1519: final Class SYMBOLIZER) {
1520: if (style == null) {
1521: return null;
1522: }
1523:
1524: FeatureTypeStyle[] ftStyleList = style.getFeatureTypeStyles();
1525:
1526: if (ftStyleList == null) {
1527: return null;
1528: }
1529:
1530: for (int i = 0; i < ftStyleList.length; i++) {
1531: FeatureTypeStyle ftStyle = ftStyleList[i];
1532: Symbolizer result = symbolizer(ftStyle, SYMBOLIZER);
1533: if (result != null)
1534: return result;
1535: }
1536: return null;
1537: }
1538:
1539: /**
1540: * Retrieve the first SYMBOLIZER from the provided FeatureTypeStyle.
1541: *
1542: * @param fts the FeatureTypeStyle SLD style information.
1543: * @param SYMBOLIZER LineSymbolizer.class, PointSymbolizer.class,
1544: * PolygonSymbolizer.class, RasterSymbolizer.class,
1545: * or TextSymbolizer.class
1546: *
1547: * @return symbolizer instance from fts, or null if not found.
1548: */
1549: protected static Symbolizer symbolizer(FeatureTypeStyle fts,
1550: final Class SYMBOLIZER) {
1551: if (fts == null) {
1552: return null;
1553: }
1554:
1555: Rule[] ruleList = fts.getRules();
1556: if (ruleList == null) {
1557: return null;
1558: }
1559:
1560: RULE: for (int j = 0; j < ruleList.length; j++) {
1561: Rule rule = ruleList[j];
1562: Symbolizer[] symbolizerList = rule.getSymbolizers();
1563:
1564: if (symbolizerList == null) {
1565: continue RULE;
1566: }
1567:
1568: SYMBOLIZER: for (int k = 0; k < symbolizerList.length; k++) {
1569: Symbolizer symbolizer = symbolizerList[k];
1570:
1571: if (symbolizer == null) {
1572: continue SYMBOLIZER;
1573: }
1574:
1575: if (SYMBOLIZER.isInstance(symbolizer)) {
1576: return symbolizer;
1577: }
1578: }
1579: }
1580: return null;
1581: }
1582:
1583: public static String colorToHex(Color c) {
1584: return "#" + Integer.toHexString(c.getRGB() & 0x00ffffff);
1585: }
1586:
1587: public static Style[] styles(StyledLayerDescriptor sld) {
1588: StyledLayer[] layers = sld.getStyledLayers();
1589: List styles = new ArrayList();
1590: for (int i = 0; i < layers.length; i++) {
1591: if (layers[i] instanceof UserLayer) {
1592: UserLayer layer = (UserLayer) layers[i];
1593: styles.addAll(toList(layer.getUserStyles()));
1594: } else if (layers[i] instanceof NamedLayer) {
1595: NamedLayer layer = (NamedLayer) layers[i];
1596: styles.addAll(toList(layer.getStyles()));
1597: }
1598: }
1599: return (Style[]) styles.toArray(new Style[styles.size()]);
1600: }
1601:
1602: public static FeatureTypeStyle[] featureTypeStyles(
1603: StyledLayerDescriptor sld) {
1604: Style[] style = styles(sld);
1605: List fts = new ArrayList();
1606: for (int i = 0; i < style.length; i++) {
1607: fts.addAll(toList(style[i].getFeatureTypeStyles()));
1608: }
1609: return (FeatureTypeStyle[]) fts
1610: .toArray(new FeatureTypeStyle[fts.size()]);
1611: }
1612:
1613: public static FeatureTypeStyle featureTypeStyle(
1614: StyledLayerDescriptor sld, FeatureType type) {
1615: //alternatively, we could use a StyleVisitor here
1616: Style[] styles = styles(sld);
1617: for (int i = 0; i < styles.length; i++) {
1618: FeatureTypeStyle[] fts = styles[i].getFeatureTypeStyles();
1619: for (int j = 0; j < fts.length; j++) {
1620: if (type.getTypeName().equals(fts[j].getName())) {
1621: return fts[j];
1622: }
1623: }
1624: }
1625: return null;
1626: }
1627:
1628: private static List toList(Object[] array) {
1629: List list = new ArrayList();
1630: for (int i = 0; i < array.length; i++) {
1631: list.add(array[i]);
1632: }
1633: return list;
1634: }
1635:
1636: public static Style defaultStyle(StyledLayerDescriptor sld) {
1637: Style[] style = styles(sld);
1638: for (int i = 0; i < style.length; i++) {
1639: if (style[i].isDefault()) {
1640: return style[i];
1641: }
1642: }
1643: //no default, so just grab the first one
1644: return style[0];
1645: }
1646:
1647: /**
1648: * Climbs the style hierarchy until null or an SLD is found.
1649: *
1650: * @param gtComponent object
1651: *
1652: * @return SLD
1653: */
1654: public static StyledLayerDescriptor styledLayerDescriptor(
1655: Object gtComponent) {
1656: if (!(gtComponent instanceof GTComponent))
1657: return null;
1658: GTComponent component = (GTComponent) gtComponent;
1659: while (component.getNote().getParent() != GTRoot.NO_PARENT) {
1660: component = component.getNote().getParent();
1661: if (component instanceof StyledLayerDescriptor) {
1662: return (StyledLayerDescriptor) component;
1663: }
1664: }
1665: return null;
1666: }
1667:
1668: public static Filter[] filters(Rule[] rule) {
1669: Filter[] filter = new Filter[rule.length];
1670: for (int i = 0; i < rule.length; i++) {
1671: filter[i] = rule[0].getFilter();
1672: }
1673: return filter;
1674: }
1675:
1676: public static Filter[] filters(Style style) {
1677: Rule[] rule = rules(style);
1678: return filters(rule);
1679: }
1680:
1681: public static Rule[] rules(Style style) {
1682: Set ruleSet = new HashSet();
1683: FeatureTypeStyle[] fts = style.getFeatureTypeStyles();
1684: for (int i = 0; i < fts.length; i++) {
1685: Rule[] ftsRules = fts[i].getRules();
1686: for (int j = 0; j < ftsRules.length; j++) {
1687: ruleSet.add(ftsRules[j]);
1688: }
1689: }
1690: if (ruleSet.size() > 0) {
1691: return toRuleArray(ruleSet.toArray());
1692: } else {
1693: return new Rule[0];
1694: }
1695: }
1696:
1697: public static Symbolizer[] symbolizers(Style style) {
1698: Set symbolizers = new HashSet();
1699: Rule[] rule = rules(style);
1700: for (int i = 0; i < rule.length; i++) {
1701: Symbolizer[] symb = rule[i].getSymbolizers();
1702: for (int j = 0; j < symb.length; j++) {
1703: symbolizers.add(symb[j]);
1704: }
1705: }
1706: if (symbolizers.size() > 0) {
1707: return toSymbolizerArray(symbolizers.toArray());
1708: } else {
1709: return new Symbolizer[0];
1710: }
1711: }
1712:
1713: public static Symbolizer[] symbolizers(Rule rule) {
1714: Set symbolizers = new HashSet();
1715: Symbolizer[] symb = rule.getSymbolizers();
1716: for (int j = 0; j < symb.length; j++) {
1717: symbolizers.add(symb[j]);
1718: }
1719: if (symbolizers.size() > 0) {
1720: return toSymbolizerArray(symbolizers.toArray());
1721: } else {
1722: return new Symbolizer[0];
1723: }
1724: }
1725:
1726: public static String[] colors(Style style) {
1727: Set colorSet = new HashSet();
1728: Rule[] rule = rules(style);
1729: for (int i = 0; i < rule.length; i++) {
1730: String[] color = colors(rule[i]);
1731: for (int j = 0; j < color.length; j++) {
1732: colorSet.add(color[j]);
1733: }
1734: }
1735: if (colorSet.size() > 0) {
1736: return toStringArray(colorSet.toArray());
1737: } else {
1738: return new String[0];
1739: }
1740: }
1741:
1742: public static String[] colors(Rule rule) {
1743: Set colorSet = new HashSet();
1744: Symbolizer[] symbolizer = rule.getSymbolizers();
1745: for (int i = 0; i < symbolizer.length; i++) {
1746: if (symbolizer[i] instanceof PolygonSymbolizer) {
1747: PolygonSymbolizer symb = (PolygonSymbolizer) symbolizer[i];
1748: colorSet.add(symb.getFill().getColor().toString());
1749: } else if (symbolizer[i] instanceof LineSymbolizer) {
1750: LineSymbolizer symb = (LineSymbolizer) symbolizer[i];
1751: colorSet.add(symb.getStroke().getColor().toString());
1752: } else if (symbolizer[i] instanceof PointSymbolizer) {
1753: PointSymbolizer symb = (PointSymbolizer) symbolizer[i];
1754: colorSet.add(symb.getGraphic().getMarks()[0].getFill()
1755: .getColor().toString());
1756: }
1757: }
1758: if (colorSet.size() > 0) {
1759: return toStringArray(colorSet.toArray());
1760: } else {
1761: return new String[0];
1762: }
1763: }
1764:
1765: private static String[] toStringArray(Object[] object) {
1766: String[] result = new String[object.length];
1767: for (int i = 0; i < object.length; i++) {
1768: result[i] = (String) object[i];
1769: }
1770: return result;
1771: }
1772:
1773: private static Rule[] toRuleArray(Object[] object) {
1774: Rule[] result = new Rule[object.length];
1775: for (int i = 0; i < object.length; i++) {
1776: result[i] = (Rule) object[i];
1777: }
1778: return result;
1779: }
1780:
1781: private static Symbolizer[] toSymbolizerArray(Object[] object) {
1782: Symbolizer[] result = new Symbolizer[object.length];
1783: for (int i = 0; i < object.length; i++) {
1784: result[i] = (Symbolizer) object[i];
1785: }
1786: return result;
1787: }
1788:
1789: /**
1790: * Converts a java.awt.Color into an HTML Colour
1791: *
1792: * @param color
1793: * @return HTML Color (fill) in hex #RRGGBB
1794: */
1795: public static String toHTMLColor(Color color) {
1796: String red = "0" + Integer.toHexString(color.getRed());
1797: red = red.substring(red.length() - 2);
1798: String grn = "0" + Integer.toHexString(color.getGreen());
1799: grn = grn.substring(grn.length() - 2);
1800: String blu = "0" + Integer.toHexString(color.getBlue());
1801: blu = blu.substring(blu.length() - 2);
1802: return ("#" + red + grn + blu).toUpperCase();
1803: }
1804:
1805: public static Color toColor(String htmlColor) {
1806: return new Color(Integer.parseInt(htmlColor.substring(1), 16));
1807: }
1808:
1809: }
|