如何在地理数据库中使用游标

相关链接:http://resources.esri.com/help/9.3/arcgisengine/dotnet/bdb9558a-d78d-446c-a9d8-f35f9eb44a5b.htm


该链接讲解了如何使用游标及如果释放游标,

释放游标常用的方法:

1)System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);

while (Marshal.ReleaseComObject(pInterpolationOp) != 0)

}

2)  System.Runtime.InteropServices.Marshal.FinalReleaseComObject(featureCursor);

3)

    using(ComReleaser comReleaser = new ComReleaser())
    {

        ICursor cursor = queryDef.Evaluate();
        comReleaser.ManageLifetime(cursor);
        int parcelIdIndex = cursor.FindField("Parcels.PARCEL_ID");
        int ownerNameIndex = cursor.FindField("owners.OWNER_NAME");
        IRow row = null;
        while ((row = cursor.NextRow()) != null)
        {
            Console.WriteLine("Parcel {0} owned by {1}", row.get_Value(parcelIdIndex)
                , row.get_Value(ownerNameIndex));
        }
    }

个人建议用第三种方法释放游标

在使用Featureclass创建游标的时候,Recycling参数怎么设置?(请参照下文,下文转自论坛)

IFeatureClass的Search方法大家经常用到,很多人对Search方法的其中一个参数bool Recycling不是很理解。
目前网上关于这个参数的意义的解释有两个版本:
1.第一版本是IsNotNull大牛的:
http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=13461
他提出的观点的是:参数Recycling为True的时候是传引用,为False的时候为传值
2.第二个版本是Echo兄的:
http://gis.cnblogs.com/home.aspx?page=3
他提出的观点是:参数Recycling为True的时候游标对象(Ifeaturecursor)是只包含一条记录,为False的时候游标对象(Ifeaturecursor)为10条记录(假设Featureclass有10条记录)

我经过测试提出个人的观点:
1.pFeatureCursor存储的是所有符合条件的Feature对象的引用,
2.Recycling的意思是回收,参数Recycling为True的时当执行这个方法IFeature pFeature=pFeatureCursor.NextFeature()上一条记录的值在内存中所占的地址就会被销毁回收,为False的时候当执行这个方法IFeature pFeature=pFeatureCursor.NextFeature()上一条记录的值依然存在在内存中。
Ok,接下来以实例证明本观点:

1.参数Recycling设置为True
           IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass;
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
            //IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);

            List pFeatures = new List();
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                pFeatures.Add(pFeature);
                pFeature = pFeatureCursor.NextFeature();
            }
            
            for (int i = 0; i < pFeatures.Count; i++)
            {
                MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString());
            }
分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为TRUE也就是说每执行一个NextFeautre方法上一条记录的Feature值在内存中被回收,所以到最后遍历pFeatures集合的时候所有的IFeature引用指向的Feature对象都为Null。所以会引发一下错误

2.参数Recycling设置为False
           IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass;
            //IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);

            List pFeatures = new List();
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                pFeatures.Add(pFeature);
                pFeature = pFeatureCursor.NextFeature();
            }
            
            for (int i = 0; i < pFeatures.Count; i++)
            {
                MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString());
            }
分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为False也就是说每执行一个NextFeautre方法上一条记录的Feature值在内存中依然存在,所以到最后遍历pFeatures集合的时候所有的IFeature引用指向的Feature对象都依然存在。所以会执行的很Happy

一般情况设置成True,True速度快,也不占用大量内存。如果要做一些插入Feature等操作必须用false否则都为Null值!

具体参照下面的英文:

The recycling parameter controls row object allocation behavior.Recycling cursors rehydrate a single feature object on each fetch and can be used to optimize read-only access, for example, when drawing. It is illegal to maintain a reference on a feature object returned by a recycling cursor across multiple calls to NextFeature on the cursor. Features returned by a recycling cursor should not be modified. Non-recycling cursors return a separate feature object on each fetch. The features returned by a non-recycling cursor may be modified and stored with polymorphic behavior.

The Geodatabase guarantees "unique instance semantics" on non-recycling feature objects fetched during an edit session. In other words, if the feature retrieved by a search cursor has already been instantiated and is being referenced by the calling application, then a reference to the existing feature object is returned.

Non-recycling feature cursors returned from the Search method *MUST* be used when copying features from the cursor into an insert cursor of another class.  This is because a recycling cursor reuses the same geometry and under some circumstances all of the features inserted into the insert cursor may have the same geometry.  Using a non-recycling cursor ensures that each geometry is unique.

参考链接:http://gis.stackexchange.com/questions/15775/is-it-permissible-to-update-features-after-the-feature-cursor-has-been-released


Recycling

你可能感兴趣的:(ArcEngine)