ArcGIS Engine创建IFeatureDataset(数据集),IFeatureClass(图层),ITable(非空间表)
这里包含创建gdb、数据集、创建图层、非空间表,修改空间数据容差
//创建数据集,里面包含创建gdb,数据集,创建图层、非空间表。修改空间数据容差
public static IWorkspace CreateFeatureDataset(string gdbPath, string gdbName, string strModelPath, ISpatialReferenceResolution spatialReferenceResolution2)
{
IWorkspace pWs = null;
try
{
FileGDBWorkspaceFactoryClass fac = new FileGDBWorkspaceFactoryClass();
fac.Create(gdbPath, gdbName, null, 0);//创建gdb
pWs = (IWorkspace)fac.OpenFromFile(gdbPath+ "\\" + gdbName, 0);//打开新创建的gdb
//修改空间数据容差为0.0001
ISpatialReferenceTolerance spatialReferenceTolerance2 = spatialReferenceResolution2 as ISpatialReferenceTolerance;
spatialReferenceTolerance2.XYTolerance = 0.0001;
spatialReferenceTolerance2.ZTolerance = 0.0001;
spatialReferenceTolerance2.MTolerance = 0.0001;
IFeatureWorkspace pFeatureWorkspace = pWs as IFeatureWorkspace;
string dsName = "dataset";//数据集名称
//创建数据集
pFeatureWorkspace.CreateFeatureDataset(dsName,spatialReferenceResolution2 as ISpatialReference);
//打开刚创建的数据集
IFeatureDataset feaDataset = pFeatureWorkspace.OpenFeatureDataset(dsName);
if(feaDataset!=null)
{
CreateFeatureClass(feaDataset, spatialReferenceResolution2 as ISpatialReference);
System.Runtime.InteropServices.Marshal.ReleaseComObject(feaDataset);//释放
}
//创建table
CreateTable( pWs,spatialReferenceResolution2 as ISpatialReference );
System.Runtime.InteropServices.Marshal.ReleaseComObject(pWs);
}
catch (Exception Err)
{
}
}
void CreateTable( IWorkspace pWs , ISpatialReference spatialReference )
{
IFields pFields = new FieldsClass();
IFieldsEdit tFieldsEdit = (IFieldsEdit)pFields;
IField objfield = new FieldClass();
IFieldEdit objfieldEdit = objfield as IFieldEdit;
objfieldEdit.Name_2 = "OBJECTID";
objfieldEdit.IsNullable_2 = false;
objfieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
tFieldsEdit.AddField(objfield);
string tabName="tab";
string tabAname="数据表";
//可以再添加其他字段
ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
uid.Value = "esriGeoDatabase.Object";
ITable pTable = pFeatureWorkspace.CreateTable(tabName, pFields, uid, null, "");//uid必须有,否则创建会报No spatial reference exists错误
//为table添加别名
ISchemaLock schemaLock = pTable as ISchemaLock;
try
{
schemaLock?.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
IClassSchemaEdit classSchemaEdit = pTable as IClassSchemaEdit;
classSchemaEdit?.AlterAliasName(tabAname);
}
catch (Exception Err)
{
}
finally
{
schemaLock?.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(pTable); //释放
}
//创建featureclass
void CreateFeatureClass(object pObject , ISpatialReference spatialReference )
{
#region
var layerAttr = layerNodeEle.Attributes;
string lyrName = "testLyr";
string lyrAName = "测试图层";
string type ="Polygon";
var fieldsNode = layerNodeEle.ChildNodes;
IFields pFields = GetFieldsByXml(fieldsNode);
//添加字段
IFields pFields = new FieldsClass();
IFieldsEdit tFieldsEdit = (IFieldsEdit)pFields;
IField objfield = new FieldClass();
IFieldEdit objfieldEdit = objfield as IFieldEdit;
objfieldEdit.Name_2 = "OBJECTID";
objfieldEdit.IsNullable_2 = false;
objfieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
tFieldsEdit.AddField(objfield);
IField pfield = new FieldClass();
IFieldEdit fieldEdit = pfield as IFieldEdit;
fieldEdit.Name_2 = "field1";
fieldEdit.AliasName_2 = "字段1";
fieldEdit.Length_2 = 10;
fieldEdit.DefaultValue_2 = "";
fieldEdit.IsNullable_2 = isNull;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
tFieldsEdit.AddField(pfield );
//确定图层几何体类型
esriGeometryType geoType = esriGeometryType.esriGeometryPolyline;
if (type == "Polygon")
{
geoType = esriGeometryType.esriGeometryPolygon;
}
else if (type == "Polyline")
{
geoType = esriGeometryType.esriGeometryPolyline;
}
else if (type == "Point")
{
geoType = esriGeometryType.esriGeometryPoint;
}
esriFeatureType pFeatureType= esriFeatureType.esriFTSimple;
UID pUidClsId=new UIDClass();
UID pUidClsExt=null;
//pUidClsID字段赋值 不能为空
switch (pFeatureType)
{
case (esriFeatureType.esriFTSimple):
if (pGeometryType == esriGeometryType.esriGeometryLine)
pGeometryType = esriGeometryType.esriGeometryPolyline;
pUidClsId.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}";
break;
case (esriFeatureType.esriFTSimpleJunction):
pGeometryType = esriGeometryType.esriGeometryPoint;
pUidClsId.Value = "{CEE8D6B8-55FE-11D1-AE55-0000F80372B4}";
break;
case (esriFeatureType.esriFTComplexJunction):
pUidClsId.Value = "{DF9D71F4-DA32-11D1-AEBA-0000F80372B4}";
break;
case (esriFeatureType.esriFTSimpleEdge):
pGeometryType = esriGeometryType.esriGeometryPolyline;
pUidClsId.Value = "{E7031C90-55FE-11D1-AE55-0000F80372B4}";
break;
case (esriFeatureType.esriFTComplexEdge):
pGeometryType = esriGeometryType.esriGeometryPolyline;
pUidClsId.Value = "{A30E8A2A-C50B-11D1-AEA9-0000F80372B4}";
break;
case (esriFeatureType.esriFTAnnotation):
pGeometryType = esriGeometryType.esriGeometryPolygon;
pUidClsId.Value = "{E3676993-C682-11D2-8A2A-006097AFF44E}";
break;
case (esriFeatureType.esriFTDimension):
pGeometryType = esriGeometryType.esriGeometryPolygon;
pUidClsId.Value = "{496764FC-E0C9-11D3-80CE-00C04F601565}";
break;
}
//pUidClsExt字段为空时
#region pUidClsExt字段为空时
switch (pFeatureType)
{
case esriFeatureType.esriFTAnnotation:
pUidClsExt = new UIDClass();
pUidClsExt.Value = "{24429589-D711-11D2-9F41-00C04F6BC6A5}";
break;
case esriFeatureType.esriFTDimension:
pUidClsExt = new UIDClass();
pUidClsExt.Value = "{48F935E2-DA66-11D3-80CE-00C04F601565}";
break;
}
#endregion
//创建几何对象字段定义
IGeometryDef tGeometryDef = new GeometryDefClass();
IGeometryDefEdit tGeometryDefEdit = tGeometryDef as IGeometryDefEdit;
//指定几何对象字段属性值
tGeometryDefEdit.GeometryType_2 = pGeometryType;
tGeometryDefEdit.GridCount_2 = 1;
tGeometryDefEdit.set_GridSize(0, 1000);
if (pObject is IWorkspace || pObject is IFeatureWorkspace)
{
tGeometryDefEdit.SpatialReference_2 = pSpatialReference;
}
//字段集合为空时
#region 字段集合为空时
IFieldsEdit tFieldsEdit = (IFieldsEdit)pFields;
//创建几何字段
IField fieldShape = new FieldClass();
IFieldEdit fieldEditShape = fieldShape as IFieldEdit;
fieldEditShape.Name_2 = "SHAPE";
fieldEditShape.AliasName_2 = "SHAPE";
fieldEditShape.Type_2 = esriFieldType.esriFieldTypeGeometry;
fieldEditShape.GeometryDef_2 = tGeometryDef;
tFieldsEdit.AddField(fieldShape);
#endregion
//几何对象字段名称
string strShapeFieldName = "";
for (int i = 0; i < pFields.FieldCount; i++)
{
if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
{
strShapeFieldName = pFields.get_Field(i).Name;
break;
}
}
//创建图层
IFeatureClass fls =null;
if (pObject is IWorkspace)
{
//创建独立的FeatureClass
IWorkspace tWorkspace = pObject as IWorkspace;
IFeatureWorkspace tFeatureWorkspace = tWorkspace as IFeatureWorkspace;
fls = tFeatureWorkspace.CreateFeatureClass(lyrName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, "");
}
else if (pObject is IFeatureDataset)
{
//在要素集中创建FeatureClass
IFeatureDataset tFeatureDataset = (IFeatureDataset)pObject;
fls = tFeatureDataset.CreateFeatureClass(lyrName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, "");
}
//修改图层别名
ISchemaLock schemaLock = fls as ISchemaLock;
try
{
schemaLock?.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
IClassSchemaEdit classSchemaEdit = fls as IClassSchemaEdit;
classSchemaEdit?.AlterAliasName(lyrAName);
}
finally
{
schemaLock?.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(fls);
#endregion
}