GeoTools : Create filter from String

If you want to create a complex filter without writing extensive java code or xml then you need the, perhaps slightly missnameed, ExpressionBuilder to parse a plain text string.

Quick Start

To create a lessthan filter, the sum total of the code you need is:

   Filter filter = (Filter)ExpressionBuilder.parse("5 < 10");

Well, ok, thats not quite true, you also need to import ExpressionBuilder, ParserException and add a try/catch block. A complete example would look like this:

import org.geotools.filter.ExpressionBuilder;
import org.geotools.filter.parser.ParseException;

public class BuilderExample {
  public void demo(){
       Filter filter = (Filter)ExpressionBuilder.parse("5 < 10");
    catch(ParseException e){

Q. Why is the cast needed?
A. The parse method will return a Filter or an Expression depending on the content of the String.

More examples


All of the following are valid epxression examples

"10 + 5"
"10 + (4 / 2)"
"10 + foo"

In the above examples foo and bar are assumed to be attributes of the features that the resulting expression will be evaluated against.


"5 < 10"
"5 < 10 AND 1 < 2"
"3 < 4 AND (1 < 2 OR 5 > 10)"
"[[[ 3 < 4 ] AND NOT [ 2 < 4 ]] AND [ 5 < 4 ]]"
"foo < 1000"
"foo < bar"

Note that square brackets [] and round brackets () are equivelent, though the convention is for the square brackets to denote logic precedence and for round brackets to denote arithmatic precidence.

Using a schema

The above examples will generate filters even if the named attributes do not exist leading to error messages at evaluation time. If you would rather know about the errors in advance then you can use the altertative parse method which takes a FeatureType as an extra argument.

(Expression)ExpressionBuilder.parse(testFeature.getFeatureType(),"10 + testInteger");


The format of the parser is exactly the same as the format used by the toString() method in all existing filters and expressions, this means you can convert a Filter or Expression into a string, allow it to be editied and then turn it back into a filter or Expression.

The following unit test illustrates the point:

String in = "[3<4  AND  2<4 ] OR 5<4";
Filter filter = (Filter)ExpressionBuilder.parse(in);
String out = filter.toString();
assertEquals(filter, (Filter)ExpressionBuilder.parse(out));

Formatted Error Messages

If the expression or filter you try to parse is malformed then a ParseException will be thrown. If you wish to present a human readable error message, complete with indication of where in the string the error occured you can use the error formater:

String exp = "12 / ] + 4";
} catch(ParseException pe){
  String formated = ExpressionBuilder.getFormattedErrorMessage(pe, exp);
  assertTrue(formated.startsWith("12 / ] + 4\n     ^"));