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 小时前
C# WPF开发
microsoft·c#·wpf
GIS工具-gistools20213 小时前
如何在 QGIS 中打开 Esri 文件地理数据库(GDB)
gis·qgis
666和7776 小时前
C#的单元测试
开发语言·单元测试·c#
小码编匠7 小时前
WPF 星空效果:创建逼真的宇宙背景
后端·c#·.net
向宇it9 小时前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
yngsqq10 小时前
一键打断线(根据相交点打断)——CAD c# 二次开发
windows·microsoft·c#
TENET信条11 小时前
day53 第十一章:图论part04
开发语言·c#·图论
anlog12 小时前
C#在自定义事件里传递数据
开发语言·c#·自定义事件
向宇it14 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
仰望大佬00714 小时前
Avalonia实例实战五:Carousel自动轮播图
数据库·microsoft·c#