创建内存FeatureClass的方式有很多种
最简单的一种就是根据模板要素图层的FeatureClass的IFields创建
这样创建创建的内存FeatureClass得需要模板要素图层得IFields属性 如果模板图层是polygon那么内存FeatueClass也是
比如:
public static IFeatureClass CreateMemoryFeatureClass(IFields pFields, string name = "Temp")
{
// 创建内存工作空间
IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass();
IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", name, null, 0);
IName pName = (IName)pWorkspaceName;
IWorkspace pWorkspace = (IWorkspace)pName.Open();
//创建内存要素类
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
return pFeatureClass;
}
上面的方法必须需要一个模板图层来创建不够灵活 下面用官方提供的例子创建内存FeatureClass
注意:下面暂时只能创建面要素内存图层 点和线图层需要修改geometry字段类型
public static IFeatureClass CreateMemoryFeatureClass(string name = "Temp")
{
// 创建内存工作空间
IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass();
IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", name, null, 0);
IName pName = (IName)pWorkspaceName;
IWorkspace pWorkspace = (IWorkspace)pName.Open();
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
//UID
UID CLSID = new UIDClass();
CLSID.Value = "esriGeoDatabase.Feature";
IFields pFields = null;
IObjectClassDescription objectClassDescription = new FeatureClassDescriptionClass();
//创建必须的字段
pFields = objectClassDescription.RequiredFields;
//对字段进行检查
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (IWorkspace)pWorkspace;
fieldChecker.Validate(pFields, out enumFieldError, out validatedFields);
IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(name, validatedFields, CLSID, null, esriFeatureType.esriFTSimple, "SHAPE", "");
return pFeatureClass;
}