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)