ArcGIS Pro SDK (九)几何 4 折线

news/2024/8/22 18:12:44 标签: arcgis, arcgis pro sdk, gis, c#

ArcGIS Pro SDK (九)几何 4 折线

文章目录

  • ArcGIS Pro SDK (九)几何 4 折线
    • 1 构造折线 - 从映射点的枚举
    • 2 获取折线的点
    • 3 获取折线的各个部分
    • 4 枚举折线的各个部分
    • 5 反转折线中点的顺序
    • 6 获取折线的段
    • 7 构建多部分折线
    • 8 折线的起点
    • 9 按角度构造
    • 10 按长度构造
      • 11 远距离分割折线

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

1 构造折线 - 从映射点的枚举

// 使用 builderEx 便捷方法或 builderEx 构造函数。
// 都不需要在 MCT 上运行

MapPoint startPt = MapPointBuilderEx.CreateMapPoint(1.0, 1.0);
MapPoint endPt = MapPointBuilderEx.CreateMapPoint(2.0, 1.0);

List<MapPoint> list = new List<MapPoint>();
list.Add(startPt);
list.Add(endPt);

Polyline polyline = PolylineBuilderEx.CreatePolyline(list, SpatialReferences.WGS84);

// 使用 AttributeFlags.None 因为列表中只有 2D 点
PolylineBuilderEx pb = new PolylineBuilderEx(list, AttributeFlags.None);
pb.SpatialReference = SpatialReferences.WGS84;
Polyline polyline2 = pb.ToGeometry();

// 使用 AttributeFlags.NoAttributes 因为列表中只有 2D 点
Polyline polyline4 = PolylineBuilderEx.CreatePolyline(list, AttributeFlags.None);

2 获取折线的点

// 获取点作为只读集合
ReadOnlyPointCollection pts = polyline.Points;
int numPts = polyline.PointCount;

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

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

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

// 或者 使用预分配的内存获取集合的子集作为 Coordinate2D

IList<Coordinate2D> coordinate2Ds = new List<Coordinate2D>(10);   // 分配一些空间
ICollection<Coordinate2D> subsetCoordinates2D = coordinate2Ds;    // 分配
pts.Copy2DCoordinatesToList(1, 2, ref subsetCoordinates2D);       // 从索引 1 复制 2 个元素到分配的列表
                                                                  // coordinate2Ds.Count = 2
                                                                  // 处理 coordinate2Ds

// 在不分配更多空间的情况下,获取不同的坐标集
pts.Copy2DCoordinatesToList(5, 9, ref subsetCoordinates2D);       // 从索引 5 复制 9 个元素到分配的列表
                                                                  // coordinate2Ds.Count = 9


// 或者 使用预分配的内存获取集合的子集作为 Coordinate3D

IList<Coordinate3D> coordinate3Ds = new List<Coordinate3D>(15);   // 分配一些空间
ICollection<Coordinate3D> subsetCoordinates3D = coordinate3Ds;    // 分配
pts.Copy3DCoordinatesToList(3, 5, ref subsetCoordinates3D);       // 从索引 3 复制 5 个元素到分配的列表
                                                                  // coordinate3Ds.Count = 5


// 或者 使用预分配的内存获取集合的子集作为 MapPoint

IList<MapPoint> mapPoints = new List<MapPoint>(7);   // 分配一些空间
ICollection<MapPoint> subsetMapPoint = mapPoints;    // 分配
pts.CopyPointsToList(1, 4, ref subsetMapPoint);      // 从索引 1 复制 4 个元素到分配的列表
                                                     // mapPoints.Count = 4

3 获取折线的各个部分

int numParts = polyline.PartCount;
// 获取部分作为只读集合
ReadOnlyPartCollection parts = polyline.Parts;

4 枚举折线的各个部分

ReadOnlyPartCollection polylineParts = polyline.Parts;

// 枚举段以获取长度
double len = 0;
IEnumerator<ReadOnlySegmentCollection> segments = polylineParts.GetEnumerator();
while (segments.MoveNext())
{
  ReadOnlySegmentCollection seg = segments.Current;
  foreach (Segment s in seg)
  {
    len += s.Length;

    // 可能根据段类型执行特定操作
    switch (s.SegmentType)
    {
      case SegmentType.Line:
        break;
      case SegmentType.Bezier:
        break;
      case SegmentType.EllipticArc:
        break;
    }
  }
}

// 或者使用 foreach 模式
foreach (var part in polyline.Parts)
{
  foreach (var segment in part)
  {
    len += segment.Length;

    // 可能根据段类型执行特定操作
    switch (segment.SegmentType)
    {
      case SegmentType.Line:
        break;
      case SegmentType.Bezier:
        break;
      case SegmentType.EllipticArc:
        break;
    }
  }
}

5 反转折线中点的顺序

var polylineBuilder = new PolylineBuilderEx(polyline);
polylineBuilder.ReverseOrientation();
Polyline reversedPolyline = polylineBuilder.ToGeometry();

6 获取折线的段

ICollection<Segment> collection = new List<Segment>();
polyline.GetAllSegments(ref collection);
int numSegments = collection.Count;    // = 10

IList<Segment> iList = collection as IList<Segment>;
for (int i = 0; i < numSegments; i++)
{
  // 处理 iList[i]
}

// 使用段构建另一条折线
Polyline polylineFromSegments = PolylineBuilderEx.CreatePolyline(collection);

7 构建多部分折线

List<MapPoint> firstPoints = new List<MapPoint>();
firstPoints.Add(MapPointBuilderEx.CreateMapPoint(1.0, 1.0));
firstPoints.Add(MapPointBuilderEx.CreateMapPoint(1.0, 2.0));
firstPoints.Add(MapPointBuilderEx.CreateMapPoint(2.0, 2.0));
firstPoints.Add(MapPointBuilderEx.CreateMapPoint(2.0, 1.0));

List<MapPoint> nextPoints = new List<MapPoint>();
nextPoints.Add(MapPointBuilderEx.CreateMapPoint(11.0, 1.0));
nextPoints.Add(MapPointBuilderEx.CreateMapPoint(11.0, 2.0));
nextPoints.Add(MapPointBuilderEx.CreateMapPoint(12.0, 2.0));
nextPoints.Add(MapPointBuilderEx.CreateMapPoint(12.0, 1.0));

// 使用 AttributeFlags.None 因为列表中有 2D 点
PolylineBuilderEx pBuilder = new PolylineBuilderEx(firstPoints, AttributeFlags.None);
pBuilder.AddPart(nextPoints);

Polyline polyline = pBuilder.ToGeometry();
// polyline 有 2 部分

pBuilder.RemovePart(0);
polyline = pBuilder.ToGeometry();
// polyline 有 1 部分

8 折线的起点

// 方法 1: 通过将折线转换为点集合来获取折线的起点,并获取第一个点

// sketchGeometry 是一个 Polyline
// 获取草图作为点集合
var pointCol = ((Multipart)sketchGeometry).Points;
// 获取线的起点
var firstPoint = pointCol[0];

// 方法 2: 将折线转换为线段集合,并获取第一条线段的 "StartPoint"
var polylineGeom = sketchGeometry as ArcGIS.Core.Geometry.Polyline;
var polyLineParts = polylineGeom.Parts;

ReadOnlySegmentCollection polylineSegments = polyLineParts.First();

// 获取第一个线段作为 LineSegment
var firsLineSegment = polylineSegments.First() as LineSegment;

// 现在获取起点
var startPoint = firsLineSegment.StartPoint;

9 按角度构造

MapPoint startPoint = MapPointBuilderEx.CreateMapPoint(0, 0);
double tangentDirection = Math.PI / 6;
ArcOrientation orientation = ArcOrientation.ArcCounterClockwise;
double startRadius = double.PositiveInfinity;
double endRadius = 0.2;
ClothoidCreateMethod createMethod = ClothoidCreateMethod.ByAngle;
double angle = Math.PI / 2;
CurveDensifyMethod densifyMethod = CurveDensifyMethod.ByLength;
double densifyParameter = 0.1;

Polyline polyline = PolylineBuilderEx.CreatePolyline(startPoint, tangentDirection, startRadius, endRadius, orientation, createMethod, angle, densifyMethod, densifyParameter, SpatialReferences.WGS84);

int numPoints = polyline.PointCount;
MapPoint queryPoint = polyline.Points[numPoints - 2];

MapPoint pointOnPath;
double radiusCalculated, tangentDirectionCalculated, lengthCalculated, angleCalculated;

PolylineBuilderEx.QueryClothoidParameters(queryPoint, startPoint, tangentDirection, startRadius, endRadius, orientation, createMethod, angle, out pointOnPath, out radiusCalculated, out tangentDirectionCalculated, out lengthCalculated, out angleCalculated, SpatialReferences.WGS84);

10 按长度构造

MapPoint startPoint = MapPointBuilderEx.CreateMapPoint(0, 0);
MapPoint queryPoint = MapPointBuilderEx.CreateMapPoint(3.8, 1);
double tangentDirection = 0;
ArcOrientation orientation = ArcOrientation.ArcCounterClockwise;
double startRadius = double.PositiveInfinity;
double endRadius = 1;
ClothoidCreateMethod createMethod = ClothoidCreateMethod.ByLength;
double curveLength = 10;
MapPoint pointOnPath;
double radiusCalculated, tangentDirectionCalculated, lengthCalculated, angleCalculated;


PolylineBuilderEx.QueryClothoidParameters(queryPoint, startPoint, tangentDirection, startRadius, endRadius, orientation, createMethod, curveLength, out pointOnPath, out radiusCalculated, out tangentDirectionCalculated, out lengthCalculated, out angleCalculated, SpatialReferences.WGS84);

pointOnPath = MapPointBuilderEx.CreateMapPoint(3.7652656620171379, 1.0332006103128575);
radiusCalculated = 2.4876382887687227;
tangentDirectionCalculated = 0.80797056423543978;
lengthCalculated = 4.0198770235802987;
angleCalculated = 0.80797056423544011;

queryPoint = MapPointBuilderEx.CreateMapPoint(1.85, 2.6);

PolylineBuilderEx.QueryClothoidParameters(queryPoint, startPoint, tangentDirection, startRadius, endRadius, orientation, createMethod, curveLength, out pointOnPath, out radiusCalculated, out tangentDirectionCalculated, out lengthCalculated, out angleCalculated, SpatialReferences.WGS84);

pointOnPath = MapPointBuilderEx.CreateMapPoint(1.8409964973501549, 2.6115979967308132);
radiusCalculated = 1;
tangentDirectionCalculated = -1.2831853071795867;
lengthCalculated = 10;
angleCalculated = 5;
 
tangentDirection = Math.PI / 4;
orientation = ArcOrientation.ArcClockwise;
startRadius = double.PositiveInfinity;
endRadius = 0.8;
createMethod = ClothoidCreateMethod.ByLength;
curveLength = 10;

Polyline polyline = PolylineBuilderEx.CreatePolyline(startPoint, tangentDirection, startRadius, endRadius, orientation, createMethod, curveLength, CurveDensifyMethod.ByLength, 0.5, SpatialReferences.WGS84);

11 远距离分割折线

// 创建点列表
MapPoint startPt = MapPointBuilderEx.CreateMapPoint(1.0, 1.0);
MapPoint endPt = MapPointBuilderEx.CreateMapPoint(2.0, 1.0);

List<MapPoint> list = new List<MapPoint>();
list.Add(startPt);
list.Add(endPt);

// BuilderEx 构造函数不需要在 MCT 上运行。

// 使用 PolylineBuilder,因为我们希望操作几何图形
// 使用 AttributeFlags.None 因为我们有 2D 点
PolylineBuilderEx polylineBuilder = new PolylineBuilderEx(list, AttributeFlags.None);
// 在 0.75 处分割
polylineBuilder.SplitAtDistance(0.75, false);
// 获取折线
Polyline p = polylineBuilder.ToGeometry();
// 折线 p 应该有 3 个点 (1,1), (1.75, 1), (2,1)

// 添加另一条路径
MapPoint p1 = MapPointBuilderEx.CreateMapPoint(4.0, 1.0);
MapPoint p2 = MapPointBuilderEx.CreateMapPoint(6.0, 1.0);
MapPoint p3 = MapPointBuilderEx.CreateMapPoint(7.0, 1.0);
List<MapPoint> pts = new List<MapPoint>();
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);

polylineBuilder.AddPart(pts);
p = polylineBuilder.ToGeometry();

// 折线 p 有 2 部分。每部分有 3 个点

// 分割第二条路径的一半 - 不创建新路径
polylineBuilder.SplitPartAtDistance(1, 0.5, true, false);

p = polylineBuilder.ToGeometry();

// 折线 p 仍然有 2 部分;但现在有 7 个点 

http://www.niftyadmin.cn/n/5556524.html

相关文章

[iOS]内存分区

[iOS]内存分区 文章目录 [iOS]内存分区五大分区栈区堆区全局区常量区代码区验证内存使用注意事项总结 函数栈堆栈溢出栈的作用 参考博客 在iOS中&#xff0c;内存主要分为栈区、堆区、全局区、常量区、代码区五大区域 还记得OC是C的超类 所以C的内存分区也是一样的 iOS系统中&a…

SAPUI5基础知识13 - 页面和面板(Page Panel)

1. 背景 在前面的博客中&#xff0c;我们主要专注于SAPUI5程序的结构设计&#xff0c;在本篇博客中&#xff0c;让我们改善一下SAPUI5程序的外观。我们将使用sap.m库中的sap.m.Page控件和sap.m.Panel控件来让应用程序的屏幕元素更好地展示。 2. 控件学习 2.1 sap.m.App (应用…

飞塔防火墙和paloalto防火墙构建ipsec

总部一台飞塔防火墙FG60F 版本v7.2.6 build1575 (Feature) 分支一台paloalto防火墙版本8.0.7 构建站点到站点IPSECVPN&#xff0c;目的是总部的网段192.168.4.0/23和分支192.168.56.0/24可以互通 一、配置fortigate防火墙 1.防火墙配置上网功能 1.1配置fortigate的wan1接口IP为…

Binder框架(二) binder初始化

注意&#xff1a;binder的初始化总体调用链&#xff1a; device_initcall(binder_init); static int __init binder_init(void) static int __init init_binder_device(const char *name) &#xff08;代码位置&#xff1a;kernel/drivers/android/binder.c&#xff09; 1、…

Java算法-力扣leetcode-3. 无重复字符的最长子串

3. 无重复字符的最长子串 给定一个字符串 s &#xff0c;请你找出其中不含有重复字符的 **最长 ** 子串 ****的长度。 示例 1: 输入: s "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc"&#xff0c;所以其长度为 3。示例 2: 输入: s &…

【C++题解】1168. 歌唱比赛评分

问题&#xff1a;1168. 歌唱比赛评分 类型&#xff1a;数组找数 题目描述&#xff1a; 四&#xff08;1&#xff09; 班要举行一次歌唱比赛&#xff0c;以选拔更好的苗子参加校的歌唱比赛。评分办法如下&#xff1a;设 N 个评委&#xff0c;打 N 个分数&#xff08; 0≤每个分…

ES6 Symbol (十三)

ES5的对象属性名都是字符串&#xff0c;这容易造成属性名的冲突。比如&#xff0c;你使用了一个他人提供的对象&#xff0c;但又想为这个对象添加新的方法&#xff08;mixin 模式&#xff09;&#xff0c;新方法的名字就有可能与现有方法产生冲突。如果有一种机制&#xff0c;保…

CH552G使用IAP下载

常见下载中的方式ISP&#xff0c;IAP&#xff0c;ICP 参考&#xff0c;CH552G中文手册&#xff0c;参考1 ISP&#xff1a;In System Programing&#xff0c;在系统编程。是常见的&#xff0c;使用软件&#xff0c;先将某个引脚&#xff08;例如boot&#xff09;连接到合适的电…