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)
相关推荐
艾妮艾妮1 小时前
C语言常见3种排序
java·c语言·开发语言·c++·算法·c#·排序算法
小码编匠2 小时前
.NET 验证码生成神器基于 SkiaSharp 的高性能方案
后端·c#·.net
专注VB编程开发20年2 小时前
Aspose.words,Aspose.cells,vb.net,c#加载许可证,生成操作选择:嵌入的资源
c#·word·.net·vb.net
andy55202 小时前
.NET 使用 WMQ 连接Queue 发送 message 实例
xml·c#·wmq·c# 连接wmq·发送消息到wmq
破罐子不摔2 小时前
【C#使用S7.NET库读取和写入西门子PLC变量】
java·c#·.net
杰尼杰尼丶3 小时前
Winform MQTT客户端连接方式
c#·winform
weixin_307779134 小时前
C#实现HiveQL建表语句中特殊数据类型的包裹
开发语言·数据仓库·hive·c#
lixy5794 小时前
C# WPF 命令机制(关闭CanExecute自动触发,改手动)
c#·wpf
天地长久.4 小时前
C# N层架构和解耦
c#·解耦·多层架构
观无5 小时前
关于跨域与.NET的处理方案
c#