0

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

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

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

谢谢

4

2 回答 2

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;
于 2012-11-05T11:37:09.877 回答
0

一般来说,一个集合上可以有多个迭代器,只要它们只是读取,而不是修改集合。看到这个问题

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

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

于 2012-03-05T22:30:49.387 回答