How does one filter a SpatialIndexFeatureCollection?

时间:2015-09-01 21:19:37

标签: java geotools opengis

When looking at the documentation for Geotools FeatureCollection, the subsection on Performance Options notes:

TreeSetFeatureCollection: the traditional TreeSet implementation used by default.

Note this does not perform well with spatial queries as the contents are not indexed.

Later it recommends a SpatialIndexFeatureCollection for faster queries:

SpatialIndexFeatureCollection: uses a spatial index to hold on to contents for fast visual display in a MapLayer; you cannot add more content to this feature collection once it is used

DataUtilities.source( featureCollection ) will wrap SpatialIndexFeatureCollection in a SpatialIndexFeatureSource that is able to take advantage of the spatial index.

The example given is:

final SimpleFeatureType TYPE = 

DataUtilities.createType("location","geom:Point,name:String");
WKTReader2 wkt = new WKTReader2();

SimpleFeatureCollection collection = new SpatialIndexFeatureCollection();
collection.add( SimpleFeatureBuilder.build( TYPE, new Object[]{ wkt.read("POINT(1,2)"), "name1"} ));
collection.add( SimpleFeatureBuilder.build( TYPE, new Object[]{ wkt.read("POINT(4,4)"), "name1"} ));

// Fast spatial Access
SimpleFeatureSource source = DataUtilities.source( collection );
SimpleFeatureCollection features = source.getFeatures( filter );

Besides not being able to compile this code (SimpleFeatureCollection is an interface and does not contain the member add), the code for SpatialIndexFeatureSource.getFeatures(Filter) directly calls SpatialIndexFeatureCollection.subCollection(Filter) which is defined as

public SimpleFeatureCollection subCollection(Filter filter) {
    throw new UnsupportedOperationException();
}

Github

Here is an example of my own attempt to use this

  FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

  SimpleFeatureCollection answers = getAnswers();
  SpatialIndexFeatureCollection collection = new SpatialIndexFeatureCollection();
  collection.addAll(answers);
  SimpleFeatureSource source = DataUtilities.source( collection );

  SimpleFeatureCollection gridCollection = getGridCollection();
  SimpleFeatureIterator iter = gridCollection.features();
  while(iter.hasNext()) {
    SimpleFeature grid = iter.next();
    Geometry gridCell = (Geometry) grid.getDefaultGeometry();
    Filter gridFilter = ff.intersects(ff.property("geometry"), ff.literal(gridCell));

    SimpleFeatureCollection results = source.getFeatures(combinedFilter);
  }

Unsurprisingly, this results in a UnsupportedOperationException

I have not been able to get this example to work and would really like to take advantage of the spatial indexing. How am I supposed to use a SpatialIndexFeatureCollection similar to the above example?

1 个答案:

答案 0 :(得分:0)

SpatialIndexFeatureCollection现在实现了subCollection方法。请参阅PR here。我没有机会向后移动这些更改,但未来版本现在将以您期望的方式工作。