【ArcGIS Pro二次开发】(40):创建行(Row)和要素(Feature)

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

规划GIS会

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值