GeoTools : How to use a Compare Filter

Here's the source code needed to create a filter that will, using GeoTools,
find only show cities with a population greater than 100,000, in a shapefile
. In this example we will use the citiesx020.shp file from the National Atlas (www.nationalatlas.gov).

DataStore store = null;
FeatureSource features = null;
FeatureCollection col = null;
FilterFactory filterFactory = FilterFactoryFinder.createFilterFactory();
CompareFilter filter = null;

try {
    store = new ShapefileDataStore(new URL("citiesx020.shp"));
    features = store.getFeatureSource(store.getTypeNames()[0]);
    //count features
    col = features.getFeatures();
    System.out.println("# cities (no filter)= "+col.size());
    //create the filter
    filter = filterFactory.createCompareFilter(CompareFilter.COMPARE_GREATER_THAN);
    FeatureType featureType = features.getFeatures().getSchema();
    filter.addLeftValue(filterFactory.createAttributeExpression("POP_2000"));
    filter.addRightValue(filterFactory.createLiteralExpression(100000));
    //count filtered features
    col = features.getFeatures(filter);
    System.out.println("# cities (after filter)= "+col.size());
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (IllegalFilterException e) {
    e.printStackTrace();
}

From Keith Knudsen on the gt2-users lists 18-8-04

Comments:

I used this code but It does not work right when DataStore is WFS.

here is my code :
---------------------------------------
Before filter, 105 features
---------------------------------------
/**

  • <p>Title: </p>
  • <p>Description: </p>
  • <p>Copyright: Copyright (c) 2005</p>
  • <p>Company: </p>
  • @author not attributable
  • @version 1.0
    */

import org.geotools.data.wfs.*;
import org.geotools.data.*;
import org.geotools.data.DataStore;
import org.geotools.data.Query;
import org.geotools.data.DefaultQuery;

import org.geotools.data.FeatureReader;
import org.geotools.feature.*;
import org.geotools.filter.*;
import org.geotools.feature.IllegalAttributeException;

import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class wfsdemo {
public static void main(String[] args){
System.out.println("HCM GIS WFS Demo");
int n=1;
try{
URL url = new URL("http://203.162.99.206:80/geoserver/wfs?version=1.0.0&request=getcapabilities&service=wfs");

Map m = new HashMap();
m.put(WFSDataStoreFactory.URL.key,url);
m.put(WFSDataStoreFactory.TIMEOUT.key,new Integer(10000));
DataStore wfs = (new WFSDataStoreFactory()).createNewDataStore(m);

System.out.println(wfs.getTypeNames()[n]);
// Create the filter
FeatureSource features= wfs.getFeatureSource(wfs.getTypeNames()[n]);
//FeatureCollection col = features.getFeatures().collection();

FilterFactory filterFactory = FilterFactory.createFilterFactory();

CompareFilter filter = filterFactory.createCompareFilter(CompareFilter.COMPARE_EQUALS);
FeatureType featureType =features.getFeatures().getSchema();
System.out.println(featureType.getNamespaceURI());

filter.addLeftValue(filterFactory.createAttributeExpression(featureType,"WIDTH"));
filter.addRightValue(filterFactory.createLiteralExpression(100));

Query query =new DefaultQuery(wfs.getTypeNames()[n],filter);//Integer.MAX_VALUE,Query.ALL_NAMES,"WIDTH");
//FeatureResults fresult= features.getFeatures(query) ;
// FeatureReader ft=fresult.reader();
System.out.println(query);
FeatureReader ft = wfs.getFeatureReader(query,Transaction.AUTO_COMMIT);

try

Unknown macro: { int count = 0; while(ft.hasNext()) if(ft.next()!=null)count++; System.out.println("HCM GIS found "+count+" features"); }

finally

Unknown macro: { ft.close(); }

}catch(IOException e)

Unknown macro: { e.printStackTrace(); }

catch (NoSuchElementException e)

catch (IllegalAttributeException e)

Unknown macro: { e.printStackTrace(); }

catch (IllegalFilterException e)

}
}
-------------------------------
After filtered, 105 features ---> not filtered
--------------------------------

Posted by bhs at Feb 28, 2005 03:02