How to convert a shapefile to a file geodatabase


The following example creates a new File geodatabase on your C drive, then converts a Shapefile into feature dataset containing a feature class in the new File geodatabase. The "counties" shapefile, available with the developer kit samples data, is converted in the code below.
To successfully run this script, you may need to modify the path to the input coverage data to reflect where you installed the developer's samples.

How to use

  1. Modify the code to fit your data.
  2. Paste the code into your VBA Application.
[VBA]
Sub LoadShps()
    
    ' +++ Create a new feature datset name object for the output File feature dataset, call
    ' +++ it "USA"
    Dim pOutFileWorkspaceFactory As IWorkspaceFactory2
    Set pOutFileWorkspaceFactory = New FileGDBWorkspaceFactory
    
    Dim pOutFileWorkspaceName As IWorkspaceName
    Set pOutFileWorkspaceName = New WorkspaceName
    
    Set pOutFileWorkspaceName = pOutFileWorkspaceFactory.Create("C:\data\", "USA_Data", Nothing, 0)
    
    Dim pOutFileFeatDSName As IFeatureDatasetName
    Set pOutFileFeatDSName = New FeatureDatasetName
    
    Dim pFileDSname As IDatasetName
    Set pFileDSname = pOutFileFeatDSName
    Set pFileDSname.WorkspaceName = pOutFileWorkspaceName
    pFileDSname.Name = "USA"
    
    ' +++ Get the name object for the input shapefile workspace
    Dim pInShpWorkspaceName As IWorkspaceName
    Set pInShpWorkspaceName = New WorkspaceName
    pInShpWorkspaceName.PathName = "C:\Program Files\ArcGIS\DeveloperKit10.0\Samples\data\usa"
    pInShpWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory.1"
    
    Dim pInShpFeatCLSNm As IFeatureClassName
    Set pInShpFeatCLSNm = New FeatureClassName
    
    Dim pShpDatasetName As IDatasetName
    Set pShpDatasetName = pInShpFeatCLSNm
    pShpDatasetName.Name = "counties"
    Set pShpDatasetName.WorkspaceName = pInShpWorkspaceName
    
    ' +++ create the new output FeatureClass name object that will be passed
    ' +++ into the conversion function
    Dim pOutputDSName As IDatasetName
    Dim pOutputFCName As IFeatureClassName
    Set pOutputFCName = New FeatureClassName
    Set pOutputDSName = pOutputFCName
    Dim pInDSNAme As IDatasetName
    
    ' +++ Set the new FeatureClass name to be the same as the input FeatureClass name
    Set pInDSNAme = pInShpFeatCLSNm
    pOutputDSName.Name = "counties"
    ' +++ Open the input Shapefile FeatureClass object, so that we can get its fields
    Dim pName As IName
    Dim pInShpFeatCls As IFeatureClass
    Set pName = pInShpFeatCLSNm
    Set pInShpFeatCls = pName.Open
    
    ' +++ Get the fields for the input feature class and run them through
    ' +++ field checker to make sure there are no illegal or duplicate field names
    Dim pOutFileFlds As IFields
    Dim pInShpFlds As IFields
    Dim pFldChk As IFieldChecker
    Dim i As Long
    Dim pGeoField As IField
    Dim pOutFileGeoDef As IGeometryDef
    Dim pOutFileGeoDefEdit As IGeometryDefEdit
    Set pInShpFlds = pInShpFeatCls.Fields
    Set pFldChk = New FieldChecker
    pFldChk.Validate pInShpFlds, Nothing, pOutFileFlds
    
    ' +++ Loop through the output fields to find the geometry field
    For i = 0 To pOutFileFlds.FieldCount
        If pOutFileFlds.Field(i).Type = esriFieldTypeGeometry Then
            Set pGeoField = pOutFileFlds.Field(i)
            Exit For
        End If
    Next i
    
    ' +++ Get the geometry field's geometry definition
    Set pOutFileGeoDef = pGeoField.GeometryDef
    
    ' +++ Give the geometry definition a spatial index grid count and grid size
    Set pOutFileGeoDefEdit = pOutFileGeoDef
    pOutFileGeoDefEdit.GridCount = 1
    pOutFileGeoDefEdit.GridSize(0) = 1500000
    
    ' +++ Now use IFeatureDataConverter::Convert to create the output FeatureDataset and
    ' +++ FeatureClass.
    Dim pShpToFc As IFeatureDataConverter
    Set pShpToFc = New FeatureDataConverter
    pShpToFc.ConvertFeatureClass pInShpFeatCLSNm, Nothing, pOutFileFeatDSName, _
        pOutputFCName, Nothing, pOutFileFlds, "", 1000, 0
    
    MsgBox "Load operation complete!", vbInformation
End Sub