【ArcGIS Pro二次开发】(59):Editing(编辑)模块

ArcGIS Pro SDK中的"Editing"(编辑)模块提供了一系列API和工具,允许开发人员在ArcGIS Pro中实现各种地图数据编辑操作,从简单的要素绘制到复杂的数据集编辑。

下面列举一些常用编辑工具的实现方法。


1、获取所选要素的属性及赋值

// 获取当前所选择的要素
var selectedFeatures = MapView.Active.Map.GetSelection();
// 获取所选要素中的第一个(取单个要素)
var firstSelectionSet = selectedFeatures.ToDictionary().First();
// 创建【inspector】实例
var inspector = new Inspector();
// 将选定要素加载到【inspector】实例
inspector.LoadAsync(firstSelectionSet.Key, firstSelectionSet.Value);
// 获取要素属性或字段值
string xzmc = inspector["XZMC"].ToString();
var myGeometry = inspector.Shape;
// 给字段赋值
inspector["XZMC"] = "清凉镇";
// 执行编辑操作
inspector.ApplyAsync();

2、通过【inspector】获取字段属性

// 获取所选要素
var firstFeatureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType().FirstOrDefault();
// 创建【inspector】实例
var inspector = new Inspector();
// 将选定要素加载到【inspector】实例
inspector.LoadSchema(firstFeatureLayer);

// 查看属性
foreach (var attribute in inspector)
{
    var fldName = attribute.FieldName;  // 字段名称
    var fldAlias = attribute.FieldAlias;  // 字段别名
    var fldType = attribute.FieldType;  // 字段类型
    int idxFld = attribute.FieldIndex;  // 字段序号
    var fld = attribute.GetField();  // 字段
    var isNullable = attribute.IsNullable;  // 是否可为空
    var isEditable = attribute.IsEditable;  // 是否可编辑
    var isVisible = attribute.IsVisible;  // 是否可见
    var isSystemField = attribute.IsSystemField;  // 是否系统字段
    var isGeometryField = attribute.IsGeometryField;  // 是否几何属性字段
    var fldLength = attribute.Length;  // 字段长度
}

3、裁剪面

// 创建编辑器
var clipFeatures = new EditOperation();
clipFeatures.Name = "Clip Features";
// 执行
clipFeatures.Clip(featureLayer, oid, clipPoly, ClipMode.PreserveArea);
if (!clipFeatures.IsEmpty)
{
    var result = clipFeatures.Execute();
}

4、用线分割面

// 获取分割线
var select = MapView.Active.SelectFeatures(clipPoly);
// 创建编辑器
var cutFeatures = new EditOperation();
cutFeatures.Name = "Cut Features";

// 执行(要素类通过oid选择出要素)
cutFeatures.Split(featureLayer, oid, cutLine);
// 执行(要素则直接使用)
cutFeatures.Split(sset, cutLine);

if (!cutFeatures.IsEmpty)
{
    var result = cutFeatures.Execute();
}

5、对所选的线要素进行平行复制

// 找到图层,作模板用
var roadsLayer = MapView.Active.Map.FindLayers("Roads").FirstOrDefault();

// 建立【平行复制生成器】,并设置参数
var parOffsetBuilder = new ParallelOffset.Builder()
{
    Selection = MapView.Active.Map.GetSelection(),     // 所选要素
    Template = roadsLayer.GetTemplate("Freeway"),     // 模板(可以不选)
    Distance = 200,     // 偏移距离
    Side = ParallelOffset.SideType.Both,         // 偏移方式(左,右,两侧)
    Corner = ParallelOffset.CornerType.Mitered,  // 拐角处理方式(圆角,斜接角,斜面角)
    Iterations = 1,            // 重复偏移的次数
    AlignConnected = false,              // 是否对齐连接线的方向
    CopyToSeparateFeatures = false,          // 是否复制到独立要素
    RemoveSelfIntersectingLoops = true          // 是否移除自相交环
};

// 创建编辑器并执行
var parallelOp = new EditOperation();
parallelOp.Create(parOffsetBuilder);
if (!parallelOp.IsEmpty)
{
    var result = parallelOp.Execute(); 
}

6、删除要素

var deleteFeatures = new EditOperation();
deleteFeatures.Name = "Delete Features";
// 删除表中的某一行
var table = MapView.Active.Map.StandaloneTables[0];
deleteFeatures.Delete(table, oid);

// 删除所选要素
var selection = MapView.Active.SelectFeatures(polygon);
deleteFeatures.Delete(selection);

if (!deleteFeatures.IsEmpty)
{
    var result = deleteFeatures.Execute();
}

7、全属性复制一个要素并移位

// 创建编辑器
var duplicateFeatures = new EditOperation();
duplicateFeatures.Name = "Duplicate Features";
// 创建【inspector】实例
var insp2 = new Inspector();
// 加载
insp2.Load(featureLayer, oid);
// 获取几何
var geom = insp2["SHAPE"] as Geometry;
// 复制要素
var rtoken = duplicateFeatures.Create(insp2.MapMember, insp2.ToDictionary(a => a.FieldName, a => a.CurrentValue));
if (!duplicateFeatures.IsEmpty)
{
    if (duplicateFeatures.Execute())
    {
        // 移动位置
        var modifyOp = duplicateFeatures.CreateChainedOperation();
        modifyOp.Modify(featureLayer, (long)rtoken.ObjectID, GeometryEngine.Instance.Move(geom, 100.0, 100.0));
        if (!modifyOp.IsEmpty)
        {
            var result = modifyOp.Execute();
        }
    }
}

8、炸开多部件

var explodeFeatures = new EditOperation();
explodeFeatures.Name = "Explode Features";
// 执行
explodeFeatures.Explode(featureLayer, new List() { oid }, true);
if (!explodeFeatures.IsEmpty)
{
    var result = explodeFeatures.Execute(); 
}

9、合并要素

var mergeFeatures = new EditOperation();
mergeFeatures.Name = "Merge Features";

// 创建【inspector】实例
var inspector = new Inspector();
// 加载
inspector.Load(featureLayer, oid);
// 执行
mergeFeatures.Merge(featureLayer, new List() { 5, 6, 7 }, inspector);

if (!mergeFeatures.IsEmpty)
{
    var result = mergeFeatures.Execute(); 
}

10、更新单个要素

var modifyFeature = new EditOperation();
modifyFeature.Name = "Modify a feature";

// 创建【inspector】实例
var modifyInspector = new Inspector();
// 加载
modifyInspector.Load(featureLayer, oid);

// 更改属性【包括几何属性】
modifyInspector["SHAPE"] = polygon;
modifyInspector["NAME"] = "Updated name";
// 更新
modifyFeature.Modify(modifyInspector);

// 涉及更改几何要素的情况
var featureAttributes = new Dictionary();
featureAttributes["NAME"] = "Updated name";// 更改属性
modifyFeature.Modify(featureLayer, oid, polygon, featureAttributes);

if (!modifyFeature.IsEmpty)
{
    var result = modifyFeature.Execute(); 
}

11、更新多个要素

// 通过筛选获取要更新的要素
var queryFilter = new QueryFilter();
queryFilter.WhereClause = "OBJECTID < 1000000";
// 获取筛选结果的oid
var oidSet = new List();
using (var rc = featureLayer.Search(queryFilter))
{
    while (rc.MoveNext())
    {
        using (var record = rc.Current)
        {
            oidSet.Add(record.GetObjectID());
        }
    }
}

// 创建编辑器
var modifyFeatures = new EditOperation();
modifyFeatures.Name = "Modify features";
modifyFeatures.ShowProgressor = true;
// 创建【inspector】实例
var muultipleFeaturesInsp = new Inspector();
muultipleFeaturesInsp.Load(featureLayer, oidSet);
muultipleFeaturesInsp["MOMC"] = 24;
// 更新
modifyFeatures.Modify(muultipleFeaturesInsp);
if (!modifyFeatures.IsEmpty)
{
    var result = modifyFeatures.ExecuteAsync();
}

12、筛选要素并更新

// 筛选
var filter = new ArcGIS.Core.Data.QueryFilter();
filter.WhereClause = "CONTRACTOR = 'KCGM'";
// 获取所选的oid
var oids = new List();
using (var rc = disLayer.Search(filter))
{
    while (rc.MoveNext())
    {
        using (var record = rc.Current)
        {
            oidSet.Add(record.GetObjectID());
        }
    }
}

var modifyOp = new ArcGIS.Desktop.Editing.EditOperation();
modifyOp.Name = "Update date";

// 执行
var dateInsp = new ArcGIS.Desktop.Editing.Attributes.Inspector();
dateInsp.Load(disLayer, oids);
dateInsp["InspDate"] = "9/21/2013";

modifyOp.Modify(insp);
if (!modifyOp.IsEmpty)
{
    var result = modifyOp.Execute();
}

你可能感兴趣的:(ArcGIS,Pro,SDK,ArcGIS,arcgis,二次开发,c#,arcgis,pro,sdk,Editing)