幾種批量刪除feature的方法
1.批量刪除feature ----ITable.DeleteSearchedRows method
對于一般的featureClass(不包括Anno FeatureClass),ITable.DeleteSearchedRows能快速有效的刪除Features.
尤其是大數據量情況下,其效率遠比逐個要素刪除要快得多。
Public Sub DeleteFeature(pFeaWorkspace As IFeatureWorkspace, pfeatName As String)
'Delete feature class
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeaWorkspace.OpenFeatureClass(pfeatName)
On Error Resume Next
If pFeatureClass Is Nothing Then
Exit Sub
End If
Dim pTable As ITable
Set pTable = pFeatureClass
pTable.DeleteSearchedRows Nothing
End Sub
'Delete feature class
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeaWorkspace.OpenFeatureClass(pfeatName)
On Error Resume Next
If pFeatureClass Is Nothing Then
Exit Sub
End If
Dim pTable As ITable
Set pTable = pFeatureClass
pTable.DeleteSearchedRows Nothing
End Sub
2. 批量刪除Annotation featureclass里的features ---IFeatureEdit.DeleteSet method.
ITable.DeleteSearchedRows用來刪除Annotation featureclass要素的效果并不好,很多時候是根本沒刪除到數據.
IFeatureEdit.DeleteSet方法在這時候能很好的派上用場,由于是DeleteSet,所以遇到某些相關聯要素時,不會發生像
IFeatureCursor.nextfeature單個刪除時找不到要素的問題。
Public Sub DeleteAnnoFeature(pFeaWorkspace As IFeatureWorkspace, pfeatName As String)
'Delete feature class
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeaWorkspace.OpenFeatureClass(pfeatName)
On Error Resume Next
If pFeatureClass Is Nothing Then
Exit Sub
End If
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
End Sub
'Delete feature class
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeaWorkspace.OpenFeatureClass(pfeatName)
On Error Resume Next
If pFeatureClass Is Nothing Then
Exit Sub
End If
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
End Sub
3. 批量刪除要素的一般方法.
Public Sub DeleteAnnoFeature()Sub DeleteAnnoFeature(pFeaWorkspace As IFeatureWorkspace, pfeatName As String)
'Delete feature class
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeaWorkspace.OpenFeatureClass(pfeatName)
On Error Resume Next
If pFeatureClass Is Nothing Then
Exit Sub
End If
Dim pFeature As IFeature
Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureClass.Search(Nothing, False)
Set pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
pFeature.delete
Set pFeature = pFeatureCursor.NextFeature
Loop
set pFeatureClass=nothing
End Sub
'Delete feature class
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeaWorkspace.OpenFeatureClass(pfeatName)
On Error Resume Next
If pFeatureClass Is Nothing Then
Exit Sub
End If
Dim pFeature As IFeature
Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureClass.Search(Nothing, False)
Set pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
pFeature.delete
Set pFeature = pFeatureCursor.NextFeature
Loop
set pFeatureClass=nothing
End Sub
4. 在FeatureLayer上刪除features...利用IFeatureEdit.DeleteSet
Public Sub DeleteSelectedFeatures()
Dim pDeleteSet As ISet
Dim pEditLayers As IEditLayers
Dim pEditor As IEditor
Dim pEnumLayer As IEnumLayer
Dim pFeature As IFeature
Dim pFeatureEdit As IFeatureEdit
Dim pFeatureCursor As IFeatureCursor
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pID As New UID
Dim pInvalidArea As IInvalidArea
Dim pSelectionSet As ISelectionSet
Dim bInOperation As Boolean
Dim DeletedFeatureCount As Integer
Dim LayerCount As Integer
On Error GoTo ErrorHandler
''Get a handle to the Editor extension
pID = "esriEditor.Editor"
Set pEditor = Application.FindExtensionByCLSID(pID)
If Not pEditor.EditState = esriStateEditing Then Exit Sub
Set pEditLayers = pEditor
If pEditor.SelectionCount = 0 Then Exit Sub
'Set up the InvalidArea object
Set pInvalidArea = New InvalidArea
Set pInvalidArea.Display = pEditor.Display
'Loop through the selected features and delete them
pEditor.StartOperation
bInOperation = True
pID = "{40A9E885-5533-11D0-98BE-00805F7CED21}" 'IGeoFeatureLayer
Set pEnumLayer = pEditor.Map.Layers(pID, True)
pEnumLayer.Reset
Set pFeatureLayer = pEnumLayer.Next
Do While Not pFeatureLayer Is Nothing
'Check if feature layer is editable
If pEditLayers.IsEditable(pFeatureLayer) Then
Set pFeatureSelection = pFeatureLayer
Set pSelectionSet = pFeatureSelection.SelectionSet
''Check if there is a selection
If Not pSelectionSet.Count = 0 Then
'Add each feature to a Set
'and delete the set - need a Set incase there are related features
Set pDeleteSet = New esriSystem.Set
pSelectionSet.Search Nothing, False, pFeatureCursor
Set pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
pInvalidArea.Add pFeature
pDeleteSet.Add pFeature
DeletedFeatureCount = DeletedFeatureCount + 1
Set pFeature = pFeatureCursor.NextFeature
Loop
pDeleteSet.Reset
Set pFeatureEdit = pDeleteSet.Next
Do While Not pFeatureEdit Is Nothing
pFeatureEdit.DeleteSet pDeleteSet
Set pFeatureEdit = pDeleteSet.Next
Loop
End If
End If
Set pFeatureLayer = pEnumLayer.Next
Loop
If DeletedFeatureCount = 0 Then
pEditor.AbortOperation
Else
pEditor.StopOperation ("Delete")
bInOperation = False
End If
'Clear the selection and refresh the display
pEditor.Map.ClearSelection
pInvalidArea.Invalidate esriAllScreenCaches
Exit Sub
ErrorHandler:
If bInOperation Then
pEditor.AbortOperation
End If
End Sub
Dim pDeleteSet As ISet
Dim pEditLayers As IEditLayers
Dim pEditor As IEditor
Dim pEnumLayer As IEnumLayer
Dim pFeature As IFeature
Dim pFeatureEdit As IFeatureEdit
Dim pFeatureCursor As IFeatureCursor
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pID As New UID
Dim pInvalidArea As IInvalidArea
Dim pSelectionSet As ISelectionSet
Dim bInOperation As Boolean
Dim DeletedFeatureCount As Integer
Dim LayerCount As Integer
On Error GoTo ErrorHandler
''Get a handle to the Editor extension
pID = "esriEditor.Editor"
Set pEditor = Application.FindExtensionByCLSID(pID)
If Not pEditor.EditState = esriStateEditing Then Exit Sub
Set pEditLayers = pEditor
If pEditor.SelectionCount = 0 Then Exit Sub
'Set up the InvalidArea object
Set pInvalidArea = New InvalidArea
Set pInvalidArea.Display = pEditor.Display
'Loop through the selected features and delete them
pEditor.StartOperation
bInOperation = True
pID = "{40A9E885-5533-11D0-98BE-00805F7CED21}" 'IGeoFeatureLayer
Set pEnumLayer = pEditor.Map.Layers(pID, True)
pEnumLayer.Reset
Set pFeatureLayer = pEnumLayer.Next
Do While Not pFeatureLayer Is Nothing
'Check if feature layer is editable
If pEditLayers.IsEditable(pFeatureLayer) Then
Set pFeatureSelection = pFeatureLayer
Set pSelectionSet = pFeatureSelection.SelectionSet
''Check if there is a selection
If Not pSelectionSet.Count = 0 Then
'Add each feature to a Set
'and delete the set - need a Set incase there are related features
Set pDeleteSet = New esriSystem.Set
pSelectionSet.Search Nothing, False, pFeatureCursor
Set pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
pInvalidArea.Add pFeature
pDeleteSet.Add pFeature
DeletedFeatureCount = DeletedFeatureCount + 1
Set pFeature = pFeatureCursor.NextFeature
Loop
pDeleteSet.Reset
Set pFeatureEdit = pDeleteSet.Next
Do While Not pFeatureEdit Is Nothing
pFeatureEdit.DeleteSet pDeleteSet
Set pFeatureEdit = pDeleteSet.Next
Loop
End If
End If
Set pFeatureLayer = pEnumLayer.Next
Loop
If DeletedFeatureCount = 0 Then
pEditor.AbortOperation
Else
pEditor.StopOperation ("Delete")
bInOperation = False
End If
'Clear the selection and refresh the display
pEditor.Map.ClearSelection
pInvalidArea.Invalidate esriAllScreenCaches
Exit Sub
ErrorHandler:
If bInOperation Then
pEditor.AbortOperation
End If
End Sub
-----------------------------------------------------------
佛对我说:你心里有尘。我用力的拭擦。