GeoDataBase要素的添加和删除、属性的读取和更新

1. 要素的添加

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

 

 

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

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

 

2. 要素的删除

删除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.

示例:

///<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);  
}  

 

3. 属性的读取

 

在获取属性表的值时有多种方法:

方法一:

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

方法二:

IFeatureCursor FCursor = pLayer.FeatureClass.Search(new QueryFilterClass(), false);  
IFeature feature = FCursor.NextFeature();  
if (feature == null) return null;  
clsFldValue = feature.get_Value(clsFldIndex);  
feature = FCursor.NextFeature();  

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

 

完整测试代码如下:

 IFeatureLayer pLayer = Utilities.GetLayerByName((string)cmbRegLayers.SelectedItem, m_mapControl) as IFeatureLayer;  
 IFeatureCursor FCursor = pLayer.FeatureClass.Search(new QueryFilterClass(), false);  
 IFeature feature = FCursor.NextFeature();  
  
 int t = Environment.TickCount;  
  
 object clsFldValue=null;  
 for (int i = 0; i < pLayer.FeatureClass.FeatureCount(null); i++)  
 {  
     clsFldValue = feature.get_Value(3);  
     feature = FCursor.NextFeature();  
 }  
  
 t = Environment.TickCount - t;  
 MessageBox.Show(t.ToString());  
  
 ITable pTable = pLayer.FeatureClass as ITable;  
  
 t = Environment.TickCount;  
  
 for (int i = 0; i < pTable.RowCount(null); i++)  
     clsFldValue = pTable.GetRow(i).get_Value(3);  
 t = Environment.TickCount - t;  
 MessageBox.Show(t.ToString());  

4.属性的更新

一、当将一批数据更新为某一相同的属性时,使用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);  
}  

二、逐条更新记录

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

(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倍!!

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用ArcObjects来实现这个功能。具体步骤如下: 1. 在ArcMap中打开需要添加属性值的要素类 2. 打开ArcMap的开发环境,创建一个新的C#项目 3. 在项目中添加对ESRI.ArcGIS.Geodatabase和ESRI.ArcGIS.DataSourcesRaster引用 4. 编写代码来读取栅格图像的像素值,并将其添加要素类的属性表中 以下是一个示例代码,可以帮助你读取栅格图像的像素值并将其添加要素类的属性表中: ```csharp //获取要素类 IFeatureClass featureClass = GetFeatureClass(); //获取栅格数据集 IRasterDataset rasterDataset = GetRasterDataset(); //获取栅格数据 IRaster2 raster = (IRaster2)rasterDataset.CreateDefaultRaster(); //获取栅格范围 IRasterProps rasterProps = (IRasterProps)raster; IRasterGeometry geo = rasterProps.Extent as IRasterGeometry; //获取栅格分辨率 double cellSize = rasterProps.MeanCellSize().X; //循环要素 IFeatureCursor cursor = featureClass.Update(null, false); IFeature feature = cursor.NextFeature(); while (feature != null) { //获取要素中心点 IPoint point = (IPoint)feature.ShapeCopy; point.Project(rasterProps.SpatialReference); double x = point.X; double y = point.Y; //获取栅格值 IPnt pnt = new PntClass(); pnt.SetCoords((int)((x - geo.Envelope.XMin) / cellSize), (int)((geo.Envelope.YMax - y) / cellSize)); IRasterProps2 rasterProps2 = (IRasterProps2)raster; object value = rasterProps2.GetPixelValue(0, pnt); //将栅格值添加要素属性表中 feature.set_Value(feature.Fields.FindField("PixelValue"), value); //更新要素 cursor.UpdateFeature(feature); //获取下一个要素 feature = cursor.NextFeature(); } ``` 在代码中,GetFeatureClass()和GetRasterDataset()是自定义的函数,用于获取要素类和栅格数据集。你需要根据自己的数据来修改这些函数。此外,你还需要将"PixelValue"替换为你要添加属性字段名称。 希望这个示例代码可以帮助你实现读取栅格图像的像素值并将其添加要素类的属性表中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值