ArcGIS Pro SDK (九)几何 5 多边形

ArcGIS Pro SDK (九)几何 5 多边形

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

1 构造多边形 - 从映射点的枚举

// 使用 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 构造多边形 - 从包络

// 使用 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 获取多边形的点

// 获取点作为只读集合
ReadOnlyPointCollection pts = polygon.Points;

// 获取点的枚举
IEnumerator<MapPoint> enumPts = polygon.Points.GetEnumerator();

// 获取点坐标作为只读的 Coordinate2D 列表
IReadOnlyList<Coordinate2D> coordinates = polygon.Copy2DCoordinatesToList();

// 获取点坐标作为只读的 Coordinate3D 列表
IReadOnlyList<Coordinate3D> coordinates3D = polygon.Copy3DCoordinatesToList();

4 获取多边形的各个部分

// 获取部分作为只读集合
ReadOnlyPartCollection parts = polygon.Parts;

5 枚举多边形的各个部分

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 获取多边形的线段

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 构建圆环多边形

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 侧正多边形

// <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 获取多边形的外环

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);
  }
}

相关推荐

  1. ArcGIS Pro SDK (几何 5 多边形

    2024-07-19 08:14:02       21 阅读
  2. ArcGIS Pro SDK (几何 11 几何

    2024-07-19 08:14:02       18 阅读
  3. ArcGIS Pro SDK (几何 2 坐标

    2024-07-19 08:14:02       21 阅读
  4. ArcGIS Pro SDK (几何 3 点

    2024-07-19 08:14:02       20 阅读
  5. ArcGIS Pro SDK (几何 4 折线

    2024-07-19 08:14:02       19 阅读
  6. ArcGIS Pro SDK (几何 10 弧

    2024-07-19 08:14:02       18 阅读
  7. ArcGIS Pro SDK (几何 8 线段

    2024-07-19 08:14:02       20 阅读
  8. ArcGIS Pro SDK (几何 12 多面体

    2024-07-19 08:14:02       16 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 08:14:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 08:14:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 08:14:02       57 阅读
  4. Python语言-面向对象

    2024-07-19 08:14:02       68 阅读

热门阅读

  1. SpringBoot集成EasyExcel实现模板写入多个sheet导出

    2024-07-19 08:14:02       21 阅读
  2. python中excel的读取和写入

    2024-07-19 08:14:02       20 阅读
  3. Python 3 CGI编程

    2024-07-19 08:14:02       19 阅读
  4. 为什么 HashMap 的容量是 2 的整次幂?

    2024-07-19 08:14:02       15 阅读
  5. C++编程逻辑讲解step by step:利用文档类处理数据

    2024-07-19 08:14:02       20 阅读
  6. 【Oracle】Oracle中的LISTAGG函数

    2024-07-19 08:14:02       18 阅读
  7. new和malloc

    2024-07-19 08:14:02       21 阅读
  8. Redis 地理位置 GEO 模块

    2024-07-19 08:14:02       21 阅读
  9. 一文理解ThreadPoolExecutor线程池以及运行时间

    2024-07-19 08:14:02       20 阅读
  10. AccessibilityEvent常用事件

    2024-07-19 08:14:02       19 阅读