GeoTools : Using a bounding box Filter


This page is out of date - please see:

Given an area of interest stored as an envelope the following code will limit the features returned to those which are not wholy outside the bounding box.

// "Where" road BBOX envelope
FilterFactory ff = FilterFactoryFinder.createFilterFactory();
Expression bbox = ff.createBBoxExpression( envelope );
Expression geometry = ff.createAttributeExpression( "road" );
GeometryFilter bboxFilter = ff.createGeometryFilter(AbstractFilter.GEOMETRY_BBOX);
bboxFilter.addLeftGeometry( geometry );
bboxFilter.addRightGeometry( bbox );

Of course you may still wonder where "road" comes from - that is the name of the geometry attribute.

This code fetches the default geometry for a typeName.

FeatureType featureType = dataStore.getSchema( typeName );
String geometryName = featureType.getDefaultGeomety().getName();

Note that this may or may not still lead to the datastore reading all of the filters in, for example a shapefile reader will still need to examine all the features to determine which to return. However an SQL based datastore will convert the filter into part of the SQL query which will be much faster.

In GeoTools 2.2, IndexedShapefile is available that will optmize BBOX filters