在ArcGIS Pro SDK中,SelectionSet类用于处理地图或图层中的【选定要素集合】。
一般情况下,当你使用【选择】工具进行要素的选择时,就会得到【Selection】的内容。
SelectionSet类提供了许多成员、属性和方法,用于管理和操作选择集。
一、SelectionSet的成员和方法
SelectionSet的3个主要成员:Count、isEmpty和item。
1、Count
获取选择集中记录的总数。返回的是一个int值。
2、isEmpty
判断当前选择集是否为空。返回的是一个bool值。
3、item
获取成员对象 ID的列表。返回的是一组long值。
SelectionSet的3个主要方法:Contains、FromDictionary和ToDictionary。
1、Contains
确定指定映射成员中的记录是否在选择集中。
2、FromDictionary
通过字典来创建一个选择集对象。
3、ToDictionary
从选择集创建映射成员及其相应对象 ID 的字典。
二、代码示例
首先要获取当前选择的要素(sel_poly为Geometry):
var selSet = MapView.Active.GetFeatures(sel_poly);
也可以不指定Geometry:
var selSet = MapView.Active.Map.GetSelection();
通过ToDictionary方法转换为字典【Dictionary】:
var selSetDict = selSet.ToDictionary();
如果选择内容比较多而且复杂,而你只想要某种类型的要素,可以作一个筛选:
var selSetDict = selSet.ToDictionary<FeatureLayer>();
视图缩放到选择要素集的范围:
MapView.Active.ZoomTo(MapView.Active.GetFeatures(sel_poly));
平移到选择要素集的范围:
var editOp = new EditOperation() { };
editOp.Move(MapView.Active.SelectFeatures(sel_poly), 500.0, 500.0);
editOp.Execute();
旋转:
var editOp = new EditOperation() { };
editOp.Rotate(MapView.Active.SelectFeatures(sel_poly), origin, 35.0);
editOp.Execute();
通过ToDictionary()和LINQ查询,来获取选择集中的第一个要素:
var selSet = MapView.Active.GetFeatures(sel_poly);
var selSetDict = selSet.ToDictionary();
var insp = new Inspector();
insp.Load(selSetDict.Keys.First(), selSetDict.Values.First());
var selGeom = insp.Shape;
通过SelectFeaturesEx方法获取指定图层的对象ID列表:
var sname = "Points of Interest";
var selSet = MapView.Active.SelectFeaturesEx(sel_poly);
var oids1 = selSet.ToDictionary().Where(kvp => kvp.Key.Name == sname).First().Value;
从对象ID列表中创建选择集:
var addToSelection = new Dictionary<MapMember, List<long>>();
addToSelection.Add(featureLayer, new List<long> { 1506, 2696, 2246, 1647, 948 });
var sset = SelectionSet.FromDictionary(addToSelection);
获取和sel_poly相交的要素类的对象ID:
var selSet = MapView.Active.GetFeatures(sel_poly);
if (selSet.Contains(featureLayer))
{
var oids = selSet[featureLayer];
}