ArcGIS Pro SDK (九)几何 5 多边形
文章目录
- [ArcGIS Pro SDK (九)几何 5 多边形](#ArcGIS Pro SDK (九)几何 5 多边形)
-
- [1 构造多边形 - 从映射点的枚举](#1 构造多边形 - 从映射点的枚举)
- [2 构造多边形 - 从包络](#2 构造多边形 - 从包络)
- [3 获取多边形的点](#3 获取多边形的点)
- [4 获取多边形的各个部分](#4 获取多边形的各个部分)
- [5 枚举多边形的各个部分](#5 枚举多边形的各个部分)
- [6 获取多边形的线段](#6 获取多边形的线段)
- [7 构建圆环多边形](#7 构建圆环多边形)
- [8 创建 N 侧正多边形](#8 创建 N 侧正多边形)
- [9 获取多边形的外环](#9 获取多边形的外环)
环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0
1 构造多边形 - 从映射点的枚举
csharp
// 使用 builderEx 便捷方法或使用 builderEx 构造函数。
MapPoint pt1 = MapPointBuilderEx.CreateMapPoint(1.0, 1.0);
MapPoint pt2 = MapPointBuilderEx.CreateMapPoint(1.0, 2.0);
MapPoint pt3 = MapPointBuilderEx.CreateMapPoint(2.0, 2.0);
MapPoint pt4 = MapPointBuilderEx.CreateMapPoint(2.0, 1.0);
List<MapPoint> list = new List<MapPoint>() { pt1, pt2, pt3, pt4 };
Polygon polygon = PolygonBuilderEx.CreatePolygon(list, SpatialReferences.WGS84);
// polygon.HasZ 将为 false - 它由列表中点的 HasZ 标志决定
// 或者特别使用 AttributeFlags.NoAttributes
polygon = PolygonBuilderEx.CreatePolygon(list, AttributeFlags.None);
// 使用 AttributeFlags.None 因为我们有 2D 点
PolygonBuilderEx polygonBuilder = new PolygonBuilderEx(list, AttributeFlags.None);
polygonBuilder.SpatialReference = SpatialReferences.WGS84;
polygon = polygonBuilder.ToGeometry();
2 构造多边形 - 从包络
csharp
// 使用 builderEx 便捷方法或使用 builderEx 构造函数。
MapPoint minPt = MapPointBuilderEx.CreateMapPoint(1.0, 1.0);
MapPoint maxPt = MapPointBuilderEx.CreateMapPoint(2.0, 2.0);
// 创建包络
Envelope env = EnvelopeBuilderEx.CreateEnvelope(minPt, maxPt);
Polygon polygonFromEnv = PolygonBuilderEx.CreatePolygon(env);
PolygonBuilderEx polygonBuilderEx = new PolygonBuilderEx(env);
polygonBuilderEx.SpatialReference = SpatialReferences.WGS84;
polygonFromEnv = polygonBuilderEx.ToGeometry() as Polygon;
3 获取多边形的点
csharp
// 获取点作为只读集合
ReadOnlyPointCollection pts = polygon.Points;
// 获取点的枚举
IEnumerator<MapPoint> enumPts = polygon.Points.GetEnumerator();
// 获取点坐标作为只读的 Coordinate2D 列表
IReadOnlyList<Coordinate2D> coordinates = polygon.Copy2DCoordinatesToList();
// 获取点坐标作为只读的 Coordinate3D 列表
IReadOnlyList<Coordinate3D> coordinates3D = polygon.Copy3DCoordinatesToList();
4 获取多边形的各个部分
csharp
// 获取部分作为只读集合
ReadOnlyPartCollection parts = polygon.Parts;
5 枚举多边形的各个部分
csharp
int numSegments = 0;
IEnumerator<ReadOnlySegmentCollection> segments = polygon.Parts.GetEnumerator();
while (segments.MoveNext())
{
ReadOnlySegmentCollection seg = segments.Current;
numSegments += seg.Count;
foreach (Segment s in seg)
{
// 处理线段
}
}
6 获取多边形的线段
csharp
List<Segment> segmentList = new List<Segment>(30);
ICollection<Segment> collection = segmentList;
polygon.GetAllSegments(ref collection);
// segmentList.Count = 4
// segmentList.Capacity = 30
// 使用线段构建另一个多边形
Polygon polygonFromSegments = PolygonBuilderEx.CreatePolygon(collection);
7 构建圆环多边形
csharp
List<Coordinate2D> outerCoordinates = new List<Coordinate2D>();
outerCoordinates.Add(new Coordinate2D(10.0, 10.0));
outerCoordinates.Add(new Coordinate2D(10.0, 20.0));
outerCoordinates.Add(new Coordinate2D(20.0, 20.0));
outerCoordinates.Add(new Coordinate2D(20.0, 10.0));
// 定义内部多边形为逆时针方向
List<Coordinate2D> innerCoordinates = new List<Coordinate2D>();
innerCoordinates.Add(new Coordinate2D(13.0, 13.0));
innerCoordinates.Add(new Coordinate2D(17.0, 13.0));
innerCoordinates.Add(new Coordinate2D(17.0, 17.0));
innerCoordinates.Add(new Coordinate2D(13.0, 17.0));
PolygonBuilderEx pbEx = new PolygonBuilderEx(outerCoordinates);
Polygon donutEx = pbEx.ToGeometry() as Polygon;
double areaEx = donutEx.Area; // 面积 = 100
pbEx.AddPart(innerCoordinates);
donutEx = pbEx.ToGeometry() as Polygon;
areaEx = donutEx.Area; // 面积 = 84.0
areaEx = GeometryEngine.Instance.Area(donutEx); // 面积 = 84.0
8 创建 N 侧正多边形
csharp
// <summary>
// 创建一个 N 边正多边形。 正多边形是等角(所有角度相等)
// 和等边(所有边长相等)的多边形。 请参阅 https://en.wikipedia.org/wiki/Regular_polygon
// </summary>
// <param name="numSides">多边形的边数。</param>
// <param name="center">多边形的中心。</param>
// <param name="radius">从多边形中心到顶点的距离。</param>
// <param name="rotation">多边形起点的旋转角度(以弧度为单位)。起点将
// 从正 x 轴逆时针旋转。</param>
// <returns>N 边正多边形。</returns>
// <exception cref="ArgumentException">边数小于 3。</exception>
public Polygon CreateRegularPolygon(int numSides, Coordinate2D center, double radius, double rotation)
{
if (numSides < 3)
throw new ArgumentException();
Coordinate2D[] coords = new Coordinate2D[numSides + 1];
double centerX = center.X;
double centerY = center.Y;
double x = radius * Math.Cos(rotation) + centerX;
double y = radius * Math.Sin(rotation) + centerY;
Coordinate2D start = new Coordinate2D(x, y);
coords[0] = start;
double da = 2 * Math.PI / numSides;
for (int i = 1; i < numSides; i++)
{
x = radius * Math.Cos(i * da + rotation) + centerX;
y = radius * Math.Sin(i * da + rotation) + centerY;
coords[i] = new Coordinate2D(x, y);
}
coords[numSides] = start;
return PolygonBuilderEx.CreatePolygon(coords);
}
9 获取多边形的外环
csharp
public void GetExteriorRings(Polygon inputPolygon)
{
if (inputPolygon == null || inputPolygon.IsEmpty)
return;
// 多边形部分数
int partCount = inputPolygon.PartCount;
// 多边形外环数
int numExtRings = inputPolygon.ExteriorRingCount;
// 获取多边形的外环集
IList<Polygon> extRings = inputPolygon.GetExteriorRings();
// 测试每个部分是否为"外环"
for (int idx = 0; idx < partCount; idx++)
{
bool isExteriorRing = inputPolygon.IsExteriorRing(idx);
var ring = inputPolygon.GetExteriorRing(idx);
}
}