Select to view content in your preferred language

Problem with creating new feature class in Personal or File Geodatabase

863
3
07-26-2010 07:03 AM
AlzbetaBrychtova
New Contributor
Hi everyone,

I am trying to  programmatically (vb.net) create new feature class in Personal or File Geodatabase, but even though I am doing my best, I am not able to find a mistake.
The code below works great for creating Shapefiles and I don't know how to change it for FC in geodatabase.  The problem seems to be in a part for Field creating, but don't know where.

Does anybody have idea? Thanks for all advices.

Private Sub CreateFeature()
        Dim pWorkSpaceFactory1 As IWorkspaceFactory2
        'Dim pWorkspaceName As IWorkspaceName
        Dim pFeatureWorkspace1 As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace

        Const strTemp As String = "c:\"
        Const strShapeFieldName As String = "Shape"
        Const strFeatureClassName As String = "NewFeatureClass1"

        ' For shapefiles - works fine
        pWorkSpaceFactory1 = New ShapefileWorkspaceFactory 'Creates workspace for shapefiles
        pFeatureWorkspace1 = pWorkSpaceFactory1.OpenFromFile(strTemp, 0) 'Opens workspace for shapefiles

        ''For FC in Geodatabase - doesn't work 
        'pWorkSpaceFactory1 = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory() 'Creates workspace for personal geodatabase
        'Try
        '    pWorkspaceName = pWorkSpaceFactory1.Create(strTemp, "temp", Nothing, 0) 'Creates Personal Geodatabase
        'Catch ex As Exception
        'End Try
        'pFeatureWorkspace1 = pWorkSpaceFactory1.OpenFromFile(strTemp & "temp.mdb", 0) 'Opens workspace for personal geodatabase

        Dim pFields As IFields = New FieldsClass
        Dim pFieldsEdit As IFieldsEdit = CType(pFields, IFieldsEdit)
        pFieldsEdit.FieldCount_2 = 3

        Dim pField As IField
        Dim pFieldEdit As IFieldEdit

        pField = New Field
        pFieldEdit = CType(pField, IFieldEdit)
        pFieldEdit.Name_2 = "OBJECTID"
        pFieldEdit.AliasName_2 = "OBJECT ID"
        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID
        pFieldsEdit.Field_2(0) = pField


        pField = New Field
        pFieldEdit = CType(pField, IFieldEdit)

        Dim geometryDef As IGeometryDef = New GeometryDefClass
        Dim geometryDefEdit As IGeometryDefEdit = CType(geometryDef, IGeometryDefEdit)
        geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline
        geometryDefEdit.GridCount_2 = 1
        geometryDefEdit.HasM_2 = False
        geometryDefEdit.HasZ_2 = False

        pFieldEdit.Name_2 = strShapeFieldName
        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry
        pFieldEdit.GeometryDef_2 = geometryDef
        pFieldEdit.IsNullable_2 = True
        pFieldEdit.Required_2 = True
        pFieldsEdit.Field_2(1) = pField


        pField = New Field
        pFieldEdit = CType(pField, IFieldEdit)
        pFieldEdit.Name_2 = "txt_field"
        pFieldEdit.AliasName_2 = "Field for text information"
        pFieldEdit.Editable_2 = True
        pFieldEdit.IsNullable_2 = False
        pFieldEdit.Precision_2 = 2
        pFieldEdit.Scale_2 = 5
        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble
        pFieldsEdit.Field_2(2) = pField


        pFeatureWorkspace1.CreateFeatureClass(strFeatureClassName, pFields, Nothing, Nothing, esriFeatureType.esriFTSimple, strShapeFieldName, Nothing)

    End Sub
0 Kudos
3 Replies
JamesMacKay
Deactivated User
Hi Alzbeta,

What is the error code returned by the CreateFeatureClass call?

Also - although I don't think this is the source of the error - when you are creating a geodatabase feature class (as opposed to a shapefile, when it doesn't matter) you should typically be passing a value UID into the CLSID parameter. There are a few ways to do this, but typically I create a new feature class description, like so:

Dim objClassDescription as IObjectClassDescription = New FeatureClassDescription


Then use the IObjectClassDescription.InstanceCLSID property to get the CLSID.

Cheers,
James
0 Kudos
LindaMcCafferty
Emerging Contributor
Here is code that I use to create a filegeodatabase using C#

Could it be that you are using accessworkspace instead of the FileGDBWorkspaceFactoryClass?

IWorkspaceFactory2 fgdbWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
fgdbWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
fgdbWorkspaceFactory.Create(ClsGlobalVariables.User_Data_Path + Environment.UserName + "\\exhibits\\", "fileGDBForFacilities", null, 0);

Hope this helps!  Linda
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's a function I've used in creating featureclasses. I use the IGXDialog.DoModalSave to get the parameters to send to the function, along with the fields that were created. In this code, I'm creating the fields from an existing dataset  (pPointFLayer) and also use its spatial reference to create a line featurelayer.


    dim OutputLocation As String
    dim OutputFCName As String
    dim GDBType As String
    Dim pGxDialog As ESRI.ArcGIS.CatalogUI.IGxDialog = New ESRI.ArcGIS.CatalogUI.GxDialog
    Dim pShapeFilter As ESRI.ArcGIS.Catalog.IGxObjectFilter = New ESRI.ArcGIS.Catalog.GxFilterShapefiles
    Dim pPGDBFilter As ESRI.ArcGIS.Catalog.IGxObjectFilter = New ESRI.ArcGIS.Catalog.GxFilterPGDBFeatureClasses
    Dim pFilterCollection As ESRI.ArcGIS.Catalog.IGxObjectFilterCollection
    Dim pGeoDataset As ESRI.ArcGIS.Geodatabase.IGeoDataset
    Dim pSR As ESRI.ArcGIS.Geometry.ISpatialReference
    Dim pSpatRes As ESRI.ArcGIS.Geometry.ISpatialReferenceResolution
    Dim pFields As ESRI.ArcGIS.Geodatabase.IFields = New ESRI.ArcGIS.Geodatabase.Fields
    Dim pFieldsEdit As ESRI.ArcGIS.Geodatabase.IFieldsEdit
    Dim pField As ESRI.ArcGIS.Geodatabase.IField
    Dim pFieldEdit As ESRI.ArcGIS.Geodatabase.IFieldEdit
    Dim pGeomDefEdit As ESRI.ArcGIS.Geodatabase.IGeometryDefEdit = New ESRI.ArcGIS.Geodatabase.GeometryDef
    Dim pClone As ESRI.ArcGIS.esriSystem.IClone
    Dim pLineFLayer As ESRI.ArcGIS.Carto.IFeatureLayer = New ESRI.ArcGIS.Carto.FeatureLayer

    pGxDialog.Title = "Output Feature Class Name"
    pFilterCollection = pGxDialog
    pFilterCollection.AddFilter(pPGDBFilter, True)
    pFilterCollection.AddFilter(pShapeFilter, False)


    If pGxDialog.DoModalSave(0) Then
      OutputLocation = pGxDialog.FinalLocation.FullName
      OutputFCName = pGxDialog.Name
      GDBType = pGxDialog.FinalLocation.Category
    End If

      pGeoDataset = pPointFLayer
      pSR = pGeoDataset.SpatialReference
      pSpatRes = pSR
      pSpatRes.ConstructFromHorizon()

      'create the geometry field
      With pGeomDefEdit
        .GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline
        .HasM_2 = False
        .HasZ_2 = False
        .SpatialReference_2 = pSR
      End With

      'Create fields for output FC
      pFieldsEdit = pFields
      pField = New ESRI.ArcGIS.Geodatabase.Field
      pFieldEdit = pField
      With pFieldEdit
        If GDBType = "Folder" Then
          .Name_2 = "FID"
        Else
          .Name_2 = "ObjectID"
        End If
        .Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID
      End With
      pFieldsEdit.AddField(pField)

      pFieldsEdit = pFields
      pField = New ESRI.ArcGIS.Geodatabase.Field
      pFieldEdit = pField
      With pFieldEdit
        .Name_2 = "SHAPE"
        .Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry
        .GeometryDef_2 = pGeomDefEdit
      End With
      pFieldsEdit.AddField(pField)

      For i As Integer = 0 To pPointFLayer.FeatureClass.Fields.FieldCount - 1
        If pPointFLayer.FeatureClass.Fields.Field(i).Type < 6 Then
          pClone = pPointFLayer.FeatureClass.Fields.Field(i)
          pFieldsEdit.AddField(pClone.Clone)
        End If
      Next

    pLineFLayer.FeatureClass = CreateFeatureClass(OutputLocation, OutputFCName, GDBType, pFields)

  Friend Function CreateFeatureClass(ByVal WorkSpaceName As String, ByVal DatasetName As String, ByVal GDBType As String, ByVal pFields As ESRI.ArcGIS.Geodatabase.IFields) As ESRI.ArcGIS.Geodatabase.IFeatureClass

    Dim pWSFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory
    Dim pFWSpace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace
    Dim pDataset As ESRI.ArcGIS.Geodatabase.IDataset

    Try

      Select Case GDBType 'the values for this vary with the computer's localization. This is the English language version.
        Case "Folder"
          pWSFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory
        Case "Personal Geodatabase"
          pWSFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory
        Case "File Geodatabase"
          pWSFactory = New ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory
        Case Else
          System.Windows.Forms.MessageBox.Show("Incorrect GDBType!", "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
          Return Nothing
      End Select
      pFWSpace = pWSFactory.OpenFromFile(WorkSpaceName, 0)

      Try
        pDataset = pFWSpace.OpenFeatureClass(DatasetName)
      Catch ex As Exception
        'do nothing here
      End Try
      If Not pDataset Is Nothing Then
        If pDataset.CanDelete Then
          pDataset.Delete()
        Else
          System.Windows.Forms.MessageBox.Show("Cannot delete existing dataset " & pDataset.Name)
          Return Nothing
        End If
      End If

      Return pFWSpace.CreateFeatureClass(DatasetName, pFields, Nothing, Nothing, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, "Shape", "")

    Catch ex As Exception
      MessageBox.Show(ex.Message)
      Return Nothing
    End Try
  End Function
0 Kudos