programmatically import shapefile to FGDB dataset

1596
7
06-13-2013 07:44 PM
Gregory_KevinMorales
New Contributor III
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
Reply
0 Kudos
7 Replies
Gregory_KevinMorales
New Contributor III
found the shapefile to feature class...
Reply
0 Kudos
Gregory_KevinMorales
New Contributor III
how can i add a feature dataset in the target location?

someone? please... 🙂
Reply
0 Kudos
DuncanHornby
MVP Frequent Contributor
You could try using the IGeoProcessor and run the Featureclass to Featureclass tool to achieve this. Look at this document as there is an example in it showing you how to call the tool from the GeoProcessor.

Duncan
Reply
0 Kudos
Gregory_KevinMorales
New Contributor III
sorry duncan i don't think i have tried using iGeoProcessor...

i was hoping that i can insert some code in between maybe but i don't know what... 😄

' Open the source and target workspaces.
        Dim sourceWorkspacePath As String = "C:\\temp"
        Dim targetWorkspacePath As String = "C:\Maps.gdb"
        Dim sourceWorkspaceFactory As IWorkspaceFactory = New ShapefileWorkspaceFactoryClass()
        Dim targetWorkspaceFactory As IWorkspaceFactory = New FileGDBWorkspaceFactoryClass()
        Dim sourceWorkspace As IWorkspace = sourceWorkspaceFactory.OpenFromFile(sourceWorkspacePath, 0)
        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 targetWorkspaceDataset As IDataset = CType(targetWorkspace, IDataset)
        Dim sourceWorkspaceDatasetName As IName = sourceWorkspaceDataset.FullName
        Dim targetWorkspaceDatasetName As IName = targetWorkspaceDataset.FullName
        Dim sourceWorkspaceName As IWorkspaceName = CType(sourceWorkspaceDatasetName, IWorkspaceName)
        Dim targetWorkspaceName As IWorkspaceName = CType(targetWorkspaceDatasetName, IWorkspaceName)
Reply
0 Kudos
Gregory_KevinMorales
New Contributor III
:confused:
Reply
0 Kudos
Gregory_KevinMorales
New Contributor III
😞
google google google
Reply
0 Kudos
JosephArmbruster
New Contributor III
Assuming the geodatabase already exists, you can do something like this:

FeatureClassToFeatureClass export = new FeatureClassToFeatureClass();
export.in_features = "PATH_TO_SHAPEFILE";
export.out_path = "PATH_TO_GEODATABASE_DATASET";
export.out_name = "NEW_FEATURE_CLASS_NAME";

Geoprocessor geoprocessor = new Geoprocessor();
// set your geoprocessor environment settings here
// ...
geoprocessor.OverwriteOutput = true;

geoprocessor.Execute(export, null);

// check return / messages / etc...

Note: This code is not complete but should be good enough to get you started.
Reply
0 Kudos