本文对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