ArcGIS Pro SDK 将几何输出为要素

需求:

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

解决方案:

1.创建要素

2.将几何写进要素中

//构建面几何
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() {
                                        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(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(值得查看)

你可能感兴趣的:(arcgisprosdk,arcgis,arcgisprosdk)