/** * Calculate average (using FeatureCalc) - only one parameter is used. * * @param collection collection to calculate the average * @param expression Single Expression argument * @return An object containing the average value of the attributes * @throws IllegalFilterException * @throws IOException */ static CalcResult calculateAverage( FeatureCollection<? extends FeatureType, ? extends Feature> collection, Expression expression) throws IllegalFilterException, IOException { AverageVisitor averageVisitor = new AverageVisitor(expression); collection.accepts(averageVisitor, null); return averageVisitor.getResult(); }
public void setValue(Object newAverage) { reset(); Class type = newAverage.getClass(); strategy = createStrategy(type); strategy.add(newAverage); isOptimized = true; }
public void visit(SimpleFeature feature) { visit((Feature) feature); }
public void testAverage() throws IllegalFilterException, IOException { AverageVisitor averageVisitor = new AverageVisitor(0, ft); AverageVisitor averageVisitor2 = new AverageVisitor(3, ft2); int value1 = averageVisitor.getResult().toInt(); assertEquals(2, value1); double value2 = averageVisitor2.getResult().toDouble(); assertEquals((double) 3.75, value2, 0); CalcResult averageResult1 = averageVisitor.getResult(); CalcResult averageResult2 = averageVisitor2.getResult(); CalcResult averageResult3 = averageResult1.merge(averageResult2); assertEquals((double) 2.7, averageResult3.toDouble(), 0); averageVisitor2.setValue(5, Integer.valueOf(100)); // mergeable optimization averageResult2 = averageVisitor2.getResult(); assertEquals(20, averageResult2.toInt()); averageResult3 = averageResult1.merge(averageResult2); assertEquals((double) 13.25, averageResult3.toDouble(), 0); averageVisitor2.setValue(new Double(15.4)); // un-mergeable optimization averageResult2 = averageVisitor2.getResult(); assertEquals((double) 15.4, averageResult2.toDouble(), 0); try { averageVisitor.setValue(5, Integer.valueOf(10)); averageResult1 = averageVisitor.getResult(); averageVisitor2.setValue(5, new Double(33.3)); averageResult2 = averageVisitor2.getResult();
@Override public FeatureCalc create(Expression aggregateAttribute) { return new AverageVisitor(aggregateAttribute); }
public void visit(org.opengis.feature.Feature feature) { Object value = expr.evaluate(feature); if (value != null) { if (strategy == null) { Class type = value.getClass(); strategy = createStrategy(type); } strategy.add(value); } }
Object sum = sum(averageCalc.getExpression()); Object count = count(null); if (sum != null && count != null) { averageCalc.setValue(((Number) count).intValue(), sum); isOptimized = true; progress.complete();
@Override protected FeatureCalc createVisitor(int attributeTypeIndex, SimpleFeatureType type) { return new AverageVisitor(attributeTypeIndex, type); } }
/** * Constructor class for the AverageVisitor using AttributeDescriptor ID * * @param attributeTypeIndex integer representing the AttributeDescriptor * @param type FeatureType * @throws IllegalFilterException */ public AverageVisitor(int attributeTypeIndex, SimpleFeatureType type) throws IllegalFilterException { FilterFactory factory = CommonFactoryFinder.getFilterFactory(null); AttributeDescriptor attributeType = type.getDescriptor(attributeTypeIndex); expr = factory.property(attributeType.getLocalName()); createStrategy(attributeType.getType().getBinding()); }
/** * Calculate average (using FeatureCalc) - only one parameter is used. * * @param collection collection to calculate the average * @param expression Single Expression argument * * @return An object containing the average value of the attributes * * @throws IllegalFilterException * @throws IOException */ public static CalcResult calculateAverage(FeatureCollection collection, Expression expression) throws IllegalFilterException, IOException { AverageVisitor averageVisitor = new AverageVisitor(expression); collection.accepts(averageVisitor, null); return averageVisitor.getResult(); }
public void setValue(int newCount, Object newSum) { reset(); strategy = createStrategy(newSum.getClass()); strategy.set(newCount, newSum); isOptimized = false; // this is an optimization, but we have the same // information we would otherwise have if we had // in fact visited each feature }
FeatureCalc calc; if (function == AggregationFunction.Average) { calc = new AverageVisitor(attIndex, features.getSchema()); } else if (function == AggregationFunction.Count) { calc = new CountVisitor();
/** * Constructor class for the AverageVisitor using AttributeDescriptor Name * * @param attrName string respresenting the AttributeDescriptor * @param type FeatureType * @throws IllegalFilterException */ public AverageVisitor(String attrName, SimpleFeatureType type) throws IllegalFilterException { FilterFactory factory = CommonFactoryFinder.getFilterFactory(null); AttributeDescriptor attributeType = type.getDescriptor(attrName); expr = factory.property(attributeType.getLocalName()); createStrategy(attributeType.getType().getBinding()); }
public void visit(SimpleFeature feature) { visit(feature); }
/** * Calculate average (using FeatureCalc) - only one parameter is used. * * @param collection collection to calculate the average * @param expression Single Expression argument * * @return An object containing the average value of the attributes * * @throws IllegalFilterException * @throws IOException */ static CalcResult calculateAverage(FeatureCollection<? extends FeatureType, ? extends Feature> collection, Expression expression) throws IllegalFilterException, IOException { AverageVisitor averageVisitor = new AverageVisitor(expression); collection.accepts(averageVisitor, null); return averageVisitor.getResult(); }
public void setValue(int newCount, Object newSum) { reset(); strategy = createStrategy(newSum.getClass()); strategy.set(newCount, newSum); isOptimized = false; // this is an optimization, but we have the same // information we would otherwise have if we had // in fact visited each feature }
FeatureCalc calc; if (function == AggregationFunction.Average) { calc = new AverageVisitor(attIndex, features.getSchema()); } else if (function == AggregationFunction.Count) { calc = new CountVisitor();
public void visit(Feature feature) { Object value = expr.evaluate(feature); if (strategy == null) { Class type = value.getClass(); strategy = createStrategy(type); } strategy.add(value); }
public void testStandardDeviation() throws Exception { FilterFactory factory = CommonFactoryFinder.getFilterFactory(null); Expression expr = factory.property(ft3.getDescriptor(0).getLocalName()); // first do it the old fashioned way to ensure backwards compatibility AverageVisitor visit1 = new AverageVisitor(expr); fc3.accepts(visit1, null); CalcResult result = visit1.getResult(); double average = result.toDouble(); StandardDeviationVisitor visit2 = new StandardDeviationVisitor(expr, average); fc3.accepts(visit2, null); assertEquals(28.86, visit2.getResult().toDouble(), 0.01); // then do it single pass StandardDeviationVisitor visit3 = new StandardDeviationVisitor(expr); fc3.accepts(visit3, null); assertEquals(28.86, visit3.getResult().toDouble(), 0.01); // test empty collection StandardDeviationVisitor emptyVisitor = new StandardDeviationVisitor(expr, average); empty.accepts(emptyVisitor, null); assertEquals(CalcResult.NULL_RESULT, emptyVisitor.getResult()); // test merge assertSame(result, emptyVisitor.getResult().merge(result)); assertSame(result, result.merge(emptyVisitor.getResult())); }
public void setValue(int newCount, Object newSum) { reset(); strategy = createStrategy(newSum.getClass()); strategy.set(newCount, newSum); isOptimized = false; // this is an optimization, but we have the same // information we would otherwise have if we had // in fact visited each feature }