Added by jgarnett, last edited by jgarnett on Sep 23, 2008  (view change)

Labels

 
(None)

GeoTools makes use of the GeoAPI project Filter interface in order to express constraints. This is most often used when making a Query to retrieve specific Feature}}s from a {{DataStore.

You will find the use of Filter in an number of other locations, it is used as part of a Style when we need to select what is drawn on the screen. It is actually used as part of our FeatureType in order to express any special constraints on data values (such as the length of a String}}.

Reference:

Creating a Filter

Using Common Query Language

Most code examples in this wiki will assume you are using the "Common Query Lanaguage", this parser is provided by the gt2-cql jar and is an optional module.

Filter filter = CQL.toFilter("attName >= 5");

Using a FilterFactory

We make use of FilterFactory2 in GeoTools (which has some additional methods making it easier to work with JTS Geometry):

FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2( GeoTools.getDefaultHints );

Filter filter = ff.contains( ff.property( "THE_GEOM"), ff.literal( geometry ) );

One thing you can do with with a FilterFactory (which you cannot do in CQL) is request features by their FeatureId:

FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2( GeoTools.getDefaultHints );

Set<FeatureId> fids = new HashSet<FeatureId>();
fids.add( ff.featureId("ROAD.1") );
fids.add( ff.featureId("ROAD.2") );
Filter filter = ff.id( fids );

From XML

Parsing a Filter 1.0 document:

Parser parser = new Parser( new org.geotools.filter.v1_0.OGCConfiguration() );
Filter filter = (Filter) parser.parse( inputstream );

Using Filter

With Features

You can use a filter by hand to check an individual Feature:

if( filter.evaulate( feature ) ){
    System.out.println( feature.getId() + " was selected" );
}

With Java Beans

Java Beans (and plenty of other objects) also work with filter:

if( filter.evaulate( bean ) ){
    System.out.println( bean + " was selected" );
}

If you look in the advanced guide you can find out how to extend Filter support for your application. GeoTools users have used filters with Java Beans, Maps and Collections and featureTypes.

Writing out XML

FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
String xml = transform.transform( filter );