开发者社区> 橘子红了呐> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

ArcEngine查询、添加、删除要素的方法

简介:
+关注继续查看

 

1、查找数据

1)、利用FeaturCursor进行空间查询

复制代码
//空间查询
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = envelope;//指定几何体
String shpFld = featureClass.ShapeFieldName;
spatialFilter.GeometryField = shpFld;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;//相交
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter = (IQueryFilter)spatialFilter;
IFeatureCursor searchCursor = featureClass.Search(queryFilter, true);
IFeature feature = searchCursor.NextFeature();

int n = 0;
while (feature != null){
    n++;
    feature = baseCursor.NextFeature();
}
复制代码

 

2)、利用FeatureCursor在选择集中进行查询

IFeatureSelection pFeatureSelectio = pFeatureLayer as IFeatureSelection; 
ICursor ppCursor;
pFeatureSelectio.SelectionSet.Search(null, false, out ppCursor);
IFeatureCursor pFeatureCursor = ppCursor as IFeatureCursor; 
IFeature pFeature = pFeatureCursor.NextFeature();

3)、利用FeatureCursor进行属性查询

IQueryFilter pQueryFilter = new QueryFilterClass();                           
pQueryFilter.WhereClause = "PROJECTCODE = '" + this.ProjectNumTextBox.Text.Trim() + "'";
IFeatureCursor pFeatureCursor = LineFeaClass.Search(pQueryFilter, false);                           
IFeature pFeatureIfExit = pFeatureCursor.NextFeature();

用Environment.TickCount进行代码执行时间测试,结果发现4)读取整个表的时间为4984ms,而 3)读取同一个属性给的时间仅为32 ms,3)的执行效率是法一的156倍!!!

完整测试代码如下:

 View Code

 

4)属性表的方法读取属性值

ITable pTable = pLayer.FeatureClass as ITable;  
clsFldValue = pTable.GetRow(i).get_Value(clsFldIndex);  

 

 

2、 插入数据

 

ArcGIS Engine中,主要有两个方法用于要素的添加:

 

 

批量插入feature,如果用feature.store()方法,在图层中一个个地插入要素,较之同时使用insert cursor与feature buffer方法,会慢很多。

 

因为后者触发的事件和复杂行为比较少(比如说没有引发因拓扑关系产生的行为)。

 

 

1)、利用Insert Cursors和featureBuffer

 

复制代码
IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
IFeatureCursor featureCursor = featureClass.Insert(true);
object featureOID;         
featureBuffer.set_Value(featureBuffer.Fields.FindField("InstBy"), "B Pierce");
for (int ic = 0; ic < 99; ic++)
{ 
    featureBuffer.Shape = geometry;    
    featureOID = featureCursor.InsertFeature(featureBuffer);
}
featureBuffer.set_Value(featureBuffer.Fields.FindField("InstBy"), "K Johnston");
for (int ic = 0; ic < 99; ic++)
{
     featureBuffer.Shape = geometry
     featureOID = featureCursor.InsertFeature(featureBuffer);
}
featureCursor.Flush();
复制代码

 

 

2)、直接进行数据插入(Feature.Store方法)

复制代码
IFeature newLineFeature = LineFeaClass.CreateFeature();                           

newLineFeature.Shape = pFeature.Shape;
int ii = newLineFeature.Fields.FindField(AreaZonalName); 

if (ii > 0)                           
{                               
    newLineFeature.set_Value(ii, this.AnalysAreaComboBox.Text.Trim());
}

newLineFeature.Store();
复制代码

 

 

注:最好将编辑操作对象中,如下:

IDataset dataset = (IDataset)featureClass;

  IWorkspace workspace = dataset.Workspace;

  // Cast the workspace to the IWorkspaceEdit interface.

  IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;

  // Start an edit session and edit operation.

  workspaceEdit.StartEditing(true);

  workspaceEdit.StartEditOperation();

//在这里编写添加要素的代码。。。。。。。。。。。

// Stop editing.

  workspaceEdit.StopEditOperation();

  workspaceEdit.StopEditing(true);

 

3、数据删除 delete 

删除feature,一个个删除就用IFeature.Delete方法即可,此处不再赘述,只写一种批量删除的方法,用于ITable是针对数据库进行操作的,所以速度很快。

The best approach to take when deleting features depends on two factors, how many features are being deleted and whether the data source is a local geodatabase or an ArcSDE geodatabase.

In the simplest case, a single feature that has already been retrieved can be deleted by callingIFeature.Delete. If bulk features are being deleted and the geodatabase is an ArcSDE geodatabase, the most efficient approach requires the use of a search cursor and the IFeature.Delete method.

On the other hand, if the geodatabase is a local geodatabase (a file or personal geodatabase), the most efficient method for bulk deletion is theITable.DeleteSearchedRows method.

方法1  ITable方法

复制代码
///<summary>  
///删除某featurelayer中所有feature  
///</summary>  
///<param name="pLayer">操作的涂层</param>  
///<remarks>该方法可以给一个queryfilter,进行删除符合条件的features</remarks>  
private void DeleteAllFeatures(IFeatureLayer pLayer, <code></code>IQueryFilter queryFilter)  
{  
  ITable pTable = pLayer.FeatureClass as ITable;  
  pTable.DeleteSearchedRows(queryFilter);  
}  
复制代码

 

方法2 迭代featureClass方法

复制代码
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Parcels");
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "ZONING_S = 'R'";
IFeatureCursor updateCursor = featureClass.Update(queryFilter, false);
IFeature feature = updateCursor.NextFeature();
int m = 0;
while (feature != null)
{
    m++;
    updateCursor.DeleteFeature(feature);
    feature = updateCursor.NextFeature();
}
复制代码

 

4、 数据更新 update 

4.1 逐条更新记录

 这种方式中可有三种方法,如下:

(1)

复制代码
    for (int i = 0; i < pTable.RowCount(null); i++)  
    {  
        pRow = pTable.GetRow(i);  
        pRow.set_Value(2, i + 6);  
        pRow.Store();  
    }  
复制代码

 

(2)

复制代码
    IFeatureCursor FCursor = pLayer.FeatureClass.Search(new QueryFilterClass(), false);  
    IFeature feature = FCursor.NextFeature();  
      
    for (int i = 0; i < featureNum; i++)  
    {  
      
        feature.set_Value(2, i);  
        feature.Store();  
        feature = FCursor.NextFeature();  
    }  
复制代码

 

(3)

复制代码
    ICursor pCursor =pTable.Update(null, false);  
    pRow = pCursor.NextRow();  
    for (int i = 0; i < pTable.RowCount(null); i++)  
    {  
        pRow.set_Value(2, i + 6);  
        pCursor.UpdateRow(pRow);  
        pRow = pCursor.NextRow();  
    }  
复制代码

试验数据为320条记录,三种方法的运行时间为:法(1)为40297ms;法(2)34922ms为;法(3)为219ms.

可见运用IFeature和IRow的Store方法更新速度都很慢,用ICursor 的UpdateRow方法速度很快,分别是前两者效率的184倍、159倍!!

 

4.2 批量更新记录

复制代码
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Parcels");

// 属性查询
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "ZONING_S = 'U'";

//利用FeatureCursor进行数据更新
IFeatureCursor updateCursor = featureClass.Update(queryFilter, false);
int fieldindex = featureClass.FindField("ZONING_S");
IFeature feature = updateCursor.NextFeature();
int m = 0;

while (feature != null)
{
    m++;
    feature.set_Value(fieldindex, "X");
    updateCursor.UpdateFeature(feature);
    feature = updateCursor.NextFeature();
}
复制代码

 

 

 当将一批数据更新为某一相同的属性时,使用ITable.UpdateSearchedRows效率会很高。

复制代码
    // Find the position of the field that will be updated.  
    int typeFieldIndex = featureClass.FindField("TYPE");  
      
    // Create a query filter defining which fields will be updated  
    // (the subfields) and how to constrain which rows are updated  
    // (the where clause).  
    IQueryFilter queryFilter = new QueryFilterClass  
    {  
        SubFields = "TYPE", WhereClause = "LANE_COUNT = 4"  
    };  
      
    // Create a ComReleaser for buffer management.  
    using(ComReleaser comReleaser = new ComReleaser())  
    {  
        // Create a feature buffer containing the values to be updated.  
        IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();  
        featureBuffer.set_Value(typeFieldIndex, "Highway");  
        comReleaser.ManageLifetime(featureBuffer);  
      
        // Cast the class to ITable and perform the updates.  
        ITable table = (ITable)featureClass;  
        IRowBuffer rowBuffer = (IRowBuffer)featureBuffer;  
        table.UpdateSearchedRows(queryFilter, rowBuffer);  
    }  
复制代码

 

 

 

 

 参考文章

 ArcEngine查询、添加、删除要素的方法

ArcGIS Engine效率探究——要素的添加和删除、属性的读取和更新

 

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。



  本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5905407.html,如需转载请自行联系原作者





版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
基于C#的ArcEngine二次开发48:点是否落在实体上检查
基于C#的ArcEngine二次开发48:点是否落在实体上检查
0 0
基于C#的ArcEngine二次开发34:使用ConstructUnion方法进行多要素合并(中)
基于C#的ArcEngine二次开发34:使用ConstructUnion方法进行多要素合并(中)
0 0
基于C#的ArcEngine二次开发34:使用ConstructUnion方法进行多要素合并(上)
基于C#的ArcEngine二次开发34:使用ConstructUnion方法进行多要素合并
0 0
基于C#的ArcEngine二次开发34:使用ConstructUnion方法进行多要素合并(下)
基于C#的ArcEngine二次开发34:使用ConstructUnion方法进行多要素合并(下)
0 0
基于C#的ArcEngine二次开发36: 在地理数据库中创建要素类的接口及方法分析(上)
基于C#的ArcEngine二次开发36: 在地理数据库中创建要素类的接口及方法分析
0 0
基于C#的ArcEngine二次开发36: 在地理数据库中创建要素类的接口及方法分析(下)
基于C#的ArcEngine二次开发36: 在地理数据库中创建要素类的接口及方法分析(下)
0 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
《开放搜索查询分析服务架构分享》
立即下载
软件定义的数据智能
立即下载
动态、高效,蚂蚁动态卡片的内核逻辑
立即下载