ArcGIS Pro SDK (九)几何 11 几何包

ArcGIS Pro SDK (九)几何 11 几何包

文章目录

  • [ArcGIS Pro SDK (九)几何 11 几何包](#ArcGIS Pro SDK (九)几何 11 几何包)
    • [1 构造几何包](#1 构造几何包)
    • [2 构造几何包 - 从几何枚举](#2 构造几何包 - 从几何枚举)
    • [3 构造几何包 - 来自 JSON、Xml](#3 构造几何包 - 来自 JSON、Xml)
    • [4 构造几何包 - 添加或插入几何枚举](#4 构造几何包 - 添加或插入几何枚举)

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

1 构造几何包

csharp 复制代码
MapPoint point = MapPointBuilderEx.CreateMapPoint(1, 2, SpatialReferences.WebMercator);

List<Coordinate2D> coords2D = new List<Coordinate2D>()
{
  new Coordinate2D(0, 0),
  new Coordinate2D(0, 1),
  new Coordinate2D(1, 1),
  new Coordinate2D(1, 0)
};

Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(coords2D, SpatialReferences.WGS84);
Polyline polyline = PolylineBuilderEx.CreatePolyline(coords2D, SpatialReferences.WebMercator);

GeometryBagBuilderEx builder = new GeometryBagBuilderEx(SpatialReferences.WGS84);

GeometryBag emptyBag = builder.ToGeometry();
// emptyBag.IsEmpty = true 空几何包

builder.AddGeometry(point);
// builder.GeometryCount = 1 几何数量为 1

GeometryBag geometryBag = builder.ToGeometry();
// geometryBag.PartCount = 1 部分数量为 1
// geometryBag.PointCount = 1 点数量为 1
// geometryBag.IsEmpty = false 几何包不为空

IReadOnlyList<Geometry> geometries = geometryBag.Geometries;
// geometries.Count = 1 几何数量为 1
// geometries[0] 是一个 MapPoint,空间参考为 WGS84

bool isEqual = geometryBag.IsEqual(emptyBag);   // isEqual = false 不相等

builder.InsertGeometry(0, multipoint);
geometryBag = builder.ToGeometry();
// geometryBag.PartCount = 2 部分数量为 2

geometries = geometryBag.Geometries;
// geometries.Count = 2 几何数量为 2
// geometries[0] 是 Multipoint
// geometries[1] 是 MapPoint

builder.AddGeometry(polyline);
builder.RemoveGeometry(1);
geometryBag = builder.ToGeometry();
// geometryBag.PartCount = 2 部分数量为 2

geometries = geometryBag.Geometries;
// geometries.Count = 2 几何数量为 2
// geometries[0] 是 Multipoint
// geometries[1] 是 Polyline          

2 构造几何包 - 从几何枚举

csharp 复制代码
// 使用 builder 的便捷方法或使用 builder 构造函数。

MapPoint point = MapPointBuilderEx.CreateMapPoint(10, 20);
List<Coordinate2D> coords = new List<Coordinate2D>() { new Coordinate2D(50, 60), new Coordinate2D(-120, -70), new Coordinate2D(40, 60) };
Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(coords, SpatialReferences.WebMercator);
Polyline polyline = PolylineBuilderEx.CreatePolyline(coords);

string json = "{\"rings\":[[[0,0],[0,1],[1,1],[1,0],[0,0]],[[3,0],[3,1],[4,1],[4,0],[3,0]]]}";
Polygon polygon = PolygonBuilderEx.FromJson(json);

var geometries = new List<Geometry>() { point, multipoint, polyline, polygon };

// Builder 的便捷方法不需要在 MCT 上运行。
// 在 2.x 版本中 - GeometryBag bag = GeometryBagBuilder.CreateGeometryBag(geometries, SpatialReferences.WGS84);
var bag = GeometryBagBuilderEx.CreateGeometryBag(geometries, SpatialReferences.WGS84);

// 在 2.x 版本中 - using (var builder = new GeometryBagBuilder(geometries, SpatialReferences.WGS84)) 
var builder = new GeometryBagBuilderEx(geometries, SpatialReferences.WGS84);
// 使用生成器执行操作
bag = builder.ToGeometry();

3 构造几何包 - 来自 JSON、Xml

csharp 复制代码
const string jsonString = "{\"geometries\":[{\"x\":1,\"y\":2},{\"rings\":[[[0,0],[0,4],[3,4],[3,0],[0,0]]]}],\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}";
// 在 2.x 版本中 - GeometryBag geometryBag = GeometryBagBuilder.FromJson(jsonString);
var geometryBag = GeometryBagBuilderEx.FromJson(jsonString);

string xml = geometryBag.ToXml();
// 在 2.x 版本中 - GeometryBag xmlString = GeometryBagBuilder.FromXML(xml);
var xmlString = GeometryBagBuilderEx.FromXml(xml);

4 构造几何包 - 添加或插入几何枚举

csharp 复制代码
MapPoint point = MapPointBuilderEx.CreateMapPoint(10, 20);
List<Coordinate2D> coords = new List<Coordinate2D>() { new Coordinate2D(50, 60), new Coordinate2D(-120, -70), new Coordinate2D(40, 60) };
Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(coords, SpatialReferences.WebMercator);
Polyline polyline = PolylineBuilderEx.CreatePolyline(coords);

string json = "{\"rings\":[[[0,0],[0,1],[1,1],[1,0],[0,0]],[[3,0],[3,1],[4,1],[4,0],[3,0]]]}";
Polygon polygon = PolygonBuilderEx.FromJson(json);

var geometries = new List<Geometry>() { point, multipoint, polyline, polygon };

// 在 2.x 版本中 - using (var builder = new GeometryBagBuilder(SpatialReferences.WGS84))
var builder = new GeometryBagBuilderEx(SpatialReferences.WGS84);
builder.AddGeometries(geometries);

GeometryBag geomBag = builder.ToGeometry();
// geomBag.PartCount == 4    (point, multipoint, polyline, polygon)

geometries = new List<Geometry>() { point, polyline };
builder.InsertGeometries(1, geometries);
// builder.GeometryCount == 6
geomBag = builder.ToGeometry();
// geomBag.PartCount == 6    (point, point, polyline, multipoint, polyline, polygon)
相关推荐
△曉風殘月〆2 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風4 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_656974747 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo7 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo9 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发9 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
小乖兽技术10 小时前
C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
c++·c#·交互·ipc
幼儿园园霸柒柒10 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
平凡シンプル12 小时前
C# EF 使用
c#
丁德双13 小时前
winform 加载 office excel 插入QRCode图片如何设定位置
c#·excel