An Expression is an object that can perform some kind of calculation on a Feature and provide a result (that is, a generic Object, meaning that the result can really be anything). As you can see, the Expression is quite a generic concept, and the Geotools library provides some default implementations that are useful in data access and map styling which can be seen in the following class diagram.

As you can see from the class diagram, they are just interfaces. Since you need to create actual objects, you will use the FilterFactory that can create objects implementing those interfaces.

Expressions are usually composed of other expressions, until a simpler one, the LiteralExpression or an AttributeExpression, is used. The LiteralExpression is a literal, that is, it does not depend on the feature contents and is a constant value. On the other hand, an AttributeExpression will extract the value of a particular Feature attribute. Every other Expression will build on top of these expressions.
For example, a MathExpression of type "sum" will sum two other expressions, which may be literals, attributes, or composite expressions. For those of you familiar with compiler internals, a complex mathematical expression will be represented in Geotools2 as its evaluation tree where each node is an Expression object.

How to build an expression?

There are many ways to create an expression :

  • with java code
  • within xml file
  • by using an expression builder


Here is a simple example that extends the code of the basic feature tutorial by creating a simple sum expression that will sum the constant 2 and the contents of the attribute "width" of the passed feature.

public static void main(String[] args)
    throws SchemaException, ParseException, IllegalAttributeException, IllegalFilterException {
    // build the Feature
    AttributeType geom = AttributeTypeFactory.newAttributeType("the_geom", LineString.class);
    AttributeType roadWidth = AttributeTypeFactory.newAttributeType("width", Float.class);
    FeatureType ftRoad =
        FeatureTypeFactory.newFeatureType(new AttributeType[] { geom, roadWidth }, "road");

    WKTReader wktReader = new WKTReader();
    LineString geometry = (LineString)"LINESTRING (0 0, 10 10)");
    Float width = new Float(10);
    Feature theRoad = ftRoad.create(new Object[] { geometry, width }, "myRoad");


    // build a reusable sum expression
    FilterFactory ff = FilterFactory.createFilterFactory();
    LiteralExpression two = ff.createLiteralExpression(2.0);
    AttributeExpression att = ff.createAttributeExpression(ftRoad, "width");
    MathExpression addExpr = ff.createMathExpression(Expression.MATH_ADD);



expression builder

   Expression exp = (Expression)ExpressionBuilder.parse("10 + 5");

A Simple Math Expression (from Snippets)

Simple math expression which processes a simple add on two integers.

        MathExpr one = new LiteralMathExpr( 1 );
    	MathExpr two = new LiteralMathExpr( 2 );
    	MathExpr add = one.add( two );
    	Expr value = add.eval();
    	LiteralMathExpr sum = (LiteralMathExpr) value;