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.List;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.feature.visitor.CalcResult;
028: import org.geotools.feature.visitor.CountVisitor;
029: import org.geotools.filter.AttributeExpression;
030: import org.geotools.filter.Expression;
031: import org.geotools.filter.FunctionExpression;
032: import org.geotools.filter.FunctionExpressionImpl;
033: import org.geotools.filter.IllegalFilterException;
034: import org.geotools.filter.visitor.AbstractFilterVisitor;
035:
036: /**
037: * Calculates the count value of an attribute for a given FeatureCollection and
038: * Expression.
039: *
040: * @author Cory Horner
041: * @since 2.2M2
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/filter/function/Collection_CountFunction.java $
043: */
044: public class Collection_CountFunction extends FunctionExpressionImpl {
045: /** The logger for the filter module. */
046: private static final Logger LOGGER = org.geotools.util.logging.Logging
047: .getLogger("org.geotools.filter.function");
048:
049: FeatureCollection previousFeatureCollection = null;
050:
051: Object count = null;
052:
053: /**
054: * Creates a new instance of Collection_CountFunction
055: */
056: public Collection_CountFunction() {
057: super ("Collection_Count");
058: }
059:
060: public int getArgCount() {
061: return 1;
062: }
063:
064: /**
065: * Calculate count (using FeatureCalc) - only one parameter is used.
066: *
067: * @param collection
068: * collection to calculate the count
069: *
070: * @return An object containing the count value of the attributes
071: *
072: * @throws IllegalFilterException
073: * @throws IOException
074: */
075: public static CalcResult calculateCount(FeatureCollection collection)
076: throws IllegalFilterException, IOException {
077: CountVisitor countVisitor = new CountVisitor();
078: collection.accepts(countVisitor, null);
079: return countVisitor.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 count) 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
096: * DOCUMENT ME!
097: *
098: * @throws IllegalArgumentException
099: * 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: synchronized (featureCollection) {
132: if (featureCollection != previousFeatureCollection) {
133: previousFeatureCollection = featureCollection;
134: count = null;
135: try {
136: CalcResult result = calculateCount(featureCollection);
137: if (result != null) {
138: count = result.getValue();
139: }
140: } catch (IllegalFilterException e) {
141: LOGGER.log(Level.FINER, e.getLocalizedMessage(), e);
142: } catch (IOException e) {
143: LOGGER.log(Level.FINER, e.getLocalizedMessage(), e);
144: }
145: }
146: }
147: return count;
148: }
149:
150: }
|