2

在查看 Geotools FeatureCollection的文档时,Performance Options 的小节说明:

TreeSetFeatureCollection:默认使用的传统 TreeSet 实现。

请注意,这在空间查询中表现不佳,因为内容没有被索引。

后来它推荐了一个SpatialIndexFeatureCollection更快的查询:

SpatialIndexFeatureCollection:使用空间索引来保存内容,以便在 MapLayer 中快速视觉显示;一旦使用,您将无法向此功能集合添加更多内容

DataUtilities.source( featureCollection ) 将 SpatialIndexFeatureCollection 包装在能够利用空间索引的 SpatialIndexFeatureSource 中。

给出的例子是:

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

除了不能编译这段代码(SimpleFeatureCollection是一个接口,不包含成员add)之外,SpatialIndexFeatureSource.getFeatures(Filter)直接调用的代码SpatialIndexFeatureCollection.subCollection(Filter)定义为

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

Github

这是我自己尝试使用它的示例

  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);
  }

不出所料,这会导致UnsupportedOperationException

我无法让这个例子工作,我真的很想利用空间索引。我应该如何使用SpatialIndexFeatureCollection与上述示例类似的方法?

4

1 回答 1

1

SpatialIndexFeatureCollection现在实现了 subCollection 方法。请参阅此处的 PR。我还没有机会向后移植这些更改,但未来的版本现在将按照您预期的方式工作。

于 2016-03-01T08:49:27.030 回答