Arcengine创建内存工作空间,要素类(InMemoryWorkspaceFactory)
1.创建内存工作空间
public static IWorkspace CreateInMemoryWorkspace()
{
// Create an in-memory workspace factory.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.InMemoryWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
Activator.CreateInstance(factoryType);
// Create an in-memory workspace.
IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace",
null, 0);
// Cast for IName and open a reference to the in-memory workspace through the name object.
IName name = (IName)workspaceName;
IWorkspace workspace = (IWorkspace)name.Open();
return workspace;
}
2.创建要素类
/// <summary>
/// 在指定的数据库中创建与源要素类相同的要素类
/// </summary>
/// <param name="pWorkspace"></param>
/// <param name="pSrcFC"></param>
/// <param name="sTargetName"></param>
/// <param name="pTargetSrf"></param>
/// <param name="sOutputFields"></param>
/// <returns></returns>
public static IFeatureClass CreateFeatureClass(IWorkspace pWorkspace,
IFeatureClass pSrcFC,
string sTargetName = null,
ISpatialReference pTargetSrf = null,
string sOutputFields = null)
{
if (string.IsNullOrEmpty(sTargetName))
{
sTargetName = (pSrcFC as IDataset).Name;
}
sTargetName = GetDatasetName(sTargetName);
DeleteDataset(pWorkspace, sTargetName);
//验证字段
IFields pFields;
string sShapeFieldName;
string sError;
CheckFields(pSrcFC, pWorkspace, out pFields, out sShapeFieldName, out sError);
//根据输出字段限制,处理字段
if (!string.IsNullOrWhiteSpace(sOutputFields))
{
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
string[] pOutputFields = sOutputFields.Split(',');
for (int i = pFields.FieldCount - 1; i >= 0; i--)
{
IField pField = pFields.get_Field(i);
if (pField.Type == esriFieldType.esriFieldTypeGeometry ||
pField.Type == esriFieldType.esriFieldTypeOID)
{
continue;
}
bool bFind = false;
foreach (string sOutputField in pOutputFields)
{
if (sOutputField == pField.Name)
{
bFind = true;
break;
}
}
if (!bFind)
{
pFieldsEdit.DeleteField(pField);
}
}
}
//修改几何字段中的空间参考信息
if (pTargetSrf != null)
{
int iIndex = pFields.FindField(sShapeFieldName);
//几何定义
IGeometryDef pGeometryDef = pFields.get_Field(iIndex).GeometryDef;
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.SpatialReference_2 = pTargetSrf;
}
IFeatureClass pFeatureClass = CreateFeatureClass(pWorkspace, pSrcFC,
pFields, sShapeFieldName, sTargetName, null, pTargetSrf);
return pFeatureClass;
}
/// <summary>
/// 创建要素类,考虑要素类存在要素数据集中
/// </summary>
/// <param name="pWorkspace"></param>
/// <param name="pSrcFC"></param>
/// <param name="pFields"></param>
/// <param name="sShapeFieldName"></param>
/// <param name="sTargetName"></param>
/// <param name="sAlias"></param>
/// <param name="pTargetSrf"></param>
/// <returns></returns>
public static IFeatureClass CreateFeatureClass(IWorkspace pWorkspace,
IFeatureClass pSrcFC,
IFields pFields,
string sShapeFieldName,
string sTargetName = null,
string sAlias = null,
ISpatialReference pTargetSrf = null)
{
UID CLSID = new UIDClass();
CLSID.Value = "esriGeoDatabase.Feature";
if (string.IsNullOrEmpty(sTargetName))
{
sTargetName = GetDatasetName((pSrcFC as IDataset).Name);
}
IFeatureClass pFeatureClass = null;
//是否需要创建要素数据集
IFeatureDataset pTargetDataset = GetFeatureDataset(pWorkspace, pSrcFC, pTargetSrf);
try
{
if (pSrcFC.FeatureType == esriFeatureType.esriFTAnnotation)
{
//Anno Workspace
IFeatureWorkspaceAnno pFWSAnno = pWorkspace as IFeatureWorkspaceAnno;
//获得AnnoFeatureClass的显示参数units和referencescale
IAnnoClass pAnnoClass = pSrcFC.Extension as IAnnoClass;
IGraphicsLayerScale pGraphicsLayerScale = new GraphicsLayerScaleClass();
pGraphicsLayerScale.Units = pAnnoClass.ReferenceScaleUnits;
pGraphicsLayerScale.ReferenceScale = pAnnoClass.ReferenceScale;
IObjectClassDescription pOCDesc = new AnnotationFeatureClassDescription();
pFeatureClass = pFWSAnno.CreateAnnotationClass(sTargetName, pFields, pOCDesc.InstanceCLSID,
pOCDesc.ClassExtensionCLSID, pSrcFC.ShapeFieldName, "", pTargetDataset,
null, pAnnoClass.AnnoProperties, pGraphicsLayerScale,
pAnnoClass.SymbolCollection, false);
}
else
{
if (pTargetDataset != null)
{
pFeatureClass = pTargetDataset.CreateFeatureClass(sTargetName, pFields, CLSID, null, pSrcFC.FeatureType, sShapeFieldName, "");
}
else
{
pFeatureClass = (pWorkspace as IFeatureWorkspace).CreateFeatureClass(sTargetName, pFields, CLSID, null, pSrcFC.FeatureType, sShapeFieldName, "");
}
}
}
finally
{
if (pTargetDataset != null)
{
Marshal.ReleaseComObject(pTargetDataset);
}
}
//设置要素类别名
if (string.IsNullOrWhiteSpace(sAlias))
{
sAlias = pSrcFC.AliasName;
}
AlterDatasetAlias(pFeatureClass, sAlias);
return pFeatureClass;
}
转载自:https://blog.csdn.net/kone0611/article/details/73023108