Traditionally functions that work on a collection are called "aggregate functions", in the world of databases and SQL these functions include "min", "max", "average" and "count". In GeoTools we support these concepts and more. We have functions that will calculate the bounding box of your FeatureCollection.
These functions are implemented as a FeatureVisitor; and are often optimized into raw SQL on supporting DataStores.
Available Aggregate Functions
Here are the aggregate functions that ship with GeoTools at the time of writing. For the complete list check javadocs.
| Function | Visitor | |
|---|---|---|
| Collection_Average | AverageVisitor | |
| Collection_Bounds | BoundsVisitor | should be the same as getBounds() |
| Collection_Count | CountVisitor | Integer; should be the same as size() |
| Collection_Max | MaxVisitor | |
| Collection_Median | MedianVisitor | |
| Collection_Min | MinVisitor | |
| Collection_Sum | SumVisitor | |
| Collection_Unique | UniqueVisitor | Set<Object> of unique values |
Sum of a FeatureCollection
Here is an example of using Collection_Sum on a FeatureCollection:
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null); Function sum = ff.function("Collection_Sum", ff.property("age")); Object value = sum.evaluate( featureCollection ); assertEquals( 41, value );
Alternative - Direct use of MaxVisitor
This boils down to a call to MaxVisitor:
Expression = ff.property("age"); MaxVisitor maxVisitor = new MaxVisitor(expression); collection.accepts(maxVisitor, null); CalcResult result = maxVisitor.getResult(); Object max = result.getValue();
MaxVisitor is pretty good about handling numeric and string types (basically anything that is comparable should work). CalcResult is used to hold the value until you are interested in it; you can run the same visitor across several collections
and look at the result when you are interested.
Max of a FeatureCollection
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null); Function sum = ff.function("Collection_Max", ff.property("age")); Object value = sum.evaluate( featureCollection ); assertEquals( 41, value );