Added by jgarnett, last edited by jgarnett on May 12, 2009  (view change)

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.

FeatureSource is probably the reason you came to this party; it let's you access geospatial information as Java objects.

FeatureSource

Here is what FeatureSource looks like:

interface FeatureSource<T extends FeatureType, F extends Feature> {
  DataAccess<T, F> getDataStore()
  Envelope getBounds()
  Envelope getBounds(Query)
  int getCount(Query)
  
  FeatureCollection<T, F> getFeatures()
  FeatureCollection<T, F> getFeatures(Filter)
  FeatureCollection<T, F> getFeatures(Query)
  T getSchema()

  void addFeatureListener(FeatureListener)
  void removeFeatureListener(FeatureListener)
}

Using FeatureSource to Access data

Access all Features

You can access all the features using a single method call:

DataStore dataStore = ...
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
featureSource = dataStore.getFeatureSource(featureName);
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures();

This is the same as asking for all the features included in the file or table:

FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures( Filter.INCLUDE );

Feature Access using Filter

Filter filter = CQL.filter("NAME == 'Hwy 31a');
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures( filter );

Feature Access using Query

You can use Query to request a limited set of attributes; and also request the contents in a specific order.

FilterFactory ff = ...
Filter filter = ...
String typeName = ...
Query query = new DefaultQuery(typeName, filter);
((DefaultQuery)query).setMaxFeatures(10);
((DefaultQuery)query).setPropertyNames(new String[]{"the_geom", "name"});

SortBy sortBy = ff.sort("name", SortOrder.ASCENDING);
((DefaultQuery)query).setSortBy(new SortBy[]{sortBy});

FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures( query );

Feature Access using DefaultQuery and Hints

You can use Hints as part of your Query to fine tune performance and functionality; for more information please see the Hints javadocs.

FilterFactory ff = ...
Filter filter = ...
String typeName = ...
DefaultQuery query = new DefaultQuery(typeName, filter);
((DefaultQuery)query).setPropertyNames(new String[]{"the_geom", "name"});

query.setHints( new Hints( Hints.FEATURE_2D, Boolean.true ); // force 2D data
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures( query );

Summarizing a FeatureSoruce

Count

SimpleFeatureType schema = featureSource.getSchema();
Query query = new DefaultQuery( schema.getTypeName(), Filter.INCLUDE );
int count = featureSource.getCount( query );
if( count == -1 ){
   FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures( query );
   count = collection.size();
}
System.out.println("There are "+count+" "+schema.getTypeName()+ " features");

Bounding Box

SimpleFeatureType schema = featureSource.getSchema();
Query query = new DefaultQuery( schema.getTypeName(), Filter.INCLUDE );
BoundingBox bounds = featureSource.getBounds( query );
if( bounds == null ){
   FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures( query );
   bounds = collection.getBounds();
}
System.out.println("The features are contained within "+bounds );

Aggregate Functions

You can perform more detailed summaries using an aggregate function over a FeatureCollection.

FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures();

FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
Function sum = ff.function("Collection_Sum", ff.property("age"));
        
Object value = sum.evaluate( featureCollection );
assertEquals( 41, value );

For more information see 10 FeatureCollection Aggregate Functions.

there are missing double-quotes inFilter filter = CQL.filter("NAME == 'Hwy 31a');

can I edit this page directly?

furthermore the name of the method seems to be "toFilter" instead of "filter"

You are correct; this is a wiki go ahead and edit

I know how to use a MediaWiki like Wikipedia, but I dont't find the edit-button here..... please help me ....