001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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 org.geotools.feature.Feature;
019: import org.geotools.feature.FeatureIterator;
020: import org.geotools.filter.Expression;
021: import org.geotools.filter.FilterFactory;
022: import org.geotools.filter.FilterFactoryFinder;
023: import org.geotools.filter.FunctionExpression;
024: import org.opengis.filter.expression.Function;
025: import org.opengis.filter.expression.Literal;
026: import org.opengis.filter.expression.PropertyName;
027:
028: /**
029: *
030: * @author James
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/filter/function/EqualIntervalFunctionTest.java $
032: */
033: public class EqualIntervalFunctionTest extends FunctionTestSupport {
034:
035: private static final FilterFactory ff = FilterFactoryFinder
036: .createFilterFactory();
037:
038: public EqualIntervalFunctionTest(String testName) {
039: super (testName);
040: }
041:
042: protected void tearDown() throws java.lang.Exception {
043: }
044:
045: public static junit.framework.Test suite() {
046: junit.framework.TestSuite suite = new junit.framework.TestSuite(
047: EqualIntervalFunctionTest.class);
048: return suite;
049: }
050:
051: /**
052: * Test of getName method, of class org.geotools.filter.functions.EqualIntervalFunction.
053: */
054: public void testInstance() {
055: Function equInt = ff.createFunctionExpression("EqualInterval");
056: assertNotNull(equInt);
057: assertEquals("test get name", "EqualInterval", equInt.getName());
058: }
059:
060: /**
061: * Test of setNumberOfClasses method, of class org.geotools.filter.functions.EqualIntervalFunction.
062: */
063: public void testSetClasses() throws Exception {
064: PropertyName property = ff.property("foo");
065: Literal literal = ff.literal(3);
066:
067: EqualIntervalFunction func = (EqualIntervalFunction) ff
068: .function("EqualInterval", property, literal);
069: assertEquals(3, func.getClasses());
070:
071: func.getParameters().set(1, ff.literal(12));
072: assertEquals(12, func.getClasses());
073: }
074:
075: public void testEvaluateWithExpressions() throws Exception {
076: Expression classes = (Expression) builder.parser(dataType, "3");
077: Expression expr1 = (Expression) builder.parser(dataType, "foo");
078: FunctionExpression func = fac
079: .createFunctionExpression("EqualInterval");
080: func.setArgs(new Expression[] { expr1, classes });
081:
082: Object classifier = func.evaluate(featureCollection);
083: assertTrue(classifier instanceof RangedClassifier);
084: RangedClassifier ranged = (RangedClassifier) classifier;
085: //values = 4,90,20,43,29,61,8,12
086: //4..90 = 4..32.67, 32.67..61.33, 61.33..90
087:
088: //correct number of classes
089: assertEquals(3, ranged.getSize());
090: //correct titles
091: assertEquals("4..32.667", ranged.getTitle(0));
092: assertEquals("32.667..61.333", ranged.getTitle(1));
093: assertEquals("61.333..90", ranged.getTitle(2));
094: //check classifier binning
095: assertEquals(0, ranged.classify(new Double(4)));
096: assertEquals(2, ranged.classify(expr1, testFeatures[1])); //90
097: assertEquals(0, ranged.classify(new Double(20)));
098: assertEquals(1, ranged.classify(new Double(43)));
099: assertEquals(0, ranged.classify(new Double(29)));
100: assertEquals(1, ranged.classify(new Double(61)));
101: assertEquals(0, ranged.classify(expr1, testFeatures[6])); //8
102: assertEquals(0, ranged.classify(new Double(12)));
103:
104: //try again with foo
105: }
106:
107: /** FIXME: Please for the love on binpop */
108: public void XtestEvaulateWithStrings() throws Exception {
109: org.opengis.filter.expression.Expression function = ff
110: .function("EqualInterval", ff.property("group"), ff
111: .literal(5));
112: Classifier classifier = (Classifier) function
113: .evaluate(featureCollection);
114: assertNotNull(classifier);
115:
116: Classifier classifier2 = (Classifier) function.evaluate(
117: featureCollection, Classifier.class);
118: assertNotNull(classifier2);
119:
120: Integer number = (Integer) function.evaluate(featureCollection,
121: Integer.class);
122: assertNull(number);
123: }
124:
125: public void testUpgradeExample() {
126: Function function = ff.function("equalInterval", ff
127: .property("foo"), ff.literal(12));
128: Object value = function.evaluate(featureCollection);
129: assertNotNull("classifier failed", value);
130:
131: Classifier split = (Classifier) value;
132: Function classify = ff.function("classify", ff.property("foo"),
133: ff.literal(split));
134:
135: Feature victim = testFeatures[2]; //foo = 20
136: assertEquals("Feature was placed in wrong bin", new Integer(2),
137: classify.evaluate(victim));
138: }
139: }
|