ArcGIS Pro SDK (八)地理数据库 5 编辑
文章目录
- [ArcGIS Pro SDK (八)地理数据库 5 编辑](#ArcGIS Pro SDK (八)地理数据库 5 编辑)
-
- [1 创建行](#1 创建行)
- [2 创建要素](#2 创建要素)
- [3 修改行](#3 修改行)
- [4 修改要素](#4 修改要素)
- [5 将值写入 Guid 列](#5 将值写入 Guid 列)
- [6 删除行/要素](#6 删除行/要素)
- [7 添加附件](#7 添加附件)
- [8 更新附件](#8 更新附件)
- [9 删除附件](#9 删除附件)
- [10 写入 Blob 字段](#10 写入 Blob 字段)
- [11 读取 Blob 字段](#11 读取 Blob 字段)
- [12 获取按关系类关联的行](#12 获取按关系类关联的行)
- [13 创建关系](#13 创建关系)
- [14 删除关系](#14 删除关系)
- [15 使用插入游标](#15 使用插入游标)
- [16 使用 RowBuffer 在注记要素类中创建新的注记要素](#16 使用 RowBuffer 在注记要素类中创建新的注记要素)
环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0
1 创建行
csharp
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 创建要素
csharp
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 修改行
csharp
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 修改要素
csharp
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 列
csharp
row[field.Name] = "{" + guid.ToString() + "}";
6 删除行/要素
csharp
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 添加附件
csharp
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 更新附件
csharp
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 删除附件
csharp
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 字段
csharp
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 字段
csharp
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 获取按关系类关联的行
csharp
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 创建关系
csharp
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 删除关系
csharp
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 使用插入游标
csharp
// 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 在注记要素类中创建新的注记要素
csharp
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();
}
});
}