001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
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: * Created on May 11, 2005, 6:21 PM
017: */
018: package org.geotools.filter.function;
019:
020: import java.io.IOException;
021: import java.util.Collections;
022: import java.util.List;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.geotools.feature.Feature;
027: import org.geotools.feature.FeatureCollection;
028: import org.geotools.feature.visitor.BoundsVisitor;
029: import org.geotools.feature.visitor.CalcResult;
030: import org.geotools.filter.AttributeExpression;
031: import org.geotools.filter.Expression;
032: import org.geotools.filter.FunctionExpression;
033: import org.geotools.filter.FunctionExpressionImpl;
034: import org.geotools.filter.IllegalFilterException;
035: import org.geotools.filter.visitor.AbstractFilterVisitor;
036:
037: /**
038: * Calculates the bounds of an attribute for a given FeatureCollection
039: * and Expression.
040: *
041: * @author Cory Horner
042: * @since 2.2M2
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/filter/function/Collection_BoundsFunction.java $
044: */
045: public class Collection_BoundsFunction extends FunctionExpressionImpl {
046: /** The logger for the filter module. */
047: private static final Logger LOGGER = org.geotools.util.logging.Logging
048: .getLogger("org.geotools.filter.function");
049: FeatureCollection previousFeatureCollection = null;
050: Object bounds = null;
051:
052: /**
053: * Creates a new instance of Collection_BoundsFunction
054: */
055: public Collection_BoundsFunction() {
056: super ("Collection_Bounds");
057: }
058:
059: public int getArgCount() {
060: return 1;
061: }
062:
063: /**
064: * Calculate unique (using FeatureCalc) - only one parameter is used.
065: *
066: * @param collection collection to calculate the unique
067: *
068: * @return An object containing the unique value of the attributes
069: *
070: * @throws IllegalFilterException
071: * @throws IOException
072: */
073: public static CalcResult calculateBounds(
074: FeatureCollection collection)
075: throws IllegalFilterException, IOException {
076: BoundsVisitor boundsVisitor = new BoundsVisitor();
077: collection.accepts(boundsVisitor, null);
078:
079: return boundsVisitor.getResult();
080: }
081:
082: /**
083: * The provided arguments are evaulated with respect to the
084: * FeatureCollection.
085: *
086: * <p>
087: * For an aggregate function (like unique) please use the WFS mandated XPath
088: * syntax to refer to featureMember content.
089: * </p>
090: *
091: * <p>
092: * To refer to all 'X': <code>featureMember/asterisk/X</code>
093: * </p>
094: *
095: * @param args DOCUMENT ME!
096: *
097: * @throws IllegalArgumentException DOCUMENT ME!
098: */
099: public void setParameters(List args) {
100: super .setParameters(args);
101: if (args.size() != 1) {
102: throw new IllegalArgumentException(
103: "Require a single argument for unique");
104: }
105:
106: //HACK: remove cast once the move to geoapi is complete
107: Expression expr;
108: expr = (Expression) getExpression(0);
109:
110: // if we see "featureMembers/*/ATTRIBUTE" change to "ATTRIBUTE"
111: expr.accept(new AbstractFilterVisitor() {
112: public void visit(AttributeExpression expression) {
113: String xpath = expression.getAttributePath();
114:
115: if (xpath.startsWith("featureMembers/*/")) {
116: xpath = xpath.substring(17);
117: } else if (xpath.startsWith("featureMember/*/")) {
118: xpath = xpath.substring(16);
119: }
120:
121: try {
122: expression.setAttributePath(xpath);
123: } catch (IllegalFilterException e) {
124: // ignore
125: }
126: }
127: });
128: }
129:
130: public Object evaluate(Object feature) {
131: if (feature == null) {
132: return new Integer(0); // no features were visited in the making of this answer
133: }
134: FeatureCollection featureCollection = (FeatureCollection) feature;
135: synchronized (featureCollection) {
136: if (featureCollection != previousFeatureCollection) {
137: previousFeatureCollection = featureCollection;
138: bounds = null;
139: try {
140: CalcResult result = calculateBounds(featureCollection);
141: if (result != null) {
142: bounds = result.getValue();
143: }
144: } catch (IllegalFilterException e) {
145: LOGGER.log(Level.FINER, e.getLocalizedMessage(), e);
146: } catch (IOException e) {
147: LOGGER.log(Level.FINER, e.getLocalizedMessage(), e);
148: }
149: }
150: }
151: return bounds;
152: }
153:
154: public void setExpression(Expression e) {
155: setParameters(Collections.singletonList(e));
156: }
157:
158: }
|