【20090312-02】插入和删除Featureclass中feature的几种方法(VB.Net源码)

ttp://bbs.esrichina-bj.cn/ESRI/thread-39785-1-1.html

本文对featureclass中插入和删除feature的几种方法进行了比较。
一、插入单个feature,方法1和方法2差别不大
方法1:
''' <summary>
  ''' 向featureclass中插入feature
  ''' </summary>
  ''' <param name="pFeatureClass">要插入的featureclass</param>
  ''' <remarks></remarks>
  Private Sub InsertFeature(ByVal pFeatureClass As IFeatureClass, ByVal pGeo As IGeometry)
    If pFeatureClass Is Nothing Then Exit Sub
    Dim pFeatBuf As IFeatureBuffer = pFeatureClass.CreateFeatureBuffer()
    Dim pFeat As IFeature = pFeatBuf
    Dim pFeatCur As IFeatureCursor = pFeatureClass.Insert(True)
    pFeat.Shape = pGeo
    pFeatCur.InsertFeature(pFeatBuf)
    pFeatCur.Flush()
  End Sub

方法2:
  ''' <summary>
  ''' 向featureclass中插入feature
  ''' </summary>
  ''' <param name="pFeatureClass">要插入的featureclass</param>
  ''' <remarks></remarks>
  Private Sub InsertFeature1(ByVal pFeatureClass As IFeatureClass, ByVal pGeo As IGeometry)
    If pFeatureClass Is Nothing Then Exit Sub
    Dim pFeature As IFeature = pFeatureClass.CreateFeature()
    pFeature.Shape = pGeo
    pFeature.Store()
  End Sub


二、批量插入feature,如果用feature.store,一个个插入,较之buffer方法,会慢很多
方法1:
  ''' <summary>
  ''' 向featureclass中批量插入features
  ''' </summary>
  ''' <param name="pFeatureClass">要插入的featureclass</param>
  ''' <remarks>批量插入features,用buffer的方法,要比循环一个个Store的方法快</remarks>
  Public Sub InsertFeatures(ByVal pFeatureClass As IFeatureClass, ByVal pGeos As List(Of IGeometry))
    If pFeatureClass Is Nothing Then Exit Sub
    Dim pFeatBuf As IFeatureBuffer = pFeatureClass.CreateFeatureBuffer()
    Dim pFeat As IFeature = pFeatBuf
    Dim pFeatCur As IFeatureCursor = pFeatureClass.Insert(True)
    For i As Integer = 0 To pGeos.Count - 1
      pFeat.Shape = pGeos(i)
      pFeatCur.InsertFeature(pFeatBuf)
      ''避免一次flush,导致速度过慢
      If i Mod 100 = 0 Then
        pFeatCur.Flush()
      End If
    Next
    pFeatCur.Flush()
  End Sub
三、删除feature,一个个删除就用IFeature.Delete方法即可,此处不再赘述,只写一种批量删除的方法,用于ITable是真对数据库进行操作的,所以速度很快
其中方法2是行不通的,会弹出“集合中找不到此项目”的错误。
方法1:
  ''' <summary>
  ''' 删除某featurelayer中所有feature
  ''' </summary>
  ''' <param name="pLayer">操作的涂层</param>
  ''' <remarks>该方法可以给一个queryfilter,进行删除符合条件的features</remarks>
  Private Sub DeleteAllFeatures(ByVal pLayer As IFeatureLayer)
    Dim pTable As ITable = pLayer.FeatureClass
    pTable.DeleteSearchedRows(Nothing)
  End Sub
方法2:
  ''' <summary>
  ''' 删除featurecursor中的features
  ''' </summary>
  ''' <param name="pFeatureCursor"></param>
  ''' <remarks>这种方法是行不通的,至少92是这样的,没有实现该方法</remarks>
  Private Sub DeleteFeatures(ByVal pFeatureCursor As IFeatureCursor)
    pFeatureCursor.DeleteFeature()
  End Sub

 

 

补充:

 

其实你考虑的东西少了....我来补充点吧,多多指教啊
1)关于插入的,如果是批量插入Annotation featureclass,如将CAD Text转到SDE的Annotation featureclass,用上面提到的2种方法是很慢的,50000多条record要1个小时。用IFDOGraphicsLayer进行插入操作则要快上好多,50000多条只要10多分钟。具体方法参见ESRI的例子Text2AnnoSample.

2)关于删除feature,用pTable.DeleteSearchedRows(Nothing)来批量删除(除Anno FeatureClass外)无疑是效率很高的,但如果是删除Anno Featureclass里的features, 速度还是很慢。删除50000多条要接近1个小时.而用IFeatureEdit.DeleteSet删除似乎效果好很多,相比有60%的效率提升.下面是代码示例,用的是vb6...凑合着用吧

  Dim pDeleteSet As esriSystem.ISet
    Set pDeleteSet = New esriSystem.Set
    Dim pFeature As IFeature
    Dim pFeatureCursor As IFeatureCursor
    Set pFeatureCursor = pFeatureClass.Search(Nothing, False)
   
    Set pFeature = pFeatureCursor.NextFeature
    Do While Not pFeature Is Nothing
        pDeleteSet.Add pFeature
        Set pFeature = pFeatureCursor.NextFeature
    Loop
   
    pDeleteSet.Reset
   
    Dim pFeatureEdit As IFeatureEdit
    Set pFeatureEdit = pDeleteSet.Next
    Do While Not pFeatureEdit Is Nothing
        pFeatureEdit.DeleteSet pDeleteSet
        Set pFeatureEdit = pDeleteSet.Next
    Loop

你可能感兴趣的:(VB.NET)