ArcGIS Pro SDK 将几何输出为要素

需求:

在获取到一个几何(geometry)的数据,我们需要将其输出为要素

解决方案:

1.创建要素

2.将几何写进要素中

cs 复制代码
//构建面几何
ArcGIS.Core.Geometry.Polygon mfGeoPolygon = new PolygonBuilderEx(mfGeoList, mapFrame.Map.SpatialReference).ToGeometry();
// 用于存储消息的字符串
string message = string.Empty;
// 标记创建结果的布尔值
bool creationResult = false;
var testName = $@"示例点{DateTime.Now:HHmmss}";
//创建要素类
 var hasZ = false;
var hasM = false;
// 创建一个ShapeDescription
var shapeDescription = new ShapeDescription(GeometryType.Polygon, SpatialReferences.WebMercator)
    {
        HasM = hasM,
        HasZ = hasZ
    };
                        // 定义字段
                        var stringFieldDescription = new ArcGIS.Core.Data.DDL.FieldDescription("字符串字段", FieldType.String);
                        // 打开默认数据库gdb
                        using (Geodatabase gdb = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(gdbPath))))
                        {
                            var fcName = $@"{testName}";
                            try
                            {
                                // 收集字段列表
                                var fieldDescriptions = new List<ArcGIS.Core.Data.DDL.FieldDescription>() {
                                        stringFieldDescription
                              };
                                // 创建FeatureClassDescription
                                var fcDescription = new FeatureClassDescription(fcName, fieldDescriptions, shapeDescription);
                                // 创建SchemaBuilder
                                SchemaBuilder schemaBuilder = new SchemaBuilder(gdb);
                                // 将创建任务添加到DDL任务列表中
                                schemaBuilder.Create(fcDescription);
                                // 执行DDL
                                bool success = schemaBuilder.Build();

                                // 打开示例要素
                                using FeatureClass enterpriseFeatureClass = gdb.OpenDataset<FeatureClass>(fcName);
                                // 创建编辑操作对象
                                EditOperation editOperation = new EditOperation();
                                editOperation.Callback(context =>
                                {
                                    // 获取要素定义
                                    FeatureClassDefinition featureClassDefinition = enterpriseFeatureClass.GetDefinition();
                                    // 创建RowBuffer
                                    using RowBuffer rowBuffer = enterpriseFeatureClass.CreateRowBuffer();
                                    // 写入字段值
                                    //rowBuffer["SHP名称"] = "新名称";
                                    // 设置一组点集合,用来构面
                                    rowBuffer[featureClassDefinition.GetShapeField()] = mfGeoPolygon;
                                    // 在表中创建新行
                                    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);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show($@"Exception: {ex}");
                            }

参考文献:

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

【ArcGIS Pro二次开发】(19):创建要素类(FeatureClass)_gdb要素类-CSDN博客

https://blog.csdn.net/xcc34452366/category_12199819_2.html(值得查看)

相关推荐
你是一个铁憨憨3 天前
从 GIS 到 Spatial Agent:MCP 如何重新定义 GIS 的 AI 入口
arcgis·ai·agent·mcp·spatial
码农小旋风7 天前
Claude Code 最佳实践指南
arcgis
兔年鸿运Q小Q17 天前
cesium1.140以上版本加载地形
arcgis·cesium
杨超越luckly17 天前
Agent应用指南:巨幕之下 · 中国 IMAX 影院205城的空间布局
人工智能·arcgis·html·agent·数据可视化
GISerQ.19 天前
DWG格式文件导入到ArcGIS中
arcgis·shp·cad文件·dwg文件
En^_^Joy23 天前
Vue项目创建与入口配置全攻略
前端·vue.js·arcgis
图件制作GIS,储量计算24 天前
补充耕地项目类、非项目类资料清单
arcgis
不在逃避q24 天前
Trae/Vs Code/Cursor命令行无法跑npm命令
前端·arcgis·npm
GISerQ.25 天前
ArcGIS实现矢量拓扑检查与修复
arcgis·拓扑检查·拓扑修复
NutShell Wang25 天前
零依赖架构实战:451个HTML文件如何用63KB平均体积交付3584个交互模块
前端·人工智能·arcgis·npm·开源·html·vibe coding