Geotools嵌套在SimpleFeatureCollection上的循环

时间:2012-03-05 21:54:27

标签: java collections iterator nested-loops geotools

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

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

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

由于

2 个答案:

答案 0 :(得分:2)

这里有一个代码示例:     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;

答案 1 :(得分:0)

通常,只要它们只是读取而不是修改集合,就可以在集合上有多个迭代器。请参阅this question

我希望SimpleFeatureCollection不是规则的例外!

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