Select to view content in your preferred language

CreateFeatureClass error for lines but not for polygons or points

527
4
02-09-2012 05:34 AM
AxelThomas
Regular Contributor
Hi all,
I am creating shape files with simple code that works fine for polygon or point shape files. For line shape files the code however fails at the FeatureWorkspace.CreateFeatureClass command (highlighted in code below). I guess it's just a pretty dumb mistake...
Any help is appreciated!

Axel

Sub CreateShape(sDBPath As String, sOutShapeName As String, lBID As Long)

  Dim pWkSp As IWorkspace, pShWkSpF As IWorkspaceFactory, pFWkSp As IFeatureWorkspace
  Dim pOutShapeFCls As IFeatureClass
  Dim pPolygon As IPolygon, pF As IFeature
  Dim pField As IField, pFieldEdit As IFieldEdit
  Dim pFields As IFields, pFieldsEdit As IFieldsEdit
  Dim pGeomDefEdit As IGeometryDefEdit, cGeomTyp As esriGeometryType
  Dim pCLSID As UID
  Dim sFeldname As String
  Dim iNFields As Integer, iFI As Integer
 
  Set pShWkSpF = New ShapefileWorkspaceFactory
  Set pWkSp = pShWkSpF.OpenFromFile(sDBPath, 0)
  Set pFWkSp = pWkSp
  If pFWkSp Is Nothing Then Exit Sub
 
  If sOutShapeName Like "*poly*" Then
    cGeomTyp = esriGeometryPolygon
    iNFields = 13
    sFeldname = "POLY-ID"
  ElseIf sOutShapeName Like "*line*" Then
    cGeomTyp = esriGeometryPolyline
    iNFields = 17
    sFeldname = "ARC-ID"
  ElseIf sOutShapeName Like "*dot*" Then
    cGeomTyp = esriGeometryPoint
    iNFields = 14
    sFeldname = "POINT-ID"
  Else
    MsgBox "File name not accepted", vbCritical
    Exit Sub
  End If
 
  Set pFields = New Fields
  Set pFieldsEdit = pFields
  Set pField = New Field
  pFieldsEdit.FieldCount = iNFields
  iFI = 0
 
  Set pGeomDefEdit = New GeometryDef                           ' Geometry field
  With pGeomDefEdit
    .GeometryType = cGeomTyp
    .HasM = False
    .HasZ = False
    Set .SpatialReference = SetProjection       ' set in a separate function, not included in this post
  End With
       
  Set pFieldEdit = New Field
  With pFieldEdit
    .AliasName = "FID"
    .Name = "FID"
    .Type = esriFieldTypeOID
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "Shape"
    .AliasName = "Geometry"
    .Type = esriFieldTypeGeometry
    Set .GeometryDef = pGeomDefEdit
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  If Not cGeomTyp = esriGeometryLine Then
    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "AREA"
      .AliasName = "Area"
      .Precision = 20
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "PERIMETER"
      .AliasName = "Perimeter"
      .Precision = 20
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1
  End If
 
  If cGeomTyp = esriGeometryLine Then
    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "FNODE"
      .AliasName = "FNODE"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "TNODE"
      .AliasName = "TNODE"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "LPOLY"
      .AliasName = "LPOLY"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "RPOLY"
      .AliasName = "RPOLY"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "LENGTH"
      .AliasName = "LENGTH"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1
  End If
 
  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "SYS_ID"
    .AliasName = "SYS_ID"
    .Precision = 2
    .Scale = 0
    .Type = esriFieldTypeSingle
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = sFeldname
    .AliasName = sFeldname
    .Precision = 2
    .Scale = 0
    .Type = esriFieldTypeSingle
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "BLATT_ID"
    .AliasName = "BLATT_ID"
    .Precision = 6
    .Scale = 6
    .Type = esriFieldTypeInteger
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  If Not cGeomTyp = esriGeometryPolygon Then
    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "SCHNITT_ID"
      .AliasName = "SCHNITT_ID"
      .Precision = 6
      .Scale = 6
      .Type = esriFieldTypeInteger
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1
  End If
     
  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "BETRIEB"
    .AliasName = "Betrieb"
    .Length = 254
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "BLATTNAME"
    .AliasName = "Blattname"
    .Length = 65
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "RASTERBILD"
    .AliasName = "Rasterbild"
    .Length = 25
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

   Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "CD_NAME"
    .AliasName = "CD_Name"
    .Length = 20
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

   Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "HOTLINK"
    .AliasName = "Hotlink"
    .Length = 80
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

   Set pFieldEdit = New Field
    With pFieldEdit
    .Name = "BEMERKUNG"
    .AliasName = "Bemerkung"
    .Length = 60
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
 
  Set pCLSID = New UID
  pCLSID.Value = "esriGeodatabase.Feature"
 
  Set pOutShapeFCls = pFWkSp.CreateFeatureClass(sOutShapeName, pFields, pCLSID, Nothing, esriFTSimple, "Shape", "")  ' code fails here
  If pOutShapeFCls Is Nothing Then Exit Sub
  Set pF = pOutShapeFCls.CreateFeature
  If pF Is Nothing Then Exit Sub
  lFld = pF.Fields.FindField("BLATT_ID")
  If lFld > -1 Then pF.Value(lFld) = lBID
  pF.Store

End Sub
0 Kudos
4 Replies
sapnas
by
Frequent Contributor
Are you adding all 17 fields to pFieldsEdit?
0 Kudos
AxelThomas
Regular Contributor
Yes - might this be an issue? It works fine with the 13 or 14 fields for point and polygon shape files.

Axel
0 Kudos
NeilClemmons
Honored Contributor
The geometry type for a line feature class is esriGeometryPolyline.  You're using the correct geometry type when creating the geometry definition but your tests on whether to create the fields or not are using esriGeometryLine.  This is causing the code to create all of the fields.  Also, a better way to add new fields is to create the field object then call IFieldsEdit::AddField.  You don't set the FieldCount property when you do this because AddField will increment it for you automatically.
0 Kudos
AxelThomas
Regular Contributor
Neil,
thanks - setting esriGeometryPolyline consistently througout the code did the job.

Regards,
Axel
0 Kudos