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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.filter.function;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.geotools.data.DataUtilities;
022: import org.geotools.data.memory.MemoryDataStore;
023: import org.geotools.feature.Feature;
024: import org.geotools.feature.FeatureCollection;
025: import org.geotools.feature.FeatureType;
026: import org.geotools.feature.visitor.FeatureVisitor;
027: import org.geotools.filter.Expression;
028: import org.geotools.filter.ExpressionType;
029: import org.geotools.filter.FilterFactoryFinder;
030: import org.geotools.filter.FunctionExpression;
031: import org.geotools.filter.MathExpression;
032: import org.geotools.filter.parser.ParseException;
033:
034: /**
035: *
036: * @author Cory Horner, Refractions Research Inc.
037: *
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/filter/function/QuantileFunctionTest.java $
039: */
040: public class QuantileFunctionTest extends FunctionTestSupport {
041:
042: public QuantileFunctionTest(String testName) {
043: super (testName);
044: }
045:
046: protected void tearDown() throws java.lang.Exception {
047: }
048:
049: public static junit.framework.Test suite() {
050: junit.framework.TestSuite suite = new junit.framework.TestSuite(
051: QuantileFunctionTest.class);
052:
053: return suite;
054: }
055:
056: public void testInstance() {
057: FunctionExpression equInt = FilterFactoryFinder
058: .createFilterFactory().createFunctionExpression(
059: "Quantile");
060: assertNotNull(equInt);
061: }
062:
063: public void testGetName() {
064: FunctionExpression qInt = FilterFactoryFinder
065: .createFilterFactory().createFunctionExpression(
066: "Quantile");
067: System.out.println("testGetName");
068: assertEquals("Quantile", qInt.getName());
069: }
070:
071: public void testSetParameters() throws Exception {
072: Expression classes = (Expression) builder.parser(dataType, "3");
073: Expression expr = (Expression) builder.parser(dataType, "foo");
074: List params = new ArrayList();
075: params.add(0, expr);
076: params.add(1, classes);
077: QuantileFunction func = (QuantileFunction) fac
078: .createFunctionExpression("Quantile");
079: func.setParameters(params);
080: assertEquals(3, func.getClasses());
081: classes = (Expression) builder.parser(dataType, "12");
082: params.set(1, classes);
083: func.setParameters(params);
084: assertEquals(12, func.getClasses());
085: //deprecated still works?
086: classes = (Expression) builder.parser(dataType, "5");
087: func.setArgs(new Expression[] { expr, classes });
088: assertEquals(5, func.getClasses());
089: }
090:
091: public void testEvaluateWithExpressions() throws Exception {
092: Expression classes = (Expression) builder.parser(dataType, "2");
093: Expression exp = (Expression) builder.parser(dataType, "foo");
094: FunctionExpression func = fac
095: .createFunctionExpression("Quantile");
096: func.setArgs(new Expression[] { exp, classes });
097:
098: Object value = func.evaluate(featureCollection);
099: assertTrue(value instanceof RangedClassifier);
100: RangedClassifier ranged = (RangedClassifier) value;
101: assertEquals(2, ranged.getSize());
102: assertEquals("4..20", ranged.getTitle(0));
103: assertEquals("20..90", ranged.getTitle(1));
104: }
105:
106: public void testEvaluateWithStrings() throws Exception {
107: org.opengis.filter.expression.Expression function = ff
108: .function("Quantile", ff.property("group"), ff
109: .literal(2));
110: Classifier classifier = (Classifier) function
111: .evaluate(featureCollection);
112: assertNotNull(classifier);
113:
114: Classifier classifier2 = (Classifier) function.evaluate(
115: featureCollection, Classifier.class);
116: assertNotNull(classifier2);
117:
118: Integer number = (Integer) function.evaluate(featureCollection,
119: Integer.class);
120: assertNull(number);
121: }
122:
123: public void xtestNullNaNHandling() throws Exception {
124: //setup
125: FunctionExpression func = fac
126: .createFunctionExpression("Quantile");
127: QuantileFunction qf = (QuantileFunction) func;
128:
129: //create a feature collection
130: FeatureType ft = DataUtilities.createType(
131: "classification.nullnan", "id:0,foo:int,bar:double");
132: Integer iVal[] = new Integer[] { new Integer(0),
133: new Integer(0), new Integer(0), new Integer(13),
134: new Integer(13), new Integer(13), null, null, null };
135: Double dVal[] = new Double[] { new Double(0.0),
136: new Double(50.01), null, new Double(0.0),
137: new Double(50.01), null, new Double(0.0),
138: new Double(50.01), null };
139:
140: Feature[] testFeatures = new Feature[iVal.length];
141:
142: for (int i = 0; i < iVal.length; i++) {
143: testFeatures[i] = ft.create(new Object[] {
144: new Integer(i + 1), iVal[i], dVal[i], },
145: "nantest.t" + (i + 1));
146: }
147: MemoryDataStore store = new MemoryDataStore();
148: store.createSchema(ft);
149: store.addFeatures(testFeatures);
150: FeatureCollection this FC = store.getFeatureSource("nullnan")
151: .getFeatures();
152:
153: //create the expression
154: MathExpression divide = fac
155: .createMathExpression(ExpressionType.MATH_DIVIDE);
156: divide
157: .addLeftValue((Expression) builder.parse(dataType,
158: "foo"));
159: divide.addRightValue((Expression) builder
160: .parse(dataType, "bar"));
161:
162: qf.setClasses(3);
163: qf.setExpression(divide);
164:
165: RangedClassifier range = (RangedClassifier) qf.evaluate(this FC);
166: assertEquals(2, range.getSize()); //2 or 3?
167: assertEquals("0..0", range.getTitle(0));
168: assertEquals("0..0.25995", range.getTitle(1));
169: }
170: }
|