矢量数据的属性值的添加、编辑、删除

/* 以下代码实现的是矢量数据的更新,删除*/

/* 注意可以用store,但是效率比较低,每次都写一下文件,不如一次性flush一下*/

/*首先定义游标,然后.NextFeature() 依次执行,但是注意select产生的游标不能应用于update 和 Insert*/

/*代码比较乱,包含添加、删除、更新三个方面的操作,没有注释掉的是更新操作,查询之前的文章介绍过*/

#region 修改字段的值

IDataset dataset = pFlayerClass as IDataset;            

IWorkspace workspace = dataset.Workspace;            

IWorkspaceEdit wse = workspace as IWorkspaceEdit;            

wse.StartEditing(false);            

wse.StartEditOperation();            

int proField = pFlayerClass.FindField(“SPEEDCLASS”);

// null 就是全部都查询不用再新建IQUERY… 了                        

int cnt = pFlayerClass.FeatureCount(null);

// 要添加数据必须使用 Insert产生的Cursor 不要使用Select生成的Cursor,如下所示            

//IFeatureCursor featureCursor = pFlayerClass.Insert(true);

// 要编辑数据就是用Update生成的Cursor              

IFeatureCursor featureCursor = pFlayerClass.Update(null,true);  

// 如果要添加数据,就使用Buffer 最后flush一下          

//IFeatureBuffer featureBuffer = pFlayerClass.CreateFeatureBuffer();

// 以下这两句 写到循环外边            

int fieldId = pFlayerClass.FindField(“ID”);            

IFeature f = featureCursor.NextFeature();            

int speedId = pFlayerClass.FindField(“SPEEDCLASS”);            

for (int i = 0; i < cnt; i++)            

{

         string roadID = pFlayerClass.GetFeature(i).get_Value(fieldId).ToString();                

         int cl = GetSpeedClass(roadID);                

         //int cl = 2;                

         f.set_Value(speedId,cl);

   // 以下的代码是写入缓存

         //featureBuffer.Shape = pFeature.Shape;  

   //featureBuffer.set_Value(fieldId, roadID);
        //featureBuffer.set_Value(speedId, cl);               

        //IFeature pFeature = pFlayerClass.GetFeature(i);

    // 插入一个新的Feature

    // featureCursor.InsertFeature(featureBuffer);               

        featureCursor.UpdateFeature(f);

   // 删除一个矢量数据   

    f.Delete();             

        f = featureCursor.NextFeature();            

}            

featureCursor.Flush();

wse.StopEditOperation();          

wse.StopEditing(true);                        

#endregion

转载自:https://blog.csdn.net/weixin_33735077/article/details/85515748

You may also like...