Skip to end of metadata
Go to start of metadata

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).

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

  • No labels

1 Comment

  1. 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
    --------------------------------