主要是使用IField,IFieldEdit,IFields,IFieldsEdit,IGeometryDef,IGeometryDefEdit接口。
字段对应表中的一列,一个要素类必须有至少2个字段,而多个字段的集合就构成了字段集。在要素类中,有一个特殊的字段,描述了空间对象,我们称之为几何字段,其中GeometryDef是用来设计几何字段的。这个几何字段定义了要素类的类型,比如说我们要在Catalog创建一个点要素类,那么我们必须指定他的类型为Point,如下图:
而上面这6个接口,其实是三类,以Edit结尾的接口是可写的,也就是说对字段,字段集合,以及几何字段的编辑都是通过后者完成的。空间数据的一个重要属性就是参考系,参考系也是在GeometryDef中定义的。
注意 在.NET中,会遇到以“_2”结尾的属性,这些属性是可写的。
参考1
//定义一个几何字段,类型为点类型 ISpatialReference pSpatialReference = axMapControl1.ActiveView.FocusMap.SpatialReference; IGeometryDefEdit pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.SpatialReference_2 = pSpatialReference; //定义一个字段集合对象 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; //定义单个的字段 IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; pFieldsEdit.AddField(pField); pFieldEdit.GeometryDef_2 = pGeoDef; //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "STCD"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM10"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM20"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField);
//定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM40"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField);
IWorkspaceFactory pFtWsFct = new AccessWorkspaceFactory(); IFeatureWorkspace pWs = pFtWsFct.OpenFromFile(@"E:arcgisEngines.mdb", 0) as IFeatureWorkspace; IFeatureClass pFtClass = pWs.CreateFeatureClass("Test", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", null)
如何改变字段的别名
public void ChangeFieldAliasName(ITable pTable, string pOriFieldName, string pDesFieldName) { IClassSchemaEdit pClassSchemaEdit = (IClassSchemaEdit)pTable; //给对象加上锁 ISchemaLock pSchemaLock = (ISchemaLock)pTable; pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); if (pTable.FindField(pOriFieldName) != -1) { pClassSchemaEdit.AlterFieldAliasName(pOriFieldName, pDesFieldName); pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } else { return; } }
参考2
IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeometryDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Length_2 = 10; pFieldEdit.Name_2 = "CODE"; pFieldEdit.AliasName_2 = "CODE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); String MapPath = @"E:data esult"; String LayerName = "AddFeature"; IWorkspaceFactory pWsf = new ShapefileWorkspaceFactory(); IFeatureWorkspace pFws=pWsf.OpenFromFile(MapPath,0) as IFeatureWorkspace; IFeatureClass pFeatureClass=pFws.CreateFeatureClass(LayerName + ".shp", pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");
新建shapefile,自定义字段,路径自定义。
在文件里加入POLYGON,自定义字段值
IDataset pDataset = (IDataset)pFeatureClass; pFws = (IFeatureWorkspace)pDataset.Workspace; IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pFws; pWorkspaceEdit.StartEditing(true); pWorkspaceEdit.StartEditOperation(); IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer(); //Create insert Feature Cursor using buffering = true. IFeatureCursor pFeatCursor = pFeatureClass.Insert(true); object featureOID; //定义新加元素的值 pFeatureBuffer.set_Value(pFeatureBuffer.Fields.FindField("CODE"),"CODE"); //生成Polygon********************************************************** IPolygon poly = new PolygonClass(); IPoint pPoint = new PointClass(); object o = Type.Missing; IPointCollection pPc = new MultipointClass(); pPc = (IPointCollection)poly; for (int i = 0; i <= 0; i++ ) { pPoint.PutCoords(200, 400); pPc.AddPoint(pPoint, ref o, ref o); pPoint.PutCoords(400, 400); pPc.AddPoint(pPoint, ref o, ref o); pPoint.PutCoords(400, 200); pPc.AddPoint(pPoint, ref o, ref o); pPoint.PutCoords(200, 200); pPc.AddPoint(pPoint, ref o, ref o); } //生成完毕 ************************************************************ pFeatureBuffer.Shape = poly; featureOID = pFeatCursor.InsertFeature(pFeatureBuffer); //Flush the feature cursor to the database //Calling flush allows you to handle any errors at a known time rather then on the cursor destruction. pFeatCursor.Flush(); pWorkspaceEdit.StopEditOperation(); pWorkspaceEdit.StopEditing(true); //释放Cursor System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatCursor); axMapControl1.AddShapeFile(MapPath,LayerName+".shp");
参考文章