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.UniqueVisitor;
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 unique value 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_UniqueFunction.java $
044: */
045: public class Collection_UniqueFunction extends FunctionExpressionImpl
046: implements FunctionExpression {
047: /** The logger for the filter module. */
048: private static final Logger LOGGER = org.geotools.util.logging.Logging
049: .getLogger("org.geotools.filter.function");
050: FeatureCollection previousFeatureCollection = null;
051: Object unique = null;
052:
053: /**
054: * Creates a new instance of Collection_UniqueFunction
055: */
056: public Collection_UniqueFunction() {
057: super ("Collection_Unique");
058: }
059:
060: public int getArgCount() {
061: return 1;
062: }
063:
064: /**
065: * Calculate unique (using FeatureCalc) - only one parameter is used.
066: *
067: * @param collection collection to calculate the unique
068: * @param expression Single Expression argument
069: *
070: * @return An object containing the unique value of the attributes
071: *
072: * @throws IllegalFilterException
073: * @throws IOException
074: */
075: public static CalcResult calculateUnique(
076: FeatureCollection collection, Expression expression)
077: throws IllegalFilterException, IOException {
078: UniqueVisitor uniqueVisitor = new UniqueVisitor(expression);
079: collection.accepts(uniqueVisitor, null);
080:
081: return uniqueVisitor.getResult();
082: }
083:
084: /**
085: * The provided arguments are evaulated with respect to the
086: * FeatureCollection.
087: *
088: * <p>
089: * For an aggregate function (like unique) please use the WFS mandated XPath
090: * syntax to refer to featureMember content.
091: * </p>
092: *
093: * <p>
094: * To refer to all 'X': <code>featureMember/asterisk/X</code>
095: * </p>
096: *
097: * @param args DOCUMENT ME!
098: *
099: * @throws IllegalArgumentException DOCUMENT ME!
100: */
101: public void setParameters(List args) {
102: super .setParameters(args);
103:
104: Expression expr = (Expression) getExpression(0);
105:
106: // if we see "featureMembers/*/ATTRIBUTE" change to "ATTRIBUTE"
107: expr.accept(new AbstractFilterVisitor() {
108: public void visit(AttributeExpression expression) {
109: String xpath = expression.getAttributePath();
110:
111: if (xpath.startsWith("featureMembers/*/")) {
112: xpath = xpath.substring(17);
113: } else if (xpath.startsWith("featureMember/*/")) {
114: xpath = xpath.substring(16);
115: }
116:
117: try {
118: expression.setAttributePath(xpath);
119: } catch (IllegalFilterException e) {
120: // ignore
121: }
122: }
123: });
124: }
125:
126: public Object evaluate(Object feature) {
127: if (feature == null) {
128: return new Integer(0); // no features were visited in the making of this answer
129: }
130: FeatureCollection featureCollection = (FeatureCollection) feature;
131: Expression expr = (Expression) getExpression(0);
132: synchronized (featureCollection) {
133: if (featureCollection != previousFeatureCollection) {
134: previousFeatureCollection = featureCollection;
135: unique = null;
136: try {
137: CalcResult result = calculateUnique(
138: featureCollection, expr);
139: if (result != null) {
140: unique = result.getValue();
141: }
142: } catch (IllegalFilterException e) {
143: LOGGER.log(Level.FINER, e.getLocalizedMessage(), e);
144: } catch (IOException e) {
145: LOGGER.log(Level.FINER, e.getLocalizedMessage(), e);
146: }
147: }
148: }
149: return unique;
150: }
151:
152: public void setExpression(Expression e) {
153: setParameters(Collections.singletonList(e));
154: }
155: }
|