ArcGIS Pro SDK (八)地理数据库 5 编辑

ArcGIS Pro SDK (八)地理数据库 5 编辑

环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0

1 创建行

public async Task CreatingARow()
{
    string message = String.Empty;
    bool creationResult = false;
    EditOperation editOperation = new EditOperation();

    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(
                new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (Table enterpriseTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.piCIPCost"))
            {

                //var geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(uri)) for a File GDB
                //用于文件地理数据库的 geodatabase

                //var shapeFileConnPath = new FileSystemConnectionPath(uri, FileSystemDatastoreType.Shapefile);
                //var shapefile = new FileSystemDatastore(shapeFileConnPath);
                //var table = shapefile.OpenDataset<Table>(strShapeFileName); 用于 Shapefile

                //声明回调函数。我们还没有执行它
                editOperation.Callback(
                    context =>
                    {
                        TableDefinition tableDefinition = enterpriseTable.GetDefinition();
                        int assetNameIndex = tableDefinition.FindField("ASSETNA");

                        using (RowBuffer rowBuffer = enterpriseTable.CreateRowBuffer())
                        {
                            // 可以使用字段索引或字段名称
                            rowBuffer[assetNameIndex] = "wMain";
                            rowBuffer["COST"] = 700;
                            rowBuffer["ACTION"] = "Open Cut";

                            // 子类型值为“Abandon”。
                            rowBuffer[tableDefinition.GetSubtypeField()] = 3;

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

    if (!string.IsNullOrEmpty(message))
        MessageBox.Show(message);
}

2 创建要素

public async Task CreatingAFeature()
{
    string message = String.Empty;
    bool creationResult = false;

    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.FacilitySite"))
            {
                //var geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(uri)) for a File GDB
                //用于文件地理数据库的 geodatabase

                //var shapeFileConnPath = new FileSystemConnectionPath(uri, FileSystemDatastoreType.Shapefile);
                //var shapefile = new FileSystemDatastore(shapeFileConnPath);
                //var table = shapefile.OpenDataset<Table>(strShapeFileName); 用于 Shapefile

                //声明回调函数。我们还没有执行它
                EditOperation editOperation = new EditOperation();
                editOperation.Callback(
                    context =>
                    {
                        FeatureClassDefinition facilitySiteDefinition = enterpriseFeatureClass.GetDefinition();
                        int facilityIdIndex = facilitySiteDefinition.FindField("FACILITYID");

                        using (RowBuffer rowBuffer = enterpriseFeatureClass.CreateRowBuffer())
                        {
                            // 可以使用字段索引或字段名称
                            rowBuffer[facilityIdIndex] = "wMain";
                            rowBuffer["NAME"] = "Griffith Park";
                            rowBuffer["OWNTYPE"] = "Municipal";
                            rowBuffer["FCODE"] = "Park";
                            // 添加到公共景点子类型
                            rowBuffer[facilitySiteDefinition.GetSubtypeField()] = 820;

                            List<Coordinate2D> newCoordinates = new List<Coordinate2D>
                            {
                                new Coordinate2D(1021570, 1880583),
                                new Coordinate2D(1028730, 1880994),
                                new Coordinate2D(1029718, 1875644),
                                new Coordinate2D(1021405, 1875397)
                            };

                            rowBuffer[facilitySiteDefinition.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;
                }
            }
        });

    if (!string.IsNullOrEmpty(message))
        MessageBox.Show(message);
}

3 修改行

public async Task ModifyingARow()
{
    string message = String.Empty;
    bool modificationResult = false;

    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (Table enterpriseTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.piCIPCost"))
            {
                //var geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(uri)) for a File GDB
                //用于文件地理数据库的 geodatabase

                //var shapeFileConnPath = new FileSystemConnectionPath(uri, FileSystemDatastoreType.Shapefile);
                //var shapefile = new FileSystemDatastore(shapeFileConnPath);
                //var table = shapefile.OpenDataset<Table>(strShapeFileName); 用于 Shapefile

                EditOperation editOperation = new EditOperation();
                editOperation.Callback(
                    context =>
                    {
                        QueryFilter openCutFilter = new QueryFilter { WhereClause = "ACTION = 'Open Cut'" };

                        using (RowCursor rowCursor = enterpriseTable.Search(openCutFilter, false))
                        {
                            TableDefinition tableDefinition = enterpriseTable.GetDefinition();
                            int subtypeFieldIndex = tableDefinition.FindField(tableDefinition.GetSubtypeField());

                            while (rowCursor.MoveNext())
                            {
                                using (Row row = rowCursor.Current)
                                {
                                    // 为了更新地图和/或属性表
                                    // 必须在对行进行任何更改之前调用
                                    context.Invalidate(row);

                                    row["ASSETNA"] = "wMainOpenCut";

                                    if (Convert.ToDouble(row["COST"]) > 700)
                                    {
                                        // 如果成本高于 700,则放弃资产(如果这是你想做的)。
                                        row["ACTION"] = "Open Cut Abandon";
                                        row[subtypeFieldIndex] = 3; //“Abandon”的子类型值
                                    }

                                    //所有更改完成后,保存更改。
                                    row.Store();

                                    // 保存之后也必须调用
                                    context.Invalidate(row);
                                }
                            }
                        }
                    }, enterpriseTable);

                try
                {
                    modificationResult = editOperation.Execute();
                    if (!modificationResult) message = editOperation.ErrorMessage;
                }
                catch (GeodatabaseException exObj)
                {
                    message = exObj.Message;
                }
            }
        });

    if (!string.IsNullOrEmpty(message))
        MessageBox.Show(message);
}

4 修改要素

public async Task ModifyingAFeature()
{
    string message = String.Empty;
    bool modificationResult = false;

    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.FacilitySite"))
            {

                //var geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(uri)) for a File GDB
                //用于文件地理数据库的 geodatabase

                //var shapeFileConnPath = new FileSystemConnectionPath(uri, FileSystemDatastoreType.Shapefile);
                //var shapefile = new FileSystemDatastore(shapeFileConnPath);
                //var table = shapefile.OpenDataset<Table>(strShapeFileName); 用于 Shapefile

                FeatureClassDefinition facilitySiteDefinition = enterpriseFeatureClass.GetDefinition();

                int ownTypeIndex = facilitySiteDefinition.FindField("OWNTYPE");
                int areaIndex = facilitySiteDefinition.FindField(facilitySiteDefinition.GetAreaField());

                EditOperation editOperation = new EditOperation();
                editOperation.Callback(
                    context =>
                    {
                        QueryFilter queryFilter = new QueryFilter { WhereClause = "FCODE = 'Hazardous Materials Facility' AND OWNTYPE = 'Private'" };

                        using (RowCursor rowCursor = enterpriseFeatureClass.Search(queryFilter, false))
                        {
                            while (rowCursor.MoveNext())
                            {
                                using (Feature feature = (Feature)rowCursor.Current)
                                {
                                    // 为了更新地图和/或属性表
                                    // 必须在对行进行任何更改之前调用
                                    context.Invalidate(feature);

                                    if (Convert.ToDouble(feature[areaIndex]) > 750)
                                    {
                                        //将所有 FCODE 值为“Hazardous Materials Facility”且 OWNTYPE 为“Private”的行更改为 OWNTYPE 值为“Municipal”
                                        feature[ownTypeIndex] = "Municipal";
                                        feature.Store();
                                    }

                                    // 保存之后也必须调用
                                    context.Invalidate(feature);
                                }
                            }
                        }
                    }, enterpriseFeatureClass);

                try
                {
                    modificationResult = editOperation.Execute();
                    if (!modificationResult) message = editOperation.ErrorMessage;
                }
                catch (GeodatabaseException exObj)
                {
                    message = exObj.Message;
                }
            }
        });

    if (!string.IsNullOrEmpty(message))
        MessageBox.Show(message);
}

5 将值写入 Guid 列

row[field.Name] = "{" + guid.ToString() + "}";

6 删除行/要素

public async Task DeletingARowOrFeature()
{
    string message = String.Empty;
    bool deletionResult = false;

    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (Table enterpriseTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.piCIPCost"))
            {
                //var geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(uri)) 对于 File GDB
                //
                //var shapeFileConnPath = new FileSystemConnectionPath(uri, FileSystemDatastoreType.Shapefile);
                //var shapefile = new FileSystemDatastore(shapeFileConnPath);
                //var table = shapefile.OpenDataset<Table>(strShapeFileName); 对于 Shape 文件

                EditOperation editOperation = new EditOperation();
                editOperation.Callback(
                    context =>
                    {
                        QueryFilter openCutFilter = new QueryFilter { WhereClause = "ACTION = 'Open Cut'" };

                        using (RowCursor rowCursor = enterpriseTable.Search(openCutFilter, false))
                        {
                            while (rowCursor.MoveNext())
                            {
                                using (Row row = rowCursor.Current)
                                {
                                    // 为了更新地图和/或属性表。必须在删除之前调用。
                                    context.Invalidate(row);

                                    row.Delete();
                                }
                            }
                        }
                    }, enterpriseTable);

                try
                {
                    deletionResult = editOperation.Execute();
                    if (!deletionResult) message = editOperation.ErrorMessage;
                }
                catch (GeodatabaseException exObj)
                {
                    message = exObj.Message;
                }
            }
        });

    if (!string.IsNullOrEmpty(message))
        MessageBox.Show(message);
}

7 添加附件

public async Task AddingAttachments()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (FeatureClass parkFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.Park"))
            {
                QueryFilter filter = new QueryFilter { WhereClause = "NUMPARKING > 0" };

                using (RowCursor parkingCursor = parkFeatureClass.Search(filter, false))
                {
                    while (parkingCursor.MoveNext())
                    {
                        using (MemoryStream stream = CreateMemoryStreamFromContentsOf("Sample.xml"))
                        {
                            Attachment attachment = new Attachment("Sample.xml", "text/xml", stream);

                            using (Row row = parkingCursor.Current)
                            {
                                long attachmentId = row.AddAttachment(attachment);
                            }
                        }
                    }
                }
            }
        });
}

private MemoryStream CreateMemoryStreamFromContentsOf(String fileNameWithPath)
{
    MemoryStream memoryStream = new MemoryStream();

    using (FileStream file = new FileStream(fileNameWithPath, FileMode.Open, FileAccess.Read))
    {
        byte[] bytes = new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);
        memoryStream.Write(bytes, 0, (int)file.Length);
    }

    return memoryStream;
}

8 更新附件

public async Task UpdatingAttachments()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (FeatureClass landUseCaseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.LandUseCase"))
            {
                QueryFilter filter = new QueryFilter { WhereClause = "CASETYPE = 'Rezoning'" };

                using (RowCursor landUseCursor = landUseCaseFeatureClass.Search(filter, false))
                {
                    while (landUseCursor.MoveNext())
                    {
                        using (Feature rezoningUseCase = (Feature)landUseCursor.Current)
                        {
                            IReadOnlyList<Attachment> rezoningAttachments = rezoningUseCase.GetAttachments();
                            IEnumerable<Attachment> filteredAttachments = rezoningAttachments.Where(attachment => !attachment.GetName().Contains("rezoning"));

                            foreach (Attachment attachmentToUpdate in filteredAttachments)
                            {
                                attachmentToUpdate.SetName(attachmentToUpdate.GetName().Replace(".pdf", "Rezoning.pdf"));
                                rezoningUseCase.UpdateAttachment(attachmentToUpdate);
                            }
                        }
                    }
                }
            }
        });
}

9 删除附件

public async Task DeletingAttachments()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
                using (Table inspectionTable = geodatabase.OpenDataset<Table>("luCodeInspection"))
            {
                QueryFilter queryFilter = new QueryFilter { WhereClause = "ACTION = '1st Notice'" };

                using (RowCursor cursor = inspectionTable.Search(queryFilter, false))
                {
                    while (cursor.MoveNext())
                    {
                        using (Row currentRow = cursor.Current)
                        {
                            IReadOnlyList<Attachment> rowAttachments = currentRow.GetAttachments(null, true);
                            IEnumerable<Attachment> attachments = rowAttachments.Where(attachment => attachment.GetContentType().Equals("application/pdf"));

                            var attachmentIDs = attachments.Select(attachment => attachment.GetAttachmentID()) as IReadOnlyList<long>;
                            IReadOnlyDictionary<long, Exception> failures = currentRow.DeleteAttachments(attachmentIDs);

                            if (failures.Count > 0)
                            {
                                //处理错误
                            }
                        }
                    }
                }
            }
        });
}

10 写入 Blob 字段

public async Task WriteBlobField(Table table, string blobFieldName, string imageFileName)
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            // 将图像文件读入 MemoryStream
            MemoryStream memoryStream = new MemoryStream(); ;
            using (FileStream imageFile = new FileStream(imageFileName, FileMode.Open, FileAccess.Read))
            {
                imageFile.CopyTo(memoryStream);
            }

            // 在表中创建新行,并将 MemoryStream 写入 blob 字段
            using (RowBuffer rowBuffer = table.CreateRowBuffer())
            {
                rowBuffer[blobFieldName] = memoryStream;
                table.CreateRow(rowBuffer).Dispose();
            }
        });
}

11 读取 Blob 字段

public async Task ReadBlobField(Table table, QueryFilter queryFilter, string blobFieldName)
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            const string imageFileBaseName = "C:\\path\\to\\image\\directory\\Image";

            // 对于满足搜索条件的每一行,将 blob 字段写出到图像文件
            using (RowCursor rowCursor = table.Search(queryFilter))
            {
                int fileCount = 0;
                while (rowCursor.MoveNext())
                {
                    using (Row row = rowCursor.Current)
                    {
                        // 将 blob 字段读入 MemoryStream
                        MemoryStream memoryStream = row[blobFieldName] as MemoryStream;

                        // 创建文件
                        using (FileStream outputFile = new FileStream(imageFileBaseName + fileCount.ToString(), FileMode.Create, FileAccess.Write))
                        {
                            // 将 MemoryStream 写入文件
                            memoryStream.WriteTo(outputFile);
                        }
                    }
                }
            }
        });
}

12 获取按关系类关联的行

public async Task GettingRowsRelatedByRelationshipClass()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
                using (RelationshipClass relationshipClass = geodatabase.OpenDataset<RelationshipClass>("LocalGovernment.GDB.luCodeViolationHasInspections"))
                using (FeatureClass violationsFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.luCodeViolation"))
                using (Table inspectionTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.luCodeInspection"))
            {
                List<Row> jeffersonAveViolations = new List<Row>();
                QueryFilter queryFilter = new QueryFilter { WhereClause = "LOCDESC LIKE '///%Jefferson///%'" };

                using (RowCursor rowCursor = violationsFeatureClass.Search(queryFilter, false))
                {
                    while (rowCursor.MoveNext())
                    {
                        jeffersonAveViolations.Add(rowCursor.Current);
                    }
                }

                IReadOnlyList<Row> relatedOriginRows = null;
                IReadOnlyList<Row> relatedDestinationRows = null;

                try
                {
                    QueryFilter filter = new QueryFilter { WhereClause = "ACTION = '1st Notice'" };

                    using (Selection selection = inspectionTable.Select(filter, SelectionType.ObjectID, SelectionOption.Normal))
                    {
                        relatedOriginRows = relationshipClass.GetRowsRelatedToDestinationRows(selection.GetObjectIDs());
                    }

                    bool containsJeffersonAve = relatedOriginRows.Any(row => Convert.ToString(row["LOCDESC"]).Contains("Jefferson"));

                    List<long> jeffersonAveViolationObjectIds = jeffersonAveViolations.Select(row => row.GetObjectID()).ToList();

                    relatedDestinationRows = relationshipClass.GetRowsRelatedToOriginRows(jeffersonAveViolationObjectIds);
                }
                finally
                {
                    if (relatedOriginRows != null)
                    {
                        foreach (Row row in relatedOriginRows)
                        {
                            row.Dispose();
                        }
                    }

                    if (relatedDestinationRows != null)
                    {
                        foreach (Row row in relatedDestinationRows)
                        {
                            row.Dispose();
                        }
                    }
                }
            }
        });
}

13 创建关系

public async Task CreatingARelationship()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
                using (RelationshipClass relationshipClass = geodatabase.OpenDataset<RelationshipClass>("LocalGovernment.GDB.OverviewToProject"))
                using (FeatureClass projectsFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.CIPProjects"))
                using (FeatureClass overviewFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.CIPProjectsOverview"))
            {
                // 获取关系类的原键字段,可以使用字段索引或字段名称
                string originKeyField = relationshipClass.GetDefinition().GetOriginKeyField();

                EditOperation editOperation = new EditOperation();
                editOperation.Callback(
                    context =>
                    {
                        // 添加示例数据行以创建关系。可以使用已有的行来创建关系
                        using (RowBuffer projectsRowBuffer = projectsFeatureClass.CreateRowBuffer())
                            using (RowBuffer overviewRowBuffer = overviewFeatureClass.CreateRowBuffer())
                        {
                            projectsRowBuffer["TOTCOST"] = 500000;

                            overviewRowBuffer[originKeyField] = "LibraryConstruction";
                            overviewRowBuffer["PROJECTMAN"] = "John Doe";
                            overviewRowBuffer["FUNDSOUR"] = "Public";

                            using (Row projectsRow = projectsFeatureClass.CreateRow(projectsRowBuffer))
                                using (Row overviewRow = overviewFeatureClass.CreateRow(overviewRowBuffer))
                            {
                                Relationship relationship = relationshipClass.CreateRelationship(overviewRow, projectsRow);

                                // 更新地图和属性表
                                context.Invalidate(projectsRow);
                                context.Invalidate(overviewRow);
                                context.Invalidate(relationshipClass);
                            }
                        }
                    }, projectsFeatureClass, overviewFeatureClass);

                bool editResult = editOperation.Execute();
            }
        });
}

14 删除关系

public async Task DeletingARelationship()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
                using (RelationshipClass relationshipClass = geodatabase.OpenDataset<RelationshipClass>("LocalGovernment.GDB.luCodeViolationHasInspections"))
                using (FeatureClass violationsFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.luCodeViolation"))
            {
                QueryFilter queryFilter = new QueryFilter { WhereClause = "LOCDESC LIKE '///%Jefferson///%'" };

                using (RowCursor rowCursor = violationsFeatureClass.Search(queryFilter, false))
                {
                    if (!rowCursor.MoveNext())
                        return;

                    using (Row jeffersonAveViolation = rowCursor.Current)
                    {
                        IReadOnlyList<Row> relatedDestinationRows = relationshipClass.GetRowsRelatedToOriginRows(new List<long> { jeffersonAveViolation.GetObjectID() });

                        try
                        {
                            EditOperation editOperation = new EditOperation();
                            editOperation.Callback(
                                context =>
                                {
                                    foreach (Row relatedDestinationRow in relatedDestinationRows)
                                    {
                                        try
                                        {
                                            relationshipClass.DeleteRelationship(jeffersonAveViolation, relatedDestinationRow);
                                        }
                                        catch (GeodatabaseRelationshipClassException exception)
                                        {
                                            Console.WriteLine(exception);
                                        }
                                    }
                                }, relationshipClass);

                            bool editResult = editOperation.Execute();
                        }
                        finally
                        {
                            foreach (Row row in relatedDestinationRows)
                                row.Dispose();
                        }
                    }
                }
            }
        });
}

15 使用插入游标

// Insert Cursors 适用于 CoreHost 应用,而非 Pro Add-ins
public void UsingInsertCursor()
{
    using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
        using (Table citiesTable = geodatabase.OpenDataset<Table>("name\\of\\cities_table"))
    {
        geodatabase.ApplyEdits(
            () =>
            {
                using (InsertCursor insertCursor = citiesTable.CreateInsertCursor())
                    using (RowBuffer rowBuffer = citiesTable.CreateRowBuffer())
                {
                    rowBuffer["State"] = "Colorado";

                    rowBuffer["Name"] = "Fort Collins";
                    rowBuffer["Population"] = 167830;
                    insertCursor.Insert(rowBuffer);

                    rowBuffer["Name"] = "Denver";
                    rowBuffer["Population"] = 727211;
                    insertCursor.Insert(rowBuffer);

                    // 插入更多行
                    // 现实应用中可能会从文件中读取源数据

                    insertCursor.Flush();
                }
            });
    }
}

16 使用 RowBuffer 在注记要素类中创建新的注记要素

public async Task CreatingAnAnnotationFeature(Geodatabase geodatabase)
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (AnnotationFeatureClass annotationFeatureClass = geodatabase.OpenDataset<AnnotationFeatureClass>("Annotation // feature // class // name"))
                using (AnnotationFeatureClassDefinition annotationFeatureClassDefinition = annotationFeatureClass.GetDefinition())
                using (RowBuffer rowBuffer = annotationFeatureClass.CreateRowBuffer())
                using (AnnotationFeature annotationFeature = annotationFeatureClass.CreateRow(rowBuffer))
            {
                annotationFeature.SetAnnotationClassID(0);
                annotationFeature.SetStatus(AnnotationStatus.Placed);

                // 获取注记类的标签集合
                IReadOnlyList<CIMLabelClass> labelClasses = 
                    annotationFeatureClassDefinition.GetLabelClassCollection();

                // 设置符号引用,包括符号 ID 和文本符号
                CIMSymbolReference cimSymbolReference = new CIMSymbolReference();
                cimSymbolReference.Symbol = labelClasses[0].TextSymbol.Symbol;
                cimSymbolReference.SymbolName = labelClasses[0].TextSymbol.SymbolName;

                // 设置文本图形
                CIMTextGraphic cimTextGraphic = new CIMTextGraphic();
                cimTextGraphic.Text = "Charlotte, North Carolina";
                cimTextGraphic.Shape = new MapPointBuilderEx(new Coordinate2D(-80.843, 35.234), SpatialReferences.WGS84).ToGeometry();
                cimTextGraphic.Symbol = cimSymbolReference;

                // 设置图形上的符号引用并存储
                annotationFeature.SetGraphic(cimTextGraphic);
                annotationFeature.Store();
            }
        });
}

相关推荐

  1. ArcGIS Pro SDK (地理数据库 5 编辑

    2024-07-11 07:28:03       11 阅读
  2. ArcGIS Pro SDK (地理数据库 3 数据

    2024-07-11 07:28:03       13 阅读
  3. ArcGIS Pro SDK (地理数据库 2 定义

    2024-07-11 07:28:03       10 阅读
  4. ArcGIS Pro SDK (地理数据库 6 版本控制

    2024-07-11 07:28:03       9 阅读
  5. ArcGIS Pro SDK (地理数据库 4 查询

    2024-07-11 07:28:03       5 阅读
  6. ArcGIS Pro SDK (地理数据库 8 拓扑

    2024-07-11 07:28:03       8 阅读
  7. 编程笔记 html5&css&js 035 HTML 地理定位

    2024-07-11 07:28:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 07:28:03       7 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 07:28:03       7 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 07:28:03       6 阅读
  4. Python语言-面向对象

    2024-07-11 07:28:03       9 阅读

热门阅读

  1. 自动驾驶论文总结

    2024-07-11 07:28:03       16 阅读
  2. Django 视图 - FBV 与 CBV

    2024-07-11 07:28:03       6 阅读
  3. Qt编程技巧小知识点(1)TCP缓存区数据读取

    2024-07-11 07:28:03       6 阅读
  4. uniapp小程序连接蓝牙设备

    2024-07-11 07:28:03       9 阅读
  5. 富格林:可信技巧隔绝遭遇欺诈

    2024-07-11 07:28:03       7 阅读
  6. WPF-控件样式设置

    2024-07-11 07:28:03       7 阅读
  7. C# —— BufferedStream的

    2024-07-11 07:28:03       7 阅读