ArcObjects メモリレイヤを追加

ArcObjectsはレイヤの扱いが直観的ではなく忘れやすい。

以下ではポリゴン、EPSG=2451のメモリレイヤを作成してMapControlに追加

try
{

    IFields fields = new Fields();
    IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
                   
    IField field = new Field();
    IFieldEdit fieldEdit = (IFieldEdit)field;

    fieldEdit.Name_2 = "OID";
    fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
    fieldsEdit.AddField(fieldEdit);

    field = new Field();
    fieldEdit = (IFieldEdit)field;
    fieldEdit.Name_2 = "NAME";
    fieldEdit.Length_2 = 10;
    fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
    fieldsEdit.AddField(fieldEdit);

    field = new Field();
    fieldEdit = (IFieldEdit)field;
    fieldEdit.Name_2 = "GEOM";
    fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;


    IGeometryDef geometryDef = new GeometryDef();
    IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
    geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;


    ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
    ISpatialReference spatialReference =
    spatialReferenceFactory.CreateProjectedCoordinateSystem((int)2451);
    ISpatialReferenceResolution spatialReferenceResolution = (ISpatialReferenceResolution)spatialReference;
    spatialReferenceResolution.ConstructFromHorizon();
    ISpatialReferenceTolerance spatialReferenceTolerance = (ISpatialReferenceTolerance)spatialReference;
    spatialReferenceTolerance.SetDefaultXYTolerance();
    geometryDefEdit.SpatialReference_2 = (ISpatialReference)spatialReference;


    fieldEdit.GeometryDef_2 = geometryDefEdit;

    fieldsEdit.AddField(fieldEdit);

    Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.InMemoryWorkspaceFactory");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
    IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0);
    IName name = (IName)workspaceName;
    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)name.Open();

    IFeatureClass featureClass = 
        featureWorkspace.CreateFeatureClass(
        "NewPolygon", 
        fields,
        null,
        null, 
        esriFeatureType.esriFTSimple, 
        "GEOM", "");

    IFeatureLayer featureLayer = new FeatureLayer();
    featureLayer.FeatureClass = featureClass;
    featureLayer.Name = "mempoly";

    axMapControl1.AddLayer(featureLayer);
}
catch (Exception e)
{
    System.Diagnostics.Debug.WriteLine(e.Message);
}

参考にしたサイト

InMemoryWorkspaceFactory CoClass

Creating a featureclass in memory and adding to the map error

IFeatureWorkspace.CreateFeatureClass Example

IFeatureClass CreateFeature Example

IFeatureClass Example


How to create geodatabases