This sample compares two methods for inserting features into a geodatabase: insert cursors and CreateFeature/Store. The primary difference between the two methods is: insert cursors bypass calling IFeature::Store, which performs all object behavior, making loading simple features much quicker. Store must be called on complex features, however, and insert cursors automatically perform this when these features are detected. In the case of complex features then, both methods yield the same performance.
The sample below loads features from a shapefile into a geodatabase feature class. For simplicity sake, the macro has been written to run within ArcMap using the selected item in the table of contents as the target feature class.
How to use
- Paste the code into VBA.
- Add a geodatabase feature class to the focus map. This is the feature classthat will receive features.
- Modify the code to point to the desired shapefile.
- Make sure the extent of the geodatabase feature class can hold all the shapefile features.
- Select the feature class in the table of contents.
- Execute the LoadFeatures macro.
- To compare times: delete all the features from the first run, change the code to use the CreateFeature routine instead of the InsertFeature routine, and execute the macro again.
Public Sub LoadFeatures()
Dim pMxDoc As IMxDocument
Dim pFeatureLayer As IFeatureLayer
Dim pInFeatureClass As IFeatureClass
Dim pOutFeatureClass As IFeatureClass
Dim pQueryFilter As IQueryFilter
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
'For simplicity sake, load features into the feature class
'selected in the table of contents
Set pMxDoc = Application.Document
If pMxDoc.SelectedItem Is Nothing Then Exit Sub
If Not TypeOf pMxDoc.SelectedItem Is IFeatureLayer Then Exit Sub
Set pFeatureLayer = pMxDoc.SelectedItem
Set pOutFeatureClass = pFeatureLayer.FeatureClass
'Open a shapefile containing the features to load into the geodatabase
Set pInFeatureClass = OpenFeatureClass("d:\data\usa", "counties")
If pInFeatureClass Is Nothing Then Exit Sub
Set pQueryFilter = New QueryFilter
Set pFeatureCursor = pInFeatureClass.Search(pQueryFilter, True)
Set pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
'The next two lines control which loading method is used
'To use an insert cursor, comment out the second line
'To use CreateFeature/Store, comment out the first line
InsertFeature pOutFeatureClass, pFeature.Shape
'CreateFeature pOutFeatureClass, pFeature.Shape
Set pFeature = pFeatureCursor.NextFeature
Loop
'Refresh the display
pMxDoc.ActiveView.PartialRefresh esriViewGeography, Nothing, Nothing
End Sub
'Method 1: an insert cursor
Private Sub InsertFeature(pFeatureClass As IFeatureClass, pGeometry As IGeometry)
Dim pFeatureBuffer As IFeatureBuffer
Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureClass.Insert(True)
Set pFeatureBuffer = pFeatureClass.CreateFeatureBuffer
Set pFeatureBuffer.Shape = pGeometry
pFeatureCursor.InsertFeature pFeatureBuffer
End Sub
'Method 2: CreateFeature/Store
Private Sub CreateFeature(pFeatureClass As IFeatureClass, pGeometry As IGeometry)
Dim pFeature As IFeature
Set pFeature = pFeatureClass.CreateFeature
Set pFeature.Shape = pGeometry
pFeature.Store
End Sub
Public Function OpenFeatureClass(strWorkspace As String, strFeatureClass As String) As IFeatureClass
On Error GoTo ErrorHandler
Dim pShpWorkspaceName As IWorkspaceName
Dim pDatasetName As IDatasetName
Dim pName As IName
'Create the workspace name object
Set pShpWorkspaceName = New WorkspaceName
pShpWorkspaceName.PathName = strWorkspace
pShpWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileworkspacefactory.1"
'Create the feature class name object
Set pDatasetName = New FeatureClassName
pDatasetName.Name = strFeatureClass
Set pDatasetName.WorkspaceName = pShpWorkspaceName
'Open the feature class
Set pName = pDatasetName
Set OpenFeatureClass = pName.Open
Exit Function
ErrorHandler:
Set OpenFeatureClass = Nothing
End Function