gkmorales

programmatically import shapefile to FGDB dataset

Discussion created by gkmorales on Jun 13, 2013
Latest reply on Jun 18, 2013 by josepharmbruster0lat
how can i import a shapefile to FGDB dataset?

hope someone can help me with this...

i have tried the code below... but it only export a feature class from FGDB to a shapefile



Public Shared Sub ConvertFeatureClassToShapefile()
        ' Open the source and target workspaces.
        Dim sourceWorkspacePath As String = "C:\Map.gdb\"
        Dim sourceWorkspaceFactory As IWorkspaceFactory = New FileGDBWorkspaceFactoryClass()
        Dim sourceWorkspace As IWorkspace = sourceWorkspaceFactory.OpenFromFile(sourceWorkspacePath, 0)

        Dim targetWorkspacePath As String = "C:\Temp"
        Dim targetWorkspaceFactory As IWorkspaceFactory = New ShapefileWorkspaceFactoryClass()
        Dim targetWorkspace As IWorkspace = targetWorkspaceFactory.OpenFromFile(targetWorkspacePath, 0)

        ' Cast the workspaces to the IDataset interface and get name objects.
        Dim sourceWorkspaceDataset As IDataset = CType(sourceWorkspace, IDataset)
        Dim sourceWorkspaceDatasetName As IName = sourceWorkspaceDataset.FullName
        Dim sourceWorkspaceName As IWorkspaceName = CType(sourceWorkspaceDatasetName, IWorkspaceName)

        Dim targetWorkspaceDataset As IDataset = CType(targetWorkspace, IDataset)
        Dim targetWorkspaceDatasetName As IName = targetWorkspaceDataset.FullName
        Dim targetWorkspaceName As IWorkspaceName = CType(targetWorkspaceDatasetName, IWorkspaceName)

        ' Create a name object for the shapefile and cast it to the IDatasetName interface.
        Dim sourceFeatureClassName As IFeatureClassName = New FeatureClassNameClass()
        Dim sourceDatasetName As IDatasetName = CType(sourceFeatureClassName, IDatasetName)
        sourceDatasetName.Name = "road"
        sourceDatasetName.WorkspaceName = sourceWorkspaceName

        ' Create a name object for the FGDB feature class and cast it to the IDatasetName interface.
        Dim targetFeatureClassName As IFeatureClassName = New FeatureClassNameClass()
        Dim targetDatasetName As IDatasetName = CType(targetFeatureClassName, IDatasetName)
        targetDatasetName.Name = "road"
        targetDatasetName.WorkspaceName = targetWorkspaceName

        ' Open source feature class to get field definitions.
        Dim sourceName As IName = CType(sourceFeatureClassName, IName)
        Dim sourceFeatureClass As IFeatureClass = CType(sourceName.Open(), IFeatureClass)

        ' Create the objects and references necessary for field validation.
        Dim fieldChecker As IFieldChecker = New FieldCheckerClass()
        Dim sourceFields As IFields = sourceFeatureClass.Fields
        Dim targetFields As IFields = Nothing
        Dim enumFieldError As IEnumFieldError = Nothing

        ' Set the required properties for the IFieldChecker interface.
        fieldChecker.InputWorkspace = sourceWorkspace
        fieldChecker.ValidateWorkspace = targetWorkspace

        ' Validate the fields and check for errors.
        fieldChecker.Validate(sourceFields, enumFieldError, targetFields)
        If Not enumFieldError Is Nothing Then
            ' Handle the errors in a way appropriate to your application.
            Console.WriteLine("Errors were encountered during field validation.")
        End If

        ' Find the shape field.
        Dim shapeFieldName As String = sourceFeatureClass.ShapeFieldName
        Dim shapeFieldIndex As Integer = sourceFeatureClass.FindField(shapeFieldName)
        Dim shapeField As IField = sourceFields.Field(shapeFieldIndex)

        ' Get the geometry definition from the shape field and clone it.
        Dim geometryDef As IGeometryDef = shapeField.GeometryDef
        Dim geometryDefClone As IClone = CType(geometryDef, IClone)
        Dim targetGeometryDefClone As IClone = geometryDefClone.Clone()
        Dim targetGeometryDef As IGeometryDef = CType(targetGeometryDefClone, IGeometryDef)

        ' Create a query filter to remove ramps, interstates and highways.
        'Dim queryFilter As IQueryFilter = New QueryFilterClass()
        'queryFilter.WhereClause = "NAME <> 'Ramp' AND PRE_TYPE NOT IN ('I', 'Hwy')"

        ' Create the converter and run the conversion.
        Dim featureDataConverter As IFeatureDataConverter = New FeatureDataConverterClass()
        Dim enumInvalidObject As IEnumInvalidObject = featureDataConverter.ConvertFeatureClass(sourceFeatureClassName, Nothing, Nothing, targetFeatureClassName, targetGeometryDef, targetFields, "", 1000, 0)

        ' Check for errors.
        enumInvalidObject.Reset()
        Dim invalidObjectInfo As IInvalidObjectInfo = enumInvalidObject.Next()
        Do While Not invalidObjectInfo Is Nothing
            ' Handle the errors in a way appropriate to the application.
            Console.WriteLine("Errors occurred for the following feature: {0}", invalidObjectInfo.InvalidObjectID)
        Loop
    End Sub

Outcomes