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: package org.geotools.brewer.color;
017:
018: import java.awt.Color;
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.List;
022: import org.opengis.filter.Filter;
023: import org.geotools.data.DataTestCase;
024: import org.geotools.data.DataUtilities;
025: import org.geotools.data.FeatureSource;
026: import org.geotools.feature.Feature;
027: import org.geotools.feature.FeatureCollection;
028: import org.geotools.feature.FeatureIterator;
029: import org.geotools.feature.FeatureType;
030: import org.geotools.filter.FilterFactory;
031: import org.geotools.filter.FilterFactoryFinder;
032: import org.geotools.filter.IllegalFilterException;
033: import org.geotools.filter.MathExpression;
034: import org.geotools.filter.function.ClassificationFunction;
035: import org.geotools.filter.function.EqualIntervalFunction;
036: import org.geotools.filter.function.RangedClassifier;
037: import org.geotools.styling.FeatureTypeStyle;
038: import org.geotools.styling.Rule;
039:
040: /**
041: *
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/brewer/src/test/java/org/geotools/brewer/color/StyleGeneratorTest.java $
043: */
044: public class StyleGeneratorTest extends DataTestCase {
045: public StyleGeneratorTest(String arg0) {
046: super (arg0);
047: }
048:
049: public void checkFilteredResultNotEmpty(Rule[] rule,
050: FeatureSource fs, String attribName) throws IOException {
051: for (int i = 0; i < rule.length; i++) {
052: Filter filter = rule[i].getFilter();
053: FeatureCollection filteredCollection = fs
054: .getFeatures(filter);
055: assertTrue(filteredCollection.size() > 0);
056:
057: String filterInfo = "Filter \"" + filter.toString()
058: + "\" contains " + filteredCollection.size()
059: + " element(s) (";
060: FeatureIterator it = filteredCollection.features();
061:
062: while (it.hasNext()) {
063: Feature feature = it.next();
064: filterInfo += ("'" + feature.getAttribute(attribName) + "'");
065:
066: if (it.hasNext()) {
067: filterInfo += ", ";
068: }
069: }
070:
071: it.close();
072: System.out.println(filterInfo + ")");
073: }
074: }
075:
076: public void testComplexExpression() throws Exception {
077: ColorBrewer brewer = new ColorBrewer();
078: brewer.loadPalettes();
079:
080: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
081: MathExpression expr = null;
082: MathExpression expr2 = null;
083: FeatureType type = roadType;
084: String attribName = type.getAttributeType(0).getName();
085: FeatureCollection fc = DataUtilities.collection(roadFeatures);
086: FeatureSource fs = DataUtilities.source(fc);
087:
088: try {
089: expr = ff
090: .createMathExpression(MathExpression.MATH_MULTIPLY);
091: expr.addLeftValue(ff.createAttributeExpression(attribName));
092: expr
093: .addRightValue(ff
094: .createAttributeExpression(attribName));
095: expr2 = ff.createMathExpression(MathExpression.MATH_ADD);
096: expr2.addLeftValue(expr);
097: expr2.addRightValue(ff.createLiteralExpression(3));
098: } catch (IllegalFilterException e) {
099: fail(e.getMessage());
100: }
101:
102: String paletteName = "YlGn"; //type = Sequential
103:
104: //create the classification function
105: ClassificationFunction function = new EqualIntervalFunction();
106: List params = new ArrayList();
107: params.add(0, expr2); //expression
108: params.add(1, ff.literal(2)); //classes
109: function.setParameters(params);
110:
111: Object object = function.evaluate(fc);
112: assertTrue(object instanceof RangedClassifier);
113:
114: RangedClassifier classifier = (RangedClassifier) object;
115:
116: Color[] colors = brewer.getPalette(paletteName).getColors(2);
117:
118: // get the fts
119: FeatureTypeStyle fts = StyleGenerator.createFeatureTypeStyle(
120: classifier, expr2, colors, "myfts", roadFeatures[0]
121: .getFeatureType().getDefaultGeometry(),
122: StyleGenerator.ELSEMODE_IGNORE, 0.5, null);
123: assertNotNull(fts);
124:
125: // test each filter
126: Rule[] rule = fts.getRules();
127: assertEquals(2, rule.length);
128: //do a preliminary test to make sure each rule's filter returns some results
129: checkFilteredResultNotEmpty(rule, fs, attribName);
130:
131: assertNotNull(StyleGenerator.toStyleExpression(rule[0]
132: .getFilter()));
133: assertNotNull(StyleGenerator.toStyleExpression(rule[1]
134: .getFilter()));
135: }
136: }
|