Bootstrap

ArcGIS Pro SDK (九)几何 14 检索与导入、导出 - 副本

ArcGIS Pro SDK (九)几何 14 检索与导入、导出

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

1 从地理数据库检索几何

// 方法需要在MCT上运行
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
    () =>
    {
        try
        {
            // 打开一个gdb
            using (ArcGIS.Core.Data.Geodatabase gdb = 
                   new ArcGIS.Core.Data.Geodatabase(
                       new FileGeodatabaseConnectionPath(new Uri(@"c:\Temp\MyDatabase.gdb"))))
            {
                // 打开一个要素类
                using (ArcGIS.Core.Data.FeatureClass featureClass = 
                       gdb.OpenDataset<ArcGIS.Core.Data.FeatureClass>("Polygon"))
                {

                    ArcGIS.Core.Data.QueryFilter filter = 
                        new ArcGIS.Core.Data.QueryFilter()
                    {
                        WhereClause = "OBJECTID = 6"
                    };

                    // 获取行
                    using (ArcGIS.Core.Data.RowCursor rowCursor = 
                           featureClass.Search(filter, false))
                    {
                        while (rowCursor.MoveNext())
                        {
                            using (var row = rowCursor.Current)
                            {
                                long oid = row.GetObjectID();

                                // 从行中获取形状
                                ArcGIS.Core.Data.Feature feature = row as ArcGIS.Core.Data.Feature;
                                Polygon polygon = feature.GetShape() as Polygon;

                                // 在这里做一些处理
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // 错误 - 适当处理
        }
    });

2 将几何图形导入和导出为已知文本

// 创建一个带有 z 和 m 的点
MapPoint point = MapPointBuilderEx.CreateMapPoint(
    100, 200, 300, 400, SpatialReferences.WebMercator);

// 设置标志
WktExportFlags wktExportFlags = WktExportFlags.WktExportDefaults;
WktImportFlags wktImportFlags = WktImportFlags.WktImportDefaults;

// 导出和导入
string wktString = GeometryEngine.Instance.ExportToWKT(wktExportFlags, point);
MapPoint importPoint = GeometryEngine.Instance.ImportFromWKT(
    wktImportFlags, wktString, SpatialReferences.WebMercator) as MapPoint;

double x = importPoint.X;       // x = 100
double y = importPoint.Y;       // y = 200
bool hasZ = importPoint.HasZ;   // hasZ = true
double z = importPoint.Z;       // z = 300
bool hasM = importPoint.HasM;   // hasM = true
double m = importPoint.M;       // m = 400

// 不带 z 的导出
WktExportFlags exportFlagsNoZ = WktExportFlags.WktExportStripZs;
wktString = GeometryEngine.Instance.ExportToWKT(exportFlagsNoZ, point);
importPoint = GeometryEngine.Instance.ImportFromWKT(
    wktImportFlags, wktString, SpatialReferences.WebMercator) as MapPoint;

x = importPoint.X;        // x = 100
y = importPoint.Y;        // y = 200
hasZ = importPoint.HasZ;  // hasZ = false
z = importPoint.Z;        // z = 0
hasM = importPoint.HasM;  // hasM = true
m = importPoint.M;        // m = 400

// 不带 m 的导出
WktExportFlags exportFlagsNoM = WktExportFlags.WktExportStripMs;
wktString = GeometryEngine.Instance.ExportToWKT(exportFlagsNoM, point);
importPoint = GeometryEngine.Instance.ImportFromWKT(
    wktImportFlags, wktString, SpatialReferences.WebMercator) as MapPoint;

x = importPoint.X;        // x = 100
y = importPoint.Y;        // y = 200
hasZ = importPoint.HasZ;  // hasZ = true
z = importPoint.Z;        // z = 300
hasM = importPoint.HasM;  // hasM = false
m = importPoint.M;        // m = Nan

// 不带 z 和 m 的导出
wktString = GeometryEngine.Instance.ExportToWKT(
    exportFlagsNoZ | exportFlagsNoM, point);
importPoint = GeometryEngine.Instance.ImportFromWKT(
    wktImportFlags, wktString, SpatialReferences.WebMercator) as MapPoint;

x = importPoint.X;        // x = 100
y = importPoint.Y;        // y = 200
hasZ = importPoint.HasZ;  // hasZ = false
z = importPoint.Z;        // z = 0
hasM = importPoint.HasM;  // hasM = false
m = importPoint.M;        // m = Nan

3 将几何图形导入和导出到众所周知的二进制

// 创建一条折线
List<Coordinate2D> coords = new List<Coordinate2D>
{
    new Coordinate2D(0, 0),
    new Coordinate2D(0, 1),
    new Coordinate2D(1, 1),
    new Coordinate2D(1, 0)
};

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

WkbExportFlags wkbExportFlags = WkbExportFlags.WkbExportDefaults;
WkbImportFlags wkbImportFlags = WkbImportFlags.WkbImportDefaults;

// 导出和导入
byte[] buffer = GeometryEngine.Instance.ExportToWKB(wkbExportFlags, polyline);
Geometry geometry = GeometryEngine.Instance.ImportFromWKB(
    wkbImportFlags, buffer, SpatialReferences.WGS84);
Polyline importPolyline = geometry as Polyline;


// 或者,确定缓冲区的大小
int bufferSize = GeometryEngine.Instance.GetWKBSize(wkbExportFlags, polyline);
buffer = new byte[bufferSize];
// 导出
bufferSize = GeometryEngine.Instance.ExportToWKB(
    wkbExportFlags, polyline, ref buffer);
// 导入
importPolyline = GeometryEngine.Instance.ImportFromWKB(
    wkbImportFlags, buffer, SpatialReferences.WGS84) as Polyline;

4 将几何导入和导出到 EsriShape

// 创建一个包络线
List<MapPoint> coordsZM = new List<MapPoint>
{
    MapPointBuilderEx.CreateMapPoint(1001, 1002, 1003, 1004),
    MapPointBuilderEx.CreateMapPoint(2001, 2002, Double.NaN, 2004),
    MapPointBuilderEx.CreateMapPoint(3001, -3002, 3003, 3004),
    MapPointBuilderEx.CreateMapPoint(1001, -4002, 4003, 4004)
};

Envelope envelope = EnvelopeBuilderEx.CreateEnvelope(
    coordsZM[0], coordsZM[2], SpatialReferences.WGS84);

// 导出和导入
EsriShapeExportFlags exportFlags = EsriShapeExportFlags.EsriShapeExportDefaults;
EsriShapeImportFlags importFlags = EsriShapeImportFlags.EsriShapeImportDefaults;
byte[] buffer = GeometryEngine.Instance.ExportToEsriShape(exportFlags, envelope);
Polygon importedPolygon = GeometryEngine.Instance.ImportFromEsriShape(
    importFlags, buffer, envelope.SpatialReference) as Polygon;
Envelope importedEnvelope = importedPolygon.Extent;

// 不带 z 和 m 的导出
buffer = GeometryEngine.Instance.ExportToEsriShape(
    EsriShapeExportFlags.EsriShapeExportStripZs | 
    EsriShapeExportFlags.EsriShapeExportStripMs, envelope);
importedPolygon = GeometryEngine.Instance.ImportFromEsriShape(
    importFlags, buffer, SpatialReferences.WGS84) as Polygon;
importedEnvelope = importedPolygon.Extent;

bool hasZ = importedEnvelope.HasZ;      // hasZ = false
bool hasM = importedEnvelope.HasM;      // hasM = false

// 使用 shapeSize 导出
int bufferSize = GeometryEngine.Instance.GetEsriShapeSize(
    exportFlags, envelope);
buffer = new byte[bufferSize];

bufferSize = GeometryEngine.Instance.ExportToEsriShape(
    exportFlags, envelope, ref buffer);
importedPolygon = GeometryEngine.Instance.ImportFromEsriShape(
    importFlags, buffer, envelope.SpatialReference) as Polygon;
importedEnvelope = importedPolygon.Extent;


// 或使用 envelope 和 envelopeBuilderEx 类
buffer = envelope.ToEsriShape();
// buffer 表示一个多边形,因为没有包络线 Esri shape 缓冲区
// EnvelopeBuilderEx.FromEsriShape 将多边形 Esri shape 缓冲区转换为多边形的范围。
importedEnvelope = EnvelopeBuilderEx.FromEsriShape(buffer);

5 将几何图形导入和导出为 JSON

// MapPoint
string inputString = 
    "{\"x\":1,\"y\":2,\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}";
Geometry geometry = GeometryEngine.Instance.ImportFromJson(
    JsonImportFlags.JsonImportDefaults, inputString);

MapPoint importPoint = geometry as MapPoint;
// importPoint = 1, 2
// importPoint.SpatialReference.WKid = 4326

// 使用 MapPointBuilderEx 的便捷方法
MapPoint importPoint2 = MapPointBuilderEx.FromJson(inputString);
// importPoint2 = 1, 2
// impointPoint2.SpatialReference.Wkid = 4326

string outputString = GeometryEngine.Instance.ExportToJson(
    JsonExportFlags.JsonExportDefaults, importPoint);
// outputString =
//   "{\"x\":1,\"y\":2,\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}"

string outputString2 = importPoint.ToJson();

inputString = 
    "{\"spatialReference\":{\"wkid\":4326},\"z\":3,\"m\":4,\"x\":1,\"y\":2}";
importPoint = GeometryEngine.Instance.ImportFromJson(
    JsonImportFlags.JsonImportDefaults, inputString) as MapPoint;
// importPoint.HasM = true
// importPoint.HasZ = true
// importPoint.X = 1
// importPoint.Y = 2
// importPoint.M = 4
// importPoint.Z = 3

importPoint2 = MapPointBuilderEx.FromJson(inputString);

// 导出到 JSON - 跳过空间参考
outputString = GeometryEngine.Instance.ExportToJson(
    JsonExportFlags.JsonExportSkipCRS, importPoint);
// outputString = "{\"x\":1,\"y\":2,\"z\":3,\"m\":4}"

// 从 MapPoint 导出,跳过空间参考 - 同 GeometryEngine.Instance.ExportToJson w JsonExportFlags.JsonExportSkipCRS
outputString2 = importPoint.ToJson(true);

//
// Multipoint
//
List<Coordinate2D> coords = new List<Coordinate2D>()
{
    new Coordinate2D(100, 200),
    new Coordinate2D(201, 300),
    new Coordinate2D(301, 400),
    new Coordinate2D(401, 500)
};

Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(
    coords, SpatialReferences.WebMercator);

inputString = 
    "{\"points\":[[100,200],[201,300],[301,400],[401,500]],\"spatialReference\":{\"wkid\":3857}}";
Multipoint importMultipoint = 
    GeometryEngine.Instance.ImportFromJson(
    JsonImportFlags.JsonImportDefaults, inputString) as Multipoint;
// importMultipoint.IsEqual(multipoint) = true

ReadOnlyPointCollection points = importMultipoint.Points;
// points.Count = 4
// points[0] = 100, 200
// points[1] = 201, 300
// points[2] = 301, 400
// points[3] = 401, 500

// 使用 MultipointBuilderEx 的便捷方法
Multipoint importMultipoint2 = MultipointBuilderEx.FromJson(inputString);
// importMultipoint2.IsEqual(multipoint) = true

// 导出到 JSON
outputString = GeometryEngine.Instance.ExportToJson(
    JsonExportFlags.JsonExportDefaults, multipoint);
// outputString = inputString

// 或者直接使用 multipoint 对象
outputString2 = multipoint.ToJson();

//
// Polyline
//
Polyline polyline = PolylineBuilderEx.CreatePolyline(
    coords, SpatialReferences.WebMercator);

// 导出时不带空间参考
outputString = GeometryEngine.Instance.ExportToJson(
    JsonExportFlags.JsonExportSkipCRS, polyline);
// 导入
geometry = GeometryEngine.Instance.ImportFromJson(
    JsonImportFlags.JsonImportDefaults, outputString);
Polyline importPolyline = geometry as Polyline;
// importPolyline.SpatialReference = null

points = importPolyline.Points;
// points.Count = 4
// points[0] = 100, 200
// points[1] = 201, 300
// points[2] = 301, 400
// points[3] = 401, 500

// 使用 PolylineBuilderEx 的便捷方法 
Polyline importPolyline2 = PolylineBuilderEx.FromJson(outputString);
// importPolyline2 = importPolyline

outputString2 = importPolyline2.ToJson();
// outputString2 = outputString

//
// Polygon
//
Polygon polygon = PolygonBuilderEx.CreatePolygon(
    coords, SpatialReferences.WebMercator);

// 导出时不带空间参考
outputString = GeometryEngine.Instance.ExportToJson(
    JsonExportFlags.JsonExportSkipCRS, polygon);
// 导入
geometry = GeometryEngine.Instance.ImportFromJson(
    JsonImportFlags.JsonImportDefaults, outputString);

Polygon importPolygon = geometry as Polygon;
// importPolygon.SpatialReference = null
points = importPolygon.Points;
// points.Count = 5

// 使用 PolygonBuilderEx 的便捷方法
Polygon importPolygon2 = PolygonBuilderEx.FromJson(outputString);
// importPolygon2 = importPolygon

// 从 polygon 导出
outputString2 = importPolygon2.ToJson(true);

// 空的 polygon
polygon = PolygonBuilderEx.CreatePolygon(SpatialReferences.WebMercator);
outputString = GeometryEngine.Instance.ExportToJson(
    JsonExportFlags.JsonExportDefaults, polygon);
importPolygon = GeometryEngine.Instance.ImportFromJson(
    JsonImportFlags.JsonImportDefaults, outputString) as Polygon;

// importPolygon.IsEmpty = true
// importPolygon.SpatialReference.Wkid = 3857

6 将几何图形导入和导出为 XML

MapPoint minPoint = MapPointBuilderEx.CreateMapPoint(1, 1, 1, 1, 3);
MapPoint maxPoint = MapPointBuilderEx.CreateMapPoint(5, 5, 5);

// 
//  MapPoint
// 
string xml = minPoint.ToXml();
MapPoint minPointImport = MapPointBuilderEx.FromXml(xml);
// minPointImport = minPoint

//
// Envelope
//
Envelope envelopeWithID = EnvelopeBuilderEx.CreateEnvelope(minPoint, maxPoint);

// 包络线没有 ID
// envelopeWithID.HasID = false
// envelopeWithID.HasM = true
// envelopeWithID.HasZ = true

xml = envelopeWithID.ToXml();
Envelope envelopeImport = EnvelopeBuilderEx.FromXml(xml);

//
// Multipoint
//
List<MapPoint> list = new List<MapPoint>();
list.Add(minPoint);
list.Add(maxPoint);

Multipoint multiPoint = MultipointBuilderEx.CreateMultipoint(list);

xml = multiPoint.ToXml();
Multipoint multipointImport = MultipointBuilderEx.FromXml(xml);
// multipointImport.PointCount == 2
// multipointImport.HasID = true
// multipointImport.HasM = true
// multipointImport.HasZ = true
;