AnsweredAssumed Answered

Convert shapefile to SDE

Question asked by qemmal on Dec 21, 2011
I've tested function ConvertShapefileToFeatureClass() which converts dedicated shapefile to gdb (file geodatabase) and it works very well. As a result of this function, the shapefile were converted into feature class in the file geodatabase.

Now, I want to save the converted shapefile into SDE database (in SQL server). I use the same function and modify the 'targetWsPath' to SDE connection.

Unfortunately an error occured, 'create output feature class failed'.

here i attach my code and the error. really appreciate if someone can tell me what's happening :(

Public Sub ConvertShapefileToFCSDE()
        ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.Engine)
        Dim aoInitialize As IAoInitialize
        aoInitialize = New AoInitialize

        aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine)

        ' Open the source and target workspaces.
        Dim sourceWorkspacePath As String = "C:\projek peta melaka"

        Dim pProp As IPropertySet = New PropertySet
        Dim wFac As New SdeWorkspaceFactory
        Dim targetWorkspace As IWorkspace

        Dim targetWsPath As String = "Provider=SQLNCLI10;" + _
          "Data Source=" + My.Settings.Server + ";" + _
          "Initial Catalog=" + My.Settings.Database + ";" + _
          "User ID=" + My.Settings.User + ";" + _
          "Password=" + My.Settings.Password + ";"
        pProp.SetProperty("Server", My.Settings.Server)
        pProp.SetProperty("Instance", My.Settings.Instance)
        pProp.SetProperty("Database", My.Settings.Database)
        pProp.SetProperty("User", My.Settings.User)
        pProp.SetProperty("Password", My.Settings.Password)
        pProp.SetProperty("Version", My.Settings.Version)
        targetWorkspace = wFac.Open(pProp, 0)
        WS = targetWorkspace

        'Dim targetWorkspacePath As String = "C:\projek peta melaka\Melaka.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.Open(targetWsPath, 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)

        ' 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 = "tsempdm.shp"
        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 = "tsemp"
        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)

        '-----------
        ' Cast the IGeometryDef to the IGeometryDefEdit interface.
        Dim targetGeometryDefEdit As IGeometryDefEdit = CType(targetGeometryDef, IGeometryDefEdit)

        ' Set the IGeometryDefEdit properties.
        targetGeometryDefEdit.GridCount_2 = 1
        targetGeometryDefEdit.GridSize_2(0) = 0.75

        ' 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.
        Dim invalidObjectInfo As IInvalidObjectInfo = Nothing
        enumInvalidObject.Reset()

        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)
            invalidObjectInfo = enumInvalidObject.Next()
        Loop

    End Sub

Attachments

Outcomes