在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.145");
propSet.SetProperty("INSTANCE", "5151");
propSet.SetProperty("USER", "demo");
propSet.SetProperty("PASSWORD", "111111");
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, "字段值");
二、
IFeatureClass.CreateFeatureBuffer,这个方法采用插入游标(Insert Cursors)的方法,在面对大数据量插入的时候用IFeatureClass.CreateFeatureBuffer()效率比IFeatureClass.CreateFeature高
public static void LoadDataToGDB(List<IFeature> FeaList, IFeatureWorkspace featureWorkspace, List<string> FieldsList, string shapeName, ProgressBarX progressBarX1)
{
#region 加载数据到gdb
IFeatureClass sdeFeatureClass = featureWorkspace.OpenFeatureClass(shapeName);
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = null;
IFeatureCursor pFeaCursor = sdeFeatureClass.Insert(true);
IFeature pFeature = null;
int iIndex = 0;
int iIndexGDB = 0;
long n = 0;
for (int k = 0; k < FeaList.Count; k++)
{
pFeature = FeaList[k];
IFields pField = pFeature.Fields;
int IndexChage = pField.FindField("CHANGETYPE");
string CHANGETYPE = pFeature.get_Value(IndexChage).ToString();
bool IsNewAdd = false;
if (CHANGETYPE == "2")
{
IsNewAdd = true;
}
try
{
IFeatureBuffer pFeaBuffer = sdeFeatureClass.CreateFeatureBuffer();
IFields pFieldGDB = pFeaBuffer.Fields;
pFeaBuffer.Shape = pFeature.ShapeCopy;
//添加字段值
for (int j = 0; j < FieldsList.Count; j++)
{
string FiledName = FieldsList[j];
iIndex = pField.FindField(FiledName);
string FiledValue = "";
if (iIndex == -1)
{
FiledValue = GetDefaultValue(FiledName, IsNewAdd);
}
else
{
FiledValue = pFeature.get_Value(iIndex).ToString();
}
iIndexGDB = pFieldGDB.FindField(FiledName);
pFeaBuffer.set_Value(iIndexGDB, FiledValue);
}
pFeaCursor.InsertFeature(pFeaBuffer);
}
catch (System.Exception ex)
{
// MessageBox.Show("单要素写入异常!" + ex.Message, "提示");
// return;
}
finally //达到一定数量后释放资源
{
n++;
if (n % 100 == 0)
{
pFeaCursor.Flush();
}
}
pFeaCursor.Flush();
progressBarX1.Value = k;
}
#endregion
}
三、
使用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();
}