在ArcGIS中,如果需要向独立表(StandaloneTable)添加行(Row),或者向要素类(FeatureClass)添加要素(Feature),一般情况下都是在编辑状态下进行手动编辑的。
在工具箱中也有一些类似工具,比如【追加、合并】等,缺点是自由度不高,特别是涉及更改字段值等操作的时候。
本文演示的是在ArcGIS Pro SDK中进行编辑操作,主要是【向独立表(StandaloneTable)添加行(Row)】和【向要素类(FeatureClass)添加要素(Feature)】。
以下代码基本来自官方文档,想学习更多内容,可以自行查看:
https://github.com/Esri/arcgis-pro-sdk/wiki/ProSnippets-Geodatabase#creating-a-row
一、向独立表(StandaloneTable)添加行(Row)
// 创建一个表行【Row】
public static async Task CreatingRow()
{
// 用于存储消息的字符串
string message = String.Empty;
// 标记创建结果的布尔值
bool creationResult = false;
// 创建编辑操作对象
EditOperation editOperation = new EditOperation();
// 获取默认地理数据库路径
string gdb = Project.Current.DefaultGeodatabasePath;
// 表名称
string table_name = "规划S";
// 打开表,并创建Row
await QueuedTask.Run(() =>
{
// 使用地理数据库路径创建地理数据库对象
using Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(gdb)));
// 打开表数据集
using Table enterpriseTable = geodatabase.OpenDataset<Table>(table_name);
editOperation.Callback(context =>
{
// 获取表定义
TableDefinition tableDefinition = enterpriseTable.GetDefinition();
// 创建RowBuffer
using RowBuffer rowBuffer = enterpriseTable.CreateRowBuffer();
// 写入字段值
rowBuffer["大类编码"] = "新建编码";
rowBuffer["FREQUENCY"] = 15;
// 在表中创建新行
using Row row = enterpriseTable.CreateRow(rowBuffer);
context.Invalidate(row); // 标记行为无效状态
}, enterpriseTable);
try
{
// 执行编辑操作
creationResult = editOperation.Execute();
// 如果操作失败,存储错误消息
if (!creationResult) { message = editOperation.ErrorMessage; }
}
catch (GeodatabaseException exObj)
{
// 如果出现地理数据库异常,存储异常消息
message = exObj.Message;
throw;
}
});
if (!string.IsNullOrEmpty(message))
{
// 如果消息不为空,显示消息框
MessageBox.Show(message);
}
}
二、向要素类(FeatureClass)添加要素(Feature)
// 创建一个要素【Feature】
public static async Task CreatingFeature()
{
// 用于存储消息的字符串
string message = String.Empty;
// 标记创建结果的布尔值
bool creationResult = false;
// 获取默认地理数据库路径
string gdb = Project.Current.DefaultGeodatabasePath;
// 表名称
string fc_name = "shps";
// 打开表,并创建Row
await QueuedTask.Run(() =>
{
// 使用地理数据库路径创建地理数据库对象
using Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(gdb)));
// 打开示例要素
using FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset<FeatureClass>(fc_name);
// 创建编辑操作对象
EditOperation editOperation = new EditOperation();
editOperation.Callback(context =>
{
// 获取要素定义
FeatureClassDefinition featureClassDefinition = enterpriseFeatureClass.GetDefinition();
// 创建RowBuffer
using RowBuffer rowBuffer = enterpriseFeatureClass.CreateRowBuffer();
// 写入字段值
rowBuffer["SHP名称"] = "新名称";
// 设置一组点集合,用来构面
List<Coordinate2D> newCoordinates = new List<Coordinate2D>
{
new Coordinate2D(1021570, 1880583),
new Coordinate2D(1028730, 1880994),
new Coordinate2D(1029718, 1875644),
new Coordinate2D(1021405, 1875397)
};
// 给新添加的行设置形状
rowBuffer[featureClassDefinition.GetShapeField()] = new PolygonBuilderEx(newCoordinates).ToGeometry();
// 在表中创建新行
using Feature feature = enterpriseFeatureClass.CreateRow(rowBuffer);
context.Invalidate(feature); // 标记行为无效状态
}, enterpriseFeatureClass);
try
{
// 执行编辑操作
creationResult = editOperation.Execute();
// 如果操作失败,存储错误消息
if (!creationResult) { message = editOperation.ErrorMessage; }
}
catch (GeodatabaseException exObj)
{
// 如果出现地理数据库异常,存储异常消息
message = exObj.Message;
throw;
}
});
if (!string.IsNullOrEmpty(message))
{
// 如果消息不为空,显示消息框
MessageBox.Show(message);
}
}