【问题标题】:How does one filter a SpatialIndexFeatureCollection?如何过滤空间索引特征集合?
【发布时间】:2015-11-27 05:58:36
【问题描述】:

查看 Geotools FeatureCollection 的文档时,性能选项小节说明:

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

注意这在空间查询作为内容时表现不佳 未编入索引。

稍后它会推荐SpatialIndexFeatureCollection 以加快查询速度:

SpatialIndexFeatureCollection:使用空间索引来保持 在 MapLayer 中快速视觉显示的内容;你不能添加更多 使用此功能集合后的内容

DataUtilities.source( featureCollection ) 将换行 SpatialIndexFeatureSource 中的 SpatialIndexFeatureCollection 是 能够利用空间索引。

给出的例子是:

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

【问题讨论】:

    标签: java geotools opengis


    【解决方案1】:

    SpatialIndexFeatureCollection 现在实现了 subCollection 方法。请参阅PR here。我没有机会反向移植这些更改,但未来的版本现在将按照您预期的方式运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2015-10-31
      • 2021-07-15
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多