ArcGIS Pro SDK (八)地理数据库 7 DDL

ArcGIS Pro SDK (八)地理数据库 7 DDL

文章目录

  • [ArcGIS Pro SDK (八)地理数据库 7 DDL](#ArcGIS Pro SDK (八)地理数据库 7 DDL)
    • [1 创建表](#1 创建表)
    • [2 创建要素类](#2 创建要素类)
    • [3 删除表](#3 删除表)
    • [4 删除要素类](#4 删除要素类)
    • [5 打开内存地理数据库](#5 打开内存地理数据库)
    • [6 创建内存地理数据库](#6 创建内存地理数据库)
    • [7 删除内存地理数据库](#7 删除内存地理数据库)
    • [8 创建文件地理数据库](#8 创建文件地理数据库)
    • [9 删除文件地理数据库](#9 删除文件地理数据库)
    • [10 创建移动地理数据库](#10 创建移动地理数据库)
    • [11 删除移动地理数据库](#11 删除移动地理数据库)
    • [12 创建范围域](#12 创建范围域)
    • [13 创建编码值域](#13 创建编码值域)
    • [14 创建要素数据集](#14 创建要素数据集)
    • [15 删除要素数据集](#15 删除要素数据集)
    • [16 重命名要素数据集](#16 重命名要素数据集)
    • [17 在一次操作中创建具有要素类的要素数据集](#17 在一次操作中创建具有要素类的要素数据集)
    • [18 在现有要素数据集中创建要素类](#18 在现有要素数据集中创建要素类)
    • [19 将要素类添加到要素数据集](#19 将要素类添加到要素数据集)
    • [20 重命名表](#20 重命名表)
    • [21 向要素类添加字段](#21 向要素类添加字段)
    • [22 添加使用域的字段](#22 添加使用域的字段)
    • [23 从表中删除字段](#23 从表中删除字段)
    • [24 创建注记要素类](#24 创建注记要素类)
    • [25 创建关联要素的注记要素类](#25 创建关联要素的注记要素类)
    • [26 在要素数据集内创建注记要素类](#26 在要素数据集内创建注记要素类)

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

1 创建表

csharp 复制代码
// 创建一个PoleInspection表,包含以下字段
//  GlobalID
//  ObjectID
//  InspectionDate(日期)
//  InspectionResults(预定义的InspectionResults编码值域)
//  InspectionNotes(字符串)

// 这个静态辅助方法创建一个带有默认值的GlobalID字段的FieldDescription
FieldDescription globalIDFieldDescription = FieldDescription.CreateGlobalIDField();

// 这个静态辅助方法创建一个带有默认值的ObjectID字段的FieldDescription
FieldDescription objectIDFieldDescription = FieldDescription.CreateObjectIDField();

// 为InspectionDate字段创建一个FieldDescription
FieldDescription inspectionDateFieldDescription = new FieldDescription("InspectionDate", FieldType.Date)
{
    AliasName = "Inspection Date"
};

// 这个静态辅助方法为域字段创建一个FieldDescription(来自预定义的域)
FieldDescription inspectionResultsFieldDescription = FieldDescription.CreateDomainField("InspectionResults", new CodedValueDomainDescription(inspectionResultsDomain));
inspectionResultsFieldDescription.AliasName = "Inspection Results";

// 这个静态辅助方法为字符串字段创建一个FieldDescription
FieldDescription inspectionNotesFieldDescription = FieldDescription.CreateStringField("InspectionNotes", 512);
inspectionNotesFieldDescription.AliasName = "Inspection Notes";

// 汇总所有字段描述的列表
List<FieldDescription> fieldDescriptions = new List<FieldDescription>()
{ globalIDFieldDescription, objectIDFieldDescription, inspectionDateFieldDescription, inspectionResultsFieldDescription, inspectionNotesFieldDescription };

// 创建一个描述要创建表的TableDescription对象
TableDescription tableDescription = new TableDescription("PoleInspection", fieldDescriptions);

// 创建一个SchemaBuilder对象
SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将PoleInspection表的创建添加到我们的DDL任务列表中
schemaBuilder.Create(tableDescription);

// 执行DDL
bool success = schemaBuilder.Build();

// 检查错误信息
if (!success)
{
    IReadOnlyList<string> errorMessages = schemaBuilder.ErrorMessages;
    //等等
}

2 创建要素类

csharp 复制代码
// 创建一个Cities要素类,包含以下字段
//  GlobalID
//  ObjectID
//  Name(字符串)
//  Population(整数)

// 这个静态辅助方法创建一个带有默认值的GlobalID字段的FieldDescription
FieldDescription globalIDFieldDescription = FieldDescription.CreateGlobalIDField();

// 这个静态辅助方法创建一个带有默认值的ObjectID字段的FieldDescription
FieldDescription objectIDFieldDescription = FieldDescription.CreateObjectIDField();

// 这个静态辅助方法为字符串字段创建一个FieldDescription
FieldDescription nameFieldDescription = FieldDescription.CreateStringField("Name", 255);

// 这个静态辅助方法为整数字段创建一个FieldDescription
FieldDescription populationFieldDescription = FieldDescription.CreateIntegerField("Population");

// 汇总所有字段描述的列表
List<FieldDescription> fieldDescriptions = new List<FieldDescription>()
{ globalIDFieldDescription, objectIDFieldDescription, nameFieldDescription, populationFieldDescription };

// 创建一个ShapeDescription对象
ShapeDescription shapeDescription = new ShapeDescription(GeometryType.Point, spatialReference);

// 或者,ShapeDescription可以从另一个要素类创建。在这种情况下,新的要素类将继承现有类的形状属性
ShapeDescription alternativeShapeDescription = new ShapeDescription(existingFeatureClass.GetDefinition());

// 创建一个描述要创建要素类的FeatureClassDescription对象
FeatureClassDescription featureClassDescription = 
    new FeatureClassDescription("Cities", fieldDescriptions, shapeDescription);

// 创建一个SchemaBuilder对象
SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将Cities要素类的创建添加到我们的DDL任务列表中
schemaBuilder.Create(featureClassDescription);

// 执行DDL
bool success = schemaBuilder.Build();

// 检查错误信息
if (!success)
{
    IReadOnlyList<string> errorMessages = schemaBuilder.ErrorMessages;
    //等等
}

3 删除表

csharp 复制代码
// 创建一个TableDescription对象
TableDescription tableDescription = new TableDescription(table.GetDefinition());

// 创建一个SchemaBuilder对象
SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将表的删除添加到我们的DDL任务列表中
schemaBuilder.Delete(tableDescription);

// 执行DDL
bool success = schemaBuilder.Build();

4 删除要素类

csharp 复制代码
// 创建一个FeatureClassDescription对象
FeatureClassDescription featureClassDescription = new FeatureClassDescription(featureClass.GetDefinition());

// 创建一个SchemaBuilder对象
SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将要素类的删除添加到我们的DDL任务列表中
schemaBuilder.Delete(featureClassDescription);

// 执行DDL
bool success = schemaBuilder.Build();

5 打开内存地理数据库

csharp 复制代码
// 连接到默认的内存地理数据库(如果存在),否则抛出异常
MemoryConnectionProperties memoryConnectionProperties = new MemoryConnectionProperties();

// 或者,连接到名为'InterimMemoryGeodatabase'的内存地理数据库
// MemoryConnectionProperties memoryConnectionProperties = new MemoryConnectionProperties("InterimMemoryGeodatabase");

// 打开内存地理数据库
using (Geodatabase geodatabase = new Geodatabase(memoryConnectionProperties))
{
    // 使用内存地理数据库
}

6 创建内存地理数据库

csharp 复制代码
// 创建连接默认内存地理数据库的内存连接属性
MemoryConnectionProperties memoryConnectionProperties = new MemoryConnectionProperties();

// 或者创建连接名为'InterimMemoryGeodatabase'的内存地理数据库的内存连接属性
// MemoryConnectionProperties memoryConnectionProperties = new MemoryConnectionProperties("InterimMemoryGeodatabase");

// 创建并使用内存地理数据库
using (Geodatabase geodatabase = SchemaBuilder.CreateGeodatabase(memoryConnectionProperties))
{
    // 在这里创建额外的模式
}

7 删除内存地理数据库

csharp 复制代码
// 创建连接默认内存地理数据库的内存连接属性
MemoryConnectionProperties memoryConnectionProperties = new MemoryConnectionProperties();

// 删除内存地理数据库
SchemaBuilder.DeleteGeodatabase(memoryConnectionProperties);

8 创建文件地理数据库

csharp 复制代码
// 创建FileGeodatabaseConnectionPath,指定要创建的文件地理数据库名称
FileGeodatabaseConnectionPath fileGeodatabaseConnectionPath = 
    new FileGeodatabaseConnectionPath(new Uri(@"C:\Path-To-File-Geodatabase\YourName.gdb"));

// 创建并使用文件地理数据库
using (Geodatabase geodatabase = 
       SchemaBuilder.CreateGeodatabase(fileGeodatabaseConnectionPath))
{
    // 在这里创建额外的模式
}

9 删除文件地理数据库

csharp 复制代码
// 创建FileGeodatabaseConnectionPath,指定要删除的文件地理数据库名称
FileGeodatabaseConnectionPath fileGeodatabaseConnectionPath = new FileGeodatabaseConnectionPath(new Uri(@"C:\Path-To-File-Geodatabase\YourName.gdb"));

// 删除文件地理数据库
SchemaBuilder.DeleteGeodatabase(fileGeodatabaseConnectionPath);

10 创建移动地理数据库

csharp 复制代码
// 创建MobileGeodatabaseConnectionPath,指定要创建的移动地理数据库名称
MobileGeodatabaseConnectionPath mobileGeodatabaseConnectionPath = 
    new MobileGeodatabaseConnectionPath(new Uri(@"C:\Path-To-Mobile-Geodatabase\YourName.geodatabase"));

// 创建并使用移动地理数据库
using (Geodatabase geodatabase = SchemaBuilder.CreateGeodatabase(mobileGeodatabaseConnectionPath))
{
    // 在这里创建额外的模式
}

11 删除移动地理数据库

csharp 复制代码
// 创建MobileGeodatabaseConnectionPath,指定要删除的移动地理数据库名称
MobileGeodatabaseConnectionPath mobileGeodatabaseConnectionPath = 
    new MobileGeodatabaseConnectionPath(new Uri(@"C:\Path-To-Mobile-Geodatabase\YourName.geodatabase"));

// 删除移动地理数据库
SchemaBuilder.DeleteGeodatabase(mobileGeodatabaseConnectionPath);

12 创建范围域

csharp 复制代码
// 创建一个范围描述,最小值 = 0,最大值 = 1000
RangeDomainDescription rangeDomainDescriptionMinMax = new RangeDomainDescription("RangeDomain_0_1000",
                                                                                 FieldType.Integer, 0, 1000)
{ Description = "Domain value ranges from 0 to 1000" };

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 创建范围域
schemaBuilder.Create(rangeDomainDescriptionMinMax);
schemaBuilder.Build();

13 创建编码值域

csharp 复制代码
// 创建一个用于水管的编码值域描述
CodedValueDomainDescription codedValueDomainDescription = new CodedValueDomainDescription(
    "WaterPipeTypes", 
    FieldType.String,
    new SortedList<object, string> 
    {
        { "Copper", "C_1" },
        { "Steel", "S_2" } 
    })
{
    SplitPolicy = SplitPolicy.Duplicate,
    MergePolicy = MergePolicy.DefaultValue
};

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 创建编码值域
CodedValueDomainToken codedValueDomainToken = schemaBuilder.Create(codedValueDomainDescription);
schemaBuilder.Build();

14 创建要素数据集

csharp 复制代码
// 创建名为'Parcel_Information'的要素数据集

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 创建名为'Parcel Information'的要素数据集
FeatureDatasetDescription featureDatasetDescription = 
    new FeatureDatasetDescription("Parcel_Information", SpatialReferences.WGS84);
schemaBuilder.Create(featureDatasetDescription);

// 构建状态
bool buildStatus = schemaBuilder.Build();

// 构建错误
if (!buildStatus)
{
    IReadOnlyList<string> errors = schemaBuilder.ErrorMessages;
}

15 删除要素数据集

csharp 复制代码
// 删除名为'Parcel_Information'的要素数据集

FeatureDatasetDefinition featureDatasetDefinition = geodatabase.GetDefinition<FeatureDatasetDefinition>("Parcel_Information");
FeatureDatasetDescription featureDatasetDescription = 
    new FeatureDatasetDescription(featureDatasetDefinition);

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 删除名为'Parcel_Information'的现有要素数据集
schemaBuilder.Delete(featureDatasetDescription);
schemaBuilder.Build();

16 重命名要素数据集

csharp 复制代码
// 将一个要素数据集从 'Parcel_Information' 重命名为 'Parcel_Information_With_Tax_Jurisdiction'

string originalDatasetName = "Parcel_Information";
string datasetRenameAs = "Parcel_Information_With_Tax_Jurisdiction";

FeatureDatasetDefinition originalDatasetDefinition = 
    geodatabase.GetDefinition<FeatureDatasetDefinition>(originalDatasetName);
FeatureDatasetDescription originalFeatureDatasetDescription = 
    new FeatureDatasetDescription(originalDatasetDefinition);

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将现有的要素数据集 'Parcel_Information' 重命名为 'Parcel_Information_With_Tax_Jurisdiction'
schemaBuilder.Rename(originalFeatureDatasetDescription, datasetRenameAs);
schemaBuilder.Build();

17 在一次操作中创建具有要素类的要素数据集

csharp 复制代码
// 在一次操作中创建一个名为 'Parcel_Information' 的要素数据集和一个名为 'Parcels' 的要素类

string featureDatasetName = "Parcel_Information";
string featureClassName = "Parcels";

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 创建一个要素数据集令牌
FeatureDatasetDescription featureDatasetDescription = new FeatureDatasetDescription(featureDatasetName, SpatialReferences.WGS84);
FeatureDatasetToken featureDatasetToken = schemaBuilder.Create(featureDatasetDescription);

// 创建一个要素类描述
FeatureClassDescription featureClassDescription = new FeatureClassDescription(
    featureClassName,
    new List<FieldDescription>()
    {
        new FieldDescription("Id", FieldType.Integer),
        new FieldDescription("Address", FieldType.String)
    },
    new ShapeDescription(GeometryType.Point, SpatialReferences.WGS84));

// 在要素数据集中创建一个要素类
FeatureClassToken featureClassToken = schemaBuilder.Create(new FeatureDatasetDescription(featureDatasetToken), featureClassDescription);

// 构建状态
bool buildStatus = schemaBuilder.Build();

// 构建错误
if (!buildStatus)
{
    IReadOnlyList<string> errors = schemaBuilder.ErrorMessages;
}

18 在现有要素数据集中创建要素类

csharp 复制代码
// 在现有名为 'Parcels_Information' 的要素数据集中创建一个名为 'Tax_Jurisdiction' 的要素类
string featureDatasetName = "Parcels_Information";
string featureClassName = "Tax_Jurisdiction";

// 创建一个要素类描述
FeatureClassDescription featureClassDescription = new FeatureClassDescription(
    featureClassName,
    new List<FieldDescription>()
    {
        new FieldDescription("Tax_Id", FieldType.Integer),
        new FieldDescription("Address", FieldType.String)
    },
    new ShapeDescription(GeometryType.Point, SpatialReferences.WGS84));

FeatureDatasetDefinition featureDatasetDefinition = geodatabase.GetDefinition<FeatureDatasetDefinition>(featureDatasetName);

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 使用FeatureDatasetDefinition在FeatureDataset中创建一个FeatureClass
schemaBuilder.Create(new FeatureDatasetDescription(featureDatasetDefinition), featureClassDescription);

// 构建状态
bool buildStatus = schemaBuilder.Build();

// 构建错误
if (!buildStatus)
{
    IReadOnlyList<string> errors = schemaBuilder.ErrorMessages;
}

19 将要素类添加到要素数据集

csharp 复制代码
// 将一个名为 'Tax_Jurisdiction' 的要素类添加到一个名为 'Parcels_Information' 的要素数据集中

string featureDatasetName = "Parcels_Information";
string featureClassNameToAdd = "Tax_Jurisdiction";

FeatureDatasetDefinition featureDatasetDefinition = geodatabase.GetDefinition<FeatureDatasetDefinition>(featureDatasetName);
FeatureDatasetDescription featureDatasetDescription = new FeatureDatasetDescription(featureDatasetDefinition);

FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(featureClassNameToAdd);
FeatureClassDescription featureClassDescription = new FeatureClassDescription(featureClassDefinition);

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将 'Tax_Jurisdiction' 要素类添加到 'Parcels_Information' 要素数据集中
schemaBuilder.AddFeatureClass(featureDatasetDescription, featureClassDescription);
bool addStatus = schemaBuilder.Build();

if (!addStatus)
{
    IReadOnlyList<string> errors = schemaBuilder.ErrorMessages;
}

20 重命名表

csharp 复制代码
// 将一个表从 'Original_Table' 重命名为 'Renamed_Table'

string tableToBeRenamed = "Original_Table";
string tableRenameAs = "Renamed_Table";

TableDefinition tableDefinition = geodatabase.GetDefinition<TableDefinition>(tableToBeRenamed);

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 表重命名
schemaBuilder.Rename(new TableDescription(tableDefinition), tableRenameAs);
schemaBuilder.Build();

21 向要素类添加字段

csharp 复制代码
// 向 'Parcels' 要素类添加以下字段
// 全局 ID
// Parcel_ID
// Tax_Code
// Parcel_Address

// 要添加字段的要素类
string featureClassName = "Parcels";

FeatureClassDefinition originalFeatureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(featureClassName);
FeatureClassDescription originalFeatureClassDescription = new 
    FeatureClassDescription(originalFeatureClassDefinition);

// 向 'Parcels' 要素类添加的四个新字段
FieldDescription globalIdField = FieldDescription.CreateGlobalIDField();
FieldDescription parcelIdDescription = new FieldDescription("Parcel_ID", FieldType.GUID);
FieldDescription taxCodeDescription = FieldDescription.CreateIntegerField("Tax_Code");
FieldDescription addressDescription = FieldDescription.CreateStringField("Parcel_Address", 150);

List<FieldDescription> fieldsToAdd = new List<FieldDescription> 
{ 
    globalIdField, parcelIdDescription,
    taxCodeDescription, addressDescription 
};

// 将新字段添加到新的 FieldDescription 列表中
List<FieldDescription> modifiedFieldDescriptions = new List<FieldDescription>(originalFeatureClassDescription.FieldDescriptions);
modifiedFieldDescriptions.AddRange(fieldsToAdd);

// 带有附加字段的新 FeatureClassDescription
FeatureClassDescription modifiedFeatureClassDescription = new FeatureClassDescription(
    originalFeatureClassDescription.Name,
    modifiedFieldDescriptions, 
    originalFeatureClassDescription.ShapeDescription);

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 用新添加的字段更新 'Parcels' 要素类
schemaBuilder.Modify(modifiedFeatureClassDescription);
bool modifyStatus = schemaBuilder.Build();

if (!modifyStatus)
{
    IReadOnlyList<string> errors = schemaBuilder.ErrorMessages;
}

22 添加使用域的字段

csharp 复制代码
// 添加一个名为"PipeType"的字段,该字段使用编码值域到"Pipes"要素类

// 要添加字段的要素类
string featureClassName = "Pipes";

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 创建一个水管的编码值域描述
CodedValueDomainDescription pipeDomainDescription = new CodedValueDomainDescription(
    "WaterPipeTypes", FieldType.String,
    new SortedList<object, string> {
        { "Copper", "C_1" },
        { "Steel", "S_2" }
    })
{
    SplitPolicy = SplitPolicy.Duplicate,
    MergePolicy = MergePolicy.DefaultValue
};

// 创建一个编码值域令牌
CodedValueDomainToken codedValueDomainToken = schemaBuilder.Create(pipeDomainDescription);

// 从域令牌创建一个新的描述
CodedValueDomainDescription codedValueDomainDescription = new CodedValueDomainDescription(codedValueDomainToken);

// 使用域描述创建一个名为"PipeType"的字段
FieldDescription domainFieldDescription = new FieldDescription("PipeType", FieldType.String)
{ DomainDescription = codedValueDomainDescription };

//检索"Pipes"要素类的现有信息
FeatureClassDefinition originalFeatureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(featureClassName);
FeatureClassDescription originalFeatureClassDescription = 
    new FeatureClassDescription(originalFeatureClassDefinition);

// 将域字段添加到现有字段中
List<FieldDescription> modifiedFieldDescriptions = new List<FieldDescription>(originalFeatureClassDescription.FieldDescriptions) { domainFieldDescription };

// 创建一个带有更新字段的"Pipes"要素类的新描述
FeatureClassDescription featureClassDescription = 
    new FeatureClassDescription(originalFeatureClassDescription.Name, modifiedFieldDescriptions,
                                originalFeatureClassDescription.ShapeDescription);

// 使用域字段更新"Pipes"要素类
schemaBuilder.Modify(featureClassDescription);

// 构建状态
bool buildStatus = schemaBuilder.Build();

// 构建错误
if (!buildStatus)
{
    IReadOnlyList<string> errors = schemaBuilder.ErrorMessages;
}

23 从表中删除字段

csharp 复制代码
// 从"Parcels"表中删除所有字段,保留以下字段
// Tax_Code
// Parcel_Address

// 要删除字段的表
string tableName = "Parcels";

TableDefinition tableDefinition = geodatabase.GetDefinition<TableDefinition>(tableName);
IReadOnlyList<Field> fields = tableDefinition.GetFields();

// 从"Parcels"表中获取现有字段
Field taxCodeField = fields.First(f => f.Name.Equals("Tax_Code"));
Field parcelAddressField = fields.First(f => f.Name.Equals("Parcel_Address"));

FieldDescription taxFieldDescription = new FieldDescription(taxCodeField);
FieldDescription parcelAddressFieldDescription = new FieldDescription(parcelAddressField);

// 修改后的表中要保留的字段
List<FieldDescription> fieldsToBeRetained = new List<FieldDescription>()
{
    taxFieldDescription, parcelAddressFieldDescription
};

// 包含"Tax_Code"和"Parcel_Address"字段的"Parcels"表的新描述
TableDescription modifiedTableDescription = new TableDescription(tableName, fieldsToBeRetained);

SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 删除除"Tax_Code"和"Parcel_Address"字段外的所有字段
schemaBuilder.Modify(modifiedTableDescription);
schemaBuilder.Build();

24 创建注记要素类

csharp 复制代码
// 创建一个名为"Cities"的注记要素类
// 包含以下用户定义字段
// Name 
// GlobalID

// 注记要素类名称
string annotationFeatureClassName = "CitiesAnnotation";

// 为注记要素类创建用户定义的属性字段 
FieldDescription globalIDFieldDescription = FieldDescription.CreateGlobalIDField();
FieldDescription nameFieldDescription = FieldDescription.CreateStringField("Name", 255);

// 创建所有字段描述的列表
List<FieldDescription> fieldDescriptions = new List<FieldDescription> { globalIDFieldDescription, nameFieldDescription };

// 创建一个ShapeDescription对象
ShapeDescription shapeDescription = new ShapeDescription(GeometryType.Polygon, spatialReference);

// 为Maplex引擎创建常规放置属性
CIMMaplexGeneralPlacementProperties generalPlacementProperties =
    new CIMMaplexGeneralPlacementProperties
{
    AllowBorderOverlap = true,
    PlacementQuality = MaplexQualityType.High,
    DrawUnplacedLabels = true,
    InvertedLabelTolerance = 1.0,
    RotateLabelWithDisplay = true,
    UnplacedLabelColor = new CIMRGBColor
    {
        R = 0,
        G = 255,
        B = 0,
        Alpha = 0.5f // 绿色
    }
};

// 为标准引擎创建常规放置属性

//CIMStandardGeneralPlacementProperties generalPlacementProperties =
//  new CIMStandardGeneralPlacementProperties
//  {
//    DrawUnplacedLabels = true,
//    InvertedLabelTolerance = 3.0,
//    RotateLabelWithDisplay = true,
//    UnplacedLabelColor = new CIMRGBColor
//    {
//      R = 255, G = 0, B = 0, Alpha = 0.5f // 红色
//    } 
//   };


// 创建注记标签类
// 绿色标签
CIMLabelClass greenLabelClass = new CIMLabelClass
{
    Name = "Green",
    ExpressionTitle = "Expression-Green",
    ExpressionEngine = LabelExpressionEngine.Arcade,
    Expression = "$feature.OBJECTID",
    ID = 1,
    Priority = 0,
    Visibility = true,
    TextSymbol = new CIMSymbolReference
    {
        Symbol = new CIMTextSymbol()
        {
            Angle = 45,
            FontType = FontType.Type1,
            FontFamilyName = "Tahoma",
            FontEffects = FontEffects.Normal,
            HaloSize = 2.0,

            Symbol = new CIMPolygonSymbol
            {
                SymbolLayers = new CIMSymbolLayer[]
                {
                    new CIMSolidFill
                    {
                        Color = CIMColor.CreateRGBColor(0, 255, 0)
                    }
                },
                UseRealWorldSymbolSizes = true
            }
        },
        MaxScale = 0,
        MinScale = 0,
        SymbolName = "TextSymbol-Green"
    },
    StandardLabelPlacementProperties = new CIMStandardLabelPlacementProperties
    {
        AllowOverlappingLabels = true,
        LineOffset = 1.0
    },
    MaplexLabelPlacementProperties = new CIMMaplexLabelPlacementProperties
    {
        AlignLabelToLineDirection = true,
        AvoidPolygonHoles = true
    }
};

// 蓝色标签
CIMLabelClass blueLabelClass = new CIMLabelClass
{
    Name = "Blue",
    ExpressionTitle = "Expression-Blue",
    ExpressionEngine = LabelExpressionEngine.Arcade,
    Expression = "$feature.OBJECTID",
    ID = 2,
    Priority = 0,
    Visibility = true,
    TextSymbol = new CIMSymbolReference
    {
        Symbol = new CIMTextSymbol()
        {
            Angle = 45,
            FontType = FontType.Type1,
            FontFamilyName = "Consolas",
            FontEffects = FontEffects.Normal,
            HaloSize = 2.0,

            Symbol = new CIMPolygonSymbol
            {
                SymbolLayers = new CIMSymbolLayer[]
                {
                    new CIMSolidFill
                    {
                        Color = CIMColor.CreateRGBColor(0, 0, 255)
                    }
                },
                UseRealWorldSymbolSizes = true
            }
        },
        MaxScale = 0,
        MinScale = 0,
        SymbolName = "TextSymbol-Blue"
    },
    StandardLabelPlacementProperties = new CIMStandardLabelPlacementProperties
    {
        AllowOverlappingLabels = true,
        LineOffset = 1.0
    },
    MaplexLabelPlacementProperties = new CIMMaplexLabelPlacementProperties
    {
        AlignLabelToLineDirection = true,
        AvoidPolygonHoles = true
    }
};

// 创建标签列表
List<CIMLabelClass> labelClasses = new List<CIMLabelClass> { greenLabelClass, blueLabelClass };

// 创建注记要素类描述对象以描述要创建的要素类
AnnotationFeatureClassDescription annotationFeatureClassDescription =
    new AnnotationFeatureClassDescription(annotationFeatureClassName, fieldDescriptions, shapeDescription,
                                          generalPlacementProperties, labelClasses)
{
    IsAutoCreate = true,
    IsSymbolIDRequired = false,
    IsUpdatedOnShapeChange = true
};

// 创建一个SchemaBuilder对象
SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将创建Cities注记要素类的操作添加到DDL任务列表中
schemaBuilder.Create(annotationFeatureClassDescription);

// 执行DDL
bool success = schemaBuilder.Build();

// 检查错误消息
if (!success)
{
    IReadOnlyList<string> errorMessages = schemaBuilder.ErrorMessages;
    //等。
}

25 创建关联要素的注记要素类

csharp 复制代码
// 在供水网络中创建水管和阀门之间的关联注记要素类
// 包含以下用户定义字段
// PipeName 
// GlobalID

// 注记要素类名称
string annotationFeatureClassName = "WaterPipeAnnotation";

// 为注记要素类创建用户定义的属性字段
FieldDescription pipeGlobalID = FieldDescription.CreateGlobalIDField();
FieldDescription nameFieldDescription = FieldDescription.CreateStringField("Name", 255);

// 创建所有字段描述的列表
List<FieldDescription> fieldDescriptions = new List<FieldDescription> { pipeGlobalID, nameFieldDescription };

// 创建一个ShapeDescription对象
ShapeDescription shapeDescription = new ShapeDescription(GeometryType.Polygon, spatialReference);

// 为Maplex引擎创建常规放置属性
CIMMaplexGeneralPlacementProperties generalPlacementProperties = new CIMMaplexGeneralPlacementProperties
{
    AllowBorderOverlap = true,
    PlacementQuality = MaplexQualityType.High,
    DrawUnplacedLabels = true,
    InvertedLabelTolerance = 1.0,
    RotateLabelWithDisplay = true,
    UnplacedLabelColor = new CIMRGBColor
    {
        R = 255,
        G = 0,
        B = 0,
        Alpha = 0.5f
    }
};

// 创建注记标签类
// 绿色标签
CIMLabelClass greenLabelClass = new CIMLabelClass
{
    Name = "Green",
    ExpressionTitle = "Expression-Green",
    ExpressionEngine = LabelExpressionEngine.Arcade,
    Expression = "$feature.OBJECTID",
    ID = 1,
    Priority = 0,
    Visibility = true,
    TextSymbol = new CIMSymbolReference
    {
        Symbol = new CIMTextSymbol()
        {
            Angle = 45,
            FontType = FontType.Type1,
            FontFamilyName = "Tahoma",
            FontEffects = FontEffects.Normal,
            HaloSize = 2.0,

            Symbol = new CIMPolygonSymbol
            {
                SymbolLayers = new CIMSymbolLayer[]
                {
                    new CIMSolidFill
                    {
                        Color = CIMColor.CreateRGBColor(0, 255, 0)
                    }
                },
                UseRealWorldSymbolSizes = true
            }
        },
        MaxScale = 0,
        MinScale = 0,
        SymbolName = "TextSymbol-Green"
    },
    StandardLabelPlacementProperties = new CIMStandardLabelPlacementProperties
    {
        AllowOverlappingLabels = true,
        LineOffset = 1.0
    },
    MaplexLabelPlacementProperties = new CIMMaplexLabelPlacementProperties
    {
        AlignLabelToLineDirection = true,
        AvoidPolygonHoles = true
    }
};

// 蓝色标签
CIMLabelClass blueLabelClass = new CIMLabelClass
{
    Name = "Blue",
    ExpressionTitle = "Expression-Blue",
    ExpressionEngine = LabelExpressionEngine.Arcade,
    Expression = "$feature.OBJECTID",
    ID = 2,
    Priority = 0,
    Visibility = true,
    TextSymbol = new CIMSymbolReference
    {
        Symbol = new CIMTextSymbol()
        {
            Angle = 45,
            FontType = FontType.Type1,
            FontFamilyName = "Consolas",
            FontEffects = FontEffects.Normal,
            HaloSize = 2.0,

            Symbol = new CIMPolygonSymbol
            {
                SymbolLayers = new CIMSymbolLayer[]
                {
                    new CIMSolidFill
                    {
                        Color = CIMColor.CreateRGBColor(0, 0, 255)
                    }
                },
                UseRealWorldSymbolSizes = true
            }
        },
        MaxScale = 0,
        MinScale = 0,
        SymbolName = "TextSymbol-Blue"
    },
    StandardLabelPlacementProperties = new CIMStandardLabelPlacementProperties
    {
        AllowOverlappingLabels = true,
        LineOffset = 1.0
    },
    MaplexLabelPlacementProperties = new CIMMaplexLabelPlacementProperties
    {
        AlignLabelToLineDirection = true,
        AvoidPolygonHoles = true
    }
};

// 创建标签列表
List<CIMLabelClass> labelClasses = new List<CIMLabelClass> { greenLabelClass, blueLabelClass };


// 创建关联要素描述
// 关联要素类名称
string linkedFeatureClassName = "WaterPipe";

// 为水管创建字段
FieldDescription waterPipeGlobalID = FieldDescription.CreateGlobalIDField();
FieldDescription pipeName = FieldDescription.CreateStringField("PipeName", 255);

// 创建水管字段描述的列表
List<FieldDescription> pipeFieldDescriptions = new List<FieldDescription> { waterPipeGlobalID, pipeName };

// 创建关联要素类描述
FeatureClassDescription linkedFeatureClassDescription = new FeatureClassDescription(linkedFeatureClassName, pipeFieldDescriptions,
                                                                                    new ShapeDescription(GeometryType.Polyline, spatialReference));

// 创建一个SchemaBuilder对象
SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 将创建关联要素类的操作添加到DDL任务列表中
FeatureClassToken linkedFeatureClassToken = schemaBuilder.Create(linkedFeatureClassDescription);

// 创建一个注记要素类描述对象以描述要创建的要素类
AnnotationFeatureClassDescription annotationFeatureClassDescription =
    new AnnotationFeatureClassDescription(annotationFeatureClassName, fieldDescriptions, shapeDescription,
                                          generalPlacementProperties, labelClasses, new FeatureClassDescription(linkedFeatureClassToken))
{
    IsAutoCreate = true,
    IsSymbolIDRequired = false,
    IsUpdatedOnShapeChange = true
};

// 将创建注记要素类的操作添加到DDL任务列表中
schemaBuilder.Create(annotationFeatureClassDescription);

// 执行DDL
bool success = schemaBuilder.Build();

// 检查错误消息
if (!success)
{
    IReadOnlyList<string> errorMessages = schemaBuilder.ErrorMessages;
    //等。
}

26 在要素数据集内创建注记要素类

csharp 复制代码
// 在Places要素数据集中使用现有注记要素类创建Cities注记要素类

// 要素数据集名称
string featureDatasetName = "Places";

// 注记要素类名称
string annotationFeatureClassName = "CitiesAnnotation";

// 创建一个SchemaBuilder对象
SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

// 打开现有的注记要素类
using (AnnotationFeatureClass existingAnnotationFeatureClass = geodatabase.OpenDataset<AnnotationFeatureClass>("ExistingAnnotationFeatureClass"))
{

    // 创建要素数据集描述
    FeatureDatasetDescription featureDatasetDescription = 
        new FeatureDatasetDescription(featureDatasetName, existingAnnotationFeatureClass.GetDefinition().GetSpatialReference());

    // 将创建Places数据集的操作添加到DDL任务中
    FeatureDatasetToken featureDatasetToken = schemaBuilder.Create(featureDatasetDescription);

    // 使用现有注记要素类创建注记要素类描述
    AnnotationFeatureClassDescription annotationFeatureClassDescription = new AnnotationFeatureClassDescription(annotationFeatureClassName, existingAnnotationFeatureClass.GetDefinition())
    {
        IsAutoCreate = true,
        IsSymbolIDRequired = false,
        IsUpdatedOnShapeChange = true
    };

    // 将创建Cities注记要素类的操作添加到Places要素数据集中
    schemaBuilder.Create(new FeatureDatasetDescription(featureDatasetToken), annotationFeatureClassDescription);

    // 执行DDL
    bool success = schemaBuilder.Build();

    // 检查错误消息
    if (!success)
    {
        IReadOnlyList<string> errorMessages = schemaBuilder.ErrorMessages;
        //等。
    }
}
相关推荐
△曉風殘月〆2 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風4 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_656974747 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo7 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo9 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发9 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
小乖兽技术9 小时前
C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
c++·c#·交互·ipc
幼儿园园霸柒柒10 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
平凡シンプル12 小时前
C# EF 使用
c#
丁德双13 小时前
winform 加载 office excel 插入QRCode图片如何设定位置
c#·excel