001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/graphics/sld/StyleUtils.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.graphics.sld;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Map;
049:
050: import org.deegree.io.datastore.PropertyPathResolvingException;
051: import org.deegree.model.filterencoding.Expression;
052: import org.deegree.model.filterencoding.Filter;
053: import org.deegree.model.filterencoding.FilterTools;
054: import org.deegree.ogcbase.PropertyPath;
055:
056: /**
057: * Collects all property names used by a list of styles. E.g. this can be used to optimze GetFeature
058: * requests from a WMS against a WFS.
059: *
060: * @version $Revision: 10593 $
061: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
062: * @author last edited by: $Author: aschmitz $
063: *
064: * @version 1.0. $Revision: 10593 $, $Date: 2008-03-17 06:35:32 -0700 (Mon, 17 Mar 2008) $
065: *
066: * @since 2.0
067: */
068: public class StyleUtils {
069:
070: /**
071: * @return a list of all
072: *
073: * @param styles
074: * @param scaleDen
075: * @throws PropertyPathResolvingException
076: */
077: public static List<PropertyPath> extractRequiredProperties(
078: List<UserStyle> styles, double scaleDen)
079: throws PropertyPathResolvingException {
080: List<PropertyPath> pp = new ArrayList<PropertyPath>();
081:
082: for (int i = 0; i < styles.size(); i++) {
083: FeatureTypeStyle[] fts = styles.get(i)
084: .getFeatureTypeStyles();
085: for (int j = 0; j < fts.length; j++) {
086: Rule[] rules = fts[j].getRules();
087: for (int k = 0; k < rules.length; k++) {
088: double minScale = rules[k].getMinScaleDenominator();
089: double maxScale = rules[k].getMaxScaleDenominator();
090: if (minScale <= scaleDen && maxScale > scaleDen) {
091: Filter filter = rules[k].getFilter();
092: List<PropertyPath> list = FilterTools
093: .extractPropertyPaths(filter);
094: pp.addAll(list);
095: Symbolizer[] sym = rules[k].getSymbolizers();
096: for (int d = 0; d < sym.length; d++) {
097: if (sym[d] instanceof PointSymbolizer) {
098: pp = extractPPFromPointSymbolizer(
099: (PointSymbolizer) sym[d], pp);
100: } else if (sym[d] instanceof LineSymbolizer) {
101: pp = extractPPFromLineSymbolizer(
102: (LineSymbolizer) sym[d], pp);
103: } else if (sym[d] instanceof PolygonSymbolizer) {
104: pp = extractPPFromPolygonSymbolizer(
105: (PolygonSymbolizer) sym[d], pp);
106: } else if (sym[d] instanceof TextSymbolizer) {
107: pp = extractPPFromTextSymbolizer(
108: (TextSymbolizer) sym[d], pp);
109: }
110: }
111: }
112: }
113: }
114: }
115:
116: List<PropertyPath> tmp = new ArrayList<PropertyPath>(pp.size());
117: for (int i = 0; i < pp.size(); i++) {
118: if (!tmp.contains(pp.get(i))) {
119: tmp.add(pp.get(i));
120: }
121: }
122:
123: return tmp;
124: }
125:
126: private static List<PropertyPath> extractPPFromTextSymbolizer(
127: TextSymbolizer symbolizer, List<PropertyPath> pp)
128: throws PropertyPathResolvingException {
129:
130: ParameterValueType[] bbox = symbolizer.getBoundingBox();
131: if (bbox != null) {
132: pp = extractPPFromParamValueType(bbox[0], pp);
133: pp = extractPPFromParamValueType(bbox[1], pp);
134: pp = extractPPFromParamValueType(bbox[2], pp);
135: pp = extractPPFromParamValueType(bbox[3], pp);
136: }
137:
138: Map<?, ?> css = null;
139: if (symbolizer.getFill() != null) {
140: css = symbolizer.getFill().getCssParameters();
141: pp = extractPPFromCssParameter(css, pp);
142: }
143: if (symbolizer.getFont() != null) {
144: css = symbolizer.getFont().getCssParameters();
145: pp = extractPPFromCssParameter(css, pp);
146: }
147: if (symbolizer.getGeometry() != null) {
148: pp.add(symbolizer.getGeometry().getPropertyPath());
149: }
150: Halo halo = symbolizer.getHalo();
151: ParameterValueType pvt = null;
152: if (halo != null) {
153: pvt = halo.getRadius();
154: pp = extractPPFromParamValueType(pvt, pp);
155:
156: if (halo.getFill() != null) {
157: css = halo.getFill().getCssParameters();
158: pp = extractPPFromCssParameter(css, pp);
159: }
160: if (halo.getStroke() != null) {
161: css = halo.getStroke().getCssParameters();
162: pp = extractPPFromCssParameter(css, pp);
163: }
164: }
165: pvt = symbolizer.getLabel();
166: pp = extractPPFromParamValueType(pvt, pp);
167:
168: // collect property names from line placement
169: LinePlacement lp = symbolizer.getLabelPlacement()
170: .getLinePlacement();
171: if (lp != null) {
172: pvt = lp.getGap();
173: pp = extractPPFromParamValueType(pvt, pp);
174: pvt = lp.getLineWidth();
175: pp = extractPPFromParamValueType(pvt, pp);
176: pvt = lp.getPerpendicularOffset();
177: pp = extractPPFromParamValueType(pvt, pp);
178: }
179:
180: // collect property names from line placement
181: PointPlacement ppl = symbolizer.getLabelPlacement()
182: .getPointPlacement();
183: if (ppl != null) {
184: pvt = ppl.getRotation();
185: pp = extractPPFromParamValueType(pvt, pp);
186: ParameterValueType[] pvta = ppl.getAnchorPoint();
187: if (pvta != null) {
188: for (int i = 0; i < pvta.length; i++) {
189: pp = extractPPFromParamValueType(pvta[i], pp);
190: }
191: }
192: pvta = ppl.getDisplacement();
193: if (pvta != null) {
194: for (int i = 0; i < pvta.length; i++) {
195: pp = extractPPFromParamValueType(pvta[i], pp);
196: }
197: }
198: }
199:
200: return pp;
201: }
202:
203: private static List<PropertyPath> extractPPFromPolygonSymbolizer(
204: PolygonSymbolizer symbolizer, List<PropertyPath> pp)
205: throws PropertyPathResolvingException {
206:
207: Map<?, ?> css = null;
208: if (symbolizer != null) {
209: if (symbolizer.getFill() != null) {
210: css = symbolizer.getFill().getCssParameters();
211: pp = extractPPFromCssParameter(css, pp);
212: }
213:
214: if (symbolizer.getGeometry() != null) {
215: pp.add(symbolizer.getGeometry().getPropertyPath());
216: }
217:
218: if (symbolizer.getStroke() != null) {
219: css = symbolizer.getStroke().getCssParameters();
220: pp = extractPPFromCssParameter(css, pp);
221: }
222: }
223:
224: return pp;
225: }
226:
227: private static List<PropertyPath> extractPPFromLineSymbolizer(
228: LineSymbolizer symbolizer, List<PropertyPath> pp)
229: throws PropertyPathResolvingException {
230:
231: if (symbolizer.getGeometry() != null) {
232: pp.add(symbolizer.getGeometry().getPropertyPath());
233: }
234:
235: Map<?, ?> css = symbolizer.getStroke().getCssParameters();
236: pp = extractPPFromCssParameter(css, pp);
237:
238: return pp;
239: }
240:
241: private static List<PropertyPath> extractPPFromPointSymbolizer(
242: PointSymbolizer symbolizer, List<PropertyPath> pp)
243: throws PropertyPathResolvingException {
244:
245: Graphic graphic = symbolizer.getGraphic();
246: if (graphic != null) {
247: if (graphic.getOpacity() != null) {
248: pp = extractPPFromParamValueType(graphic.getOpacity(),
249: pp);
250: }
251:
252: if (graphic.getRotation() != null) {
253: pp = extractPPFromParamValueType(graphic.getRotation(),
254: pp);
255: }
256:
257: if (graphic.getSize() != null) {
258: pp = extractPPFromParamValueType(graphic.getSize(), pp);
259: }
260:
261: if (symbolizer.getGeometry() != null) {
262: pp.add(symbolizer.getGeometry().getPropertyPath());
263: }
264: }
265:
266: return pp;
267: }
268:
269: private static List<PropertyPath> extractPPFromCssParameter(
270: Map<?, ?> css, List<PropertyPath> pp)
271: throws PropertyPathResolvingException {
272: if (css != null) {
273: Iterator<?> iter = css.values().iterator();
274: while (iter.hasNext()) {
275: ParameterValueType pvt = ((CssParameter) iter.next())
276: .getValue();
277: pp = extractPPFromParamValueType(pvt, pp);
278: }
279: }
280: return pp;
281: }
282:
283: private static List<PropertyPath> extractPPFromParamValueType(
284: ParameterValueType pvt, List<PropertyPath> pp)
285: throws PropertyPathResolvingException {
286: if (pvt != null) {
287: Object[] components = pvt.getComponents();
288: for (int i = 0; i < components.length; i++) {
289: if (components[i] instanceof Expression) {
290: pp = FilterTools.extractPropertyPaths(
291: (Expression) components[i], pp);
292: }
293: }
294: }
295: return pp;
296: }
297:
298: }
|