The GIS Professional Group

フィーチャクラスの作成

2023/6/26 (月)

Option Explicit

Public Sub CreateFeatureClass()
   
    'フィールドの作成
    Dim pFieldEdit(2) As IFieldEdit
    
    'ObjectID
    Set pFieldEdit(0) = New Field
    pFieldEdit(0).Type = esriFieldType.esriFieldTypeOID
    pFieldEdit(0).Name = "OBJECTID" '既定値は "OBJECTID"
    
    'SpatialReference
    Dim pSpatialReferenceFactory As ISpatialReferenceFactory
    Set pSpatialReferenceFactory = New SpatialReferenceEnvironment
     
    Dim pSpatialReference As ISpatialReference
    Set pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_WGS1984)
 
    'XY座標精度の設定(9.2以降は設定必須)
    Dim pSpatialReferenceResolution As ISpatialReferenceResolution
    Set pSpatialReferenceResolution = pSpatialReference
    pSpatialReferenceResolution.SetDefaultXYResolution
     
    'XY許容値の設定(9.2以降は設定必須)
    Dim pSpatialReferenceTolerance As ISpatialReferenceTolerance
    Set pSpatialReferenceTolerance = pSpatialReference
    pSpatialReferenceTolerance.SetDefaultXYTolerance
    
    
    'GeometryDef
    Dim pGeometryDefEdit As IGeometryDefEdit
    Set pGeometryDefEdit = New GeometryDef
    pGeometryDefEdit.GeometryType = esriGeometryType.esriGeometryPoint
    pGeometryDefEdit.GridCount = 1
    pGeometryDefEdit.GridSize(0) = 0
    Set pGeometryDefEdit.SpatialReference = pSpatialReference
    
    'Geometry
    Dim strShapeField As String
    strShapeField = "SHAPE"   '既定値は "SHAPE"
    
    Set pFieldEdit(1) = New Field
    pFieldEdit(1).Name = strShapeField
    pFieldEdit(1).Type = esriFieldType.esriFieldTypeGeometry
    Set pFieldEdit(1).GeometryDef = pGeometryDefEdit
    
        
    'その他のフィールド
    Set pFieldEdit(2) = New Field
    pFieldEdit(2).Type = esriFieldType.esriFieldTypeString
    pFieldEdit(2).Name = "TEXT01"
    
    'Fieldsの作成
    Dim pFieldsEdit As IFieldsEdit
    Set pFieldsEdit = New Fields
    pFieldsEdit.AddField pFieldEdit(0)  '既定の順序は、OBJECTID, SHAPE, ...
    pFieldsEdit.AddField pFieldEdit(1)
    pFieldsEdit.AddField pFieldEdit(2)
    
    'ファイル ジオデータベースの作成
    Dim pWorkspaceFacttory As IWorkspaceFactory
    Set pWorkspaceFacttory = New FileGDBWorkspaceFactory
    
    Dim pName As IName
    Set pName = pWorkspaceFacttory.Create("C:\Users\ej2061\Documents\ArcGIS\", "FGDB01.gdb", Nothing, Application.hWnd)
    
    Dim pFeatureWorkspace As IFeatureWorkspace
    Set pFeatureWorkspace = pName.Open()
    
      
    'フィーチャクラスの作成
    Dim pFeatureClass As IFeatureClass
    Set pFeatureClass = pFeatureWorkspace.CreateFeatureClass("FeatureClass01", pFieldsEdit, Nothing, Nothing, esriFTSimple, strShapeField, "")  'フィーチャクラス名
    Dim pClassSchemaEdit As IClassSchemaEdit2
    Set pClassSchemaEdit = pFeatureClass
    pClassSchemaEdit.AlterAliasName "AliasName01"   'エイリアス名

    '現在のドキュメントを取得
    Dim pMxDocument As IMxDocument
    Set pMxDocument = ThisDocument
      
    'アクティブなデータフレームの取得
    Dim pMap As IMap
    Set pMap = pMxDocument.FocusMap
    
    'フィーチャ レイヤーの作成
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = New FeatureLayer
    pFeatureLayer.Name = pFeatureClass.AliasName
    Set pFeatureLayer.FeatureClass = pFeatureClass
      
    pMap.AddLayer pFeatureLayer
      
    
    MsgBox "Done"
    
End Sub

  • B!