【问题标题】:Geotools nested loop over SimpleFeatureCollectionGeotools 在 SimpleFeatureCollection 上嵌套循环
【发布时间】:2012-03-05 21:54:27
【问题描述】:

我想在 SimpleFeatureCollection 上做一个嵌套循环。对于每个点,我都需要找到它的邻居并处理它们。

然而,SimpleFeatureCollection 只允许迭代器,但不允许数组访问,这使得(至少看起来如此)不可能实现嵌套循环。这个迭代器没有 previous() 方法,所以我不能重置它并在同一个集合上使用两个迭代器。

所以我想知道是否有其他方法可以通过索引访问功能。

谢谢

【问题讨论】:

    标签: java collections iterator nested-loops geotools


    【解决方案1】:

    这里有一个代码示例: http://docs.geotools.org/latest/userguide/library/main/collection.html#join

    它展示了如何嵌套循环:

    void polygonInteraction() {
        SimpleFeatureCollection polygonCollection = null;
        SimpleFeatureCollection fcResult = null;
        final SimpleFeatureCollection found = FeatureCollections.newCollection();
    
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
        SimpleFeature feature = null;
    
        Filter polyCheck = null;
        Filter andFil = null;
        Filter boundsCheck = null;
    
        String qryStr = null;
    
        SimpleFeatureIterator it = polygonCollection.features();
        try {
            while (it.hasNext()) {
                feature = it.next();
                BoundingBox bounds = feature.getBounds();
                boundsCheck = ff.bbox(ff.property("the_geom"), bounds);
    
                Geometry geom = (Geometry) feature.getDefaultGeometry();
                polyCheck = ff.intersects(ff.property("the_geom"), ff.literal(geom));
    
                andFil = ff.and(boundsCheck, polyCheck);
    
                try {
                    fcResult = featureSource.getFeatures(andFil);
                    // go through results and copy out the found features
                    fcResult.accepts(new FeatureVisitor() {
                        public void visit(Feature feature) {
                            found.add((SimpleFeature) feature);
                        }
                    }, null);
                } catch (IOException e1) {
                    System.out.println("Unable to run filter for " + feature.getID() + ":" + e1);
                    continue;
                }
    
            }
        } finally {
            it.close();
        }
    }
    

    如果你想忽略一些你已经访问过的功能,并跳过内容::

    HashSet<FeatureId> skip = new HashSet<FeatureId>();
    ...
    if( skip.contains( feature.getId() ) ) continue;
    

    【讨论】:

      【解决方案2】:

      一般来说,一个集合可以有多个迭代器,前提是它们只是读取,而不是修改集合。见this question

      我希望SimpleFeatureCollection 不是例外!

      对于嵌套循环,您可以为每次通过内部循环的运行创建另一个迭代器;您不需要“重置”前一个。

      【讨论】:

      • 谢谢,DNA。问题是我实际上需要修改这个集合(我需要通过更改某个属性来标记已经处理的点)。我有点想通了,怎么做我想要的,它看起来很丑。基本上,我正在创建一个新集合并覆盖迭代器。无论如何,谢谢
      • 只需在 HashSet 中记录你已经命中的特征的特征 id。注意 GeoTools 的这种结构允许您处理大量数据,如果您知道自己正在处理少量数据,请将要素复制到 List
      猜你喜欢
      • 2018-09-27
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 2021-12-30
      相关资源
      最近更新 更多