在ArcGIS Resouce Center中,ESRI介绍了两种创建Feature的方法(可以在本地的Geodatabase和sde的featureclass)
一、
IFeatureClass.CreateFeature,在这种方法最后需要加上IFeature.Store去提交创建的要素,本人认为这种方法相比下面一种方法更好些,因为Store的方法能够直接提交修改并在数据库中看到,不会因为其他复杂的操作影响数据入库。下面是在SDE库中创建IFeature的代码给大家参考一下:
IAoInitialize m_AoInitializa = new AoInitializeClass(); esriLicenseStatus pEsriLicenseStatus=m_AoInitializa.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB); IPropertySet propSet = new PropertySetClass(); propSet.SetProperty("SERVER", "192.168.1.143"); propSet.SetProperty("INSTANCE", "5151"); propSet.SetProperty("USER", "myTestUser"); propSet.SetProperty("PASSWORD", "123456"); propSet.SetProperty("VERSION", "SDE.DEFAULT"); IWorkspaceFactory pwf = new SdeWorkspaceFactoryClass(); IFeatureWorkspace pFeatureWorkspace= (IFeatureWorkspace)(pwf.Open(propSet, 0)) ; IFeatureClassfeaClass=pFeatureWorkspace.OpenFeatureClass("要打开的Featureclass名字"); IFeature feature = feaClass.CreateFeature(); feature.Shape=IGeometry;//(这里的IGeometry可以是IPolygon,IPolyline,IPoint) int fieldindex = feature.Fields.FindField("字段名"); feature.set_Value(fieldindex, "字段值"); feature.Store();
二、
IFeatureClass.CreateFeatureBuffer,这个方法采用插入游标(Insert Cursors)的方法,在创建简单数据的时候效率会比第一种方法更快些,但是在ESRI的官网上提到使用IFeatureCursor.InsertFeature方法时,复杂的操作和各种事件的触发不能够保证。根据自己实际操作的心得,有时候会出现数据创建延时,明明代码已经通过了,但数据库中的数据要过很久才能显示出来,甚至有时候都显示不出来。这个客户肯定接受不了这种没有保证的数据创建。还有一点,在使用SDE库时,这种方法只适用于没有注册版本的dateset或featureclass(这个仅仅是在我的程序中出现过这种问题,仅供参考),下面为代码(打开SDE的部分就不重复了)
IFeatureCursor feaCursor = feaClass.Insert(true); IFeatureBuffer feaBuffer = feaClass.CreateFeatureBuffer(); feaBuffer.Shape = IGeometry;//(这里的IGeometry可以是IPolygon,IPolyline,IPoint) int fieldindex = feaBuffer.Fields.FindField("字段名"); if (fieldindex >= 0) { feaBuffer.set_Value(fieldindex, "字段值" ); } feaCursor.InsertFeature(feaBuffer);
经过一位网友的帮助,以上的文章有不足的地方,就是IFeatureClass.CreateFeatureBuffer在featureclass中插入一条记录,在本文中feaCursor.InsertFeature(feaBuffer)之后加上feaCrusor.Fulsh()将创建的记录输入到数据库中,而且在面对大数据量插入的时候用IFeatureClass.CreateFeatureBuffer()效率比IFeatureClass.CreateFeature高,详细可以参见以下网址:LINK,这里面有详细介绍.
三、
使用IFeatureClassWriter接口
public void DrawPoint(ILayer pLayer,double X,double Y) { IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer; IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; IFeatureClassWrite fr = pFeatureClass as IFeatureClassWrite; IFeature pFeature; IPoint pPoint; //pWorkspaceEdit.StartEditing(true); pWorkspaceEdit.StartEditOperation(); pFeature = pFeatureClass.CreateFeature(); pPoint = new PointClass(); pPoint.PutCoords(X, Y); pFeature.Shape = pPoint; fr.WriteFeature(pFeature); pWorkspaceEdit.StopEditOperation(); }