ArcGIS Pro SDK (九)几何 15 转换
文章目录
环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0
1 创建地理转换
// 从 wkid 创建
GeographicTransformation gt1478 =
ArcGIS.Core.Geometry.GeographicTransformation.Create(1478);
string name = gt1478.Name; // 名称
string wkt = gt1478.Wkt; // WKT 表示
int wkid = gt1478.Wkid; // WKID
// 从 WKT 创建
GeographicTransformation another_gt1478 =
ArcGIS.Core.Geometry.GeographicTransformation.Create(wkt);
// 反向变换
GeographicTransformation inverse_gt148 =
another_gt1478.GetInverse() as GeographicTransformation;
bool isForward = inverse_gt148.IsForward; // 是否正向
2 创建复合地理变换
// 从 wkid 创建单例
CompositeGeographicTransformation cgt =
ArcGIS.Core.Geometry.CompositeGeographicTransformation.Create(108272);
int count = cgt.Count; // 数量 = 1
IList<GeographicTransformation> gts = cgt.Transformations
as IList<GeographicTransformation>;
gts.Add(ArcGIS.Core.Geometry.GeographicTransformation.Create(1437, false));
count = cgt.Count; // 数量 = 2
// 从枚举创建
CompositeGeographicTransformation another_cgt =
ArcGIS.Core.Geometry.CompositeGeographicTransformation.Create(gts);
GeographicTransformation gt0 = another_cgt[0]; // 第一个变换
GeographicTransformation gt1 = another_cgt[1]; // 第二个变换
// 获取反向变换
CompositeGeographicTransformation inversed_cgt = another_cgt.GetInverse() as CompositeGeographicTransformation;
// inversed_cgt[0] 与 gt1 相同
// inversed_cgt[1] 与 gt0 相同
var wkt = gt0.Wkt;
// 从字符串创建
CompositeGeographicTransformation third_cgt =
ArcGIS.Core.Geometry.CompositeGeographicTransformation.Create(wkt, gt0.IsForward);
count = third_cgt.Count; // 数量 = 1
// 从 JSON 创建
string json = cgt.ToJson();
CompositeGeographicTransformation json_cgt =
DatumTransformation.CreateFromJson(json) as CompositeGeographicTransformation;
3 创建投影转换
// 方法需要在 MCT 上运行
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
() =>
{
SpatialReference sr4267 = SpatialReferenceBuilder.CreateSpatialReference(4267);
SpatialReference sr4326 = SpatialReferences.WGS84;
SpatialReference sr3857 = SpatialReferences.WebMercator;
// 创建从 4267 -> 3857 的转换
ProjectionTransformation projTransFromSRs =
ArcGIS.Core.Geometry.ProjectionTransformation.Create(sr4267, sr3857);
// 创建一个矩形范围
Envelope env = EnvelopeBuilderEx.CreateEnvelope(
new Coordinate2D(2, 2), new Coordinate2D(3, 3), sr4267);
// 使用一个地理转换将矩形范围从 4267 投影到 3857
Envelope projectedEnvEx = GeometryEngine.Instance.ProjectEx(
env, projTransFromSRs) as Envelope;
// 创建反向转换,从 3857 -> 4267
ProjectionTransformation projTransFromSRsInverse =
ArcGIS.Core.Geometry.ProjectionTransformation.Create(sr3857, sr4267);
// 使用反向转换将投影后的矩形范围投影回去
Envelope projectedEnvBack =
GeometryEngine.Instance.ProjectEx(
projectedEnvEx, projTransFromSRsInverse) as Envelope;
bool isEqual = env.IsEqual(projectedEnvBack); // 检查两个范围是否相等
});
4 创建高压基准变换
// 从 wkid 创建
HVDatumTransformation hv110018 = HVDatumTransformation.Create(110018);
int wkid = hv110018.Wkid; // WKID
bool isForward = hv110018.IsForward; // 是否正向 = true
string name = hv110018.Name; // 名称 = WGS_1984_To_WGS_1984_EGM2008_1x1_Height
// 从 WKT 创建
string wkt = hv110018.Wkt;
HVDatumTransformation hv110018FromWkt = HVDatumTransformation.Create(wkt);
// 获取反向变换
HVDatumTransformation hv110018Inverse =
hv110018.GetInverse() as HVDatumTransformation;
// hv110018Inverse.IsForward = false
5 创建复合高压基准变换
HVDatumTransformation hv1 = HVDatumTransformation.Create(108034);
HVDatumTransformation hv2 = HVDatumTransformation.Create(108033, false);
List<HVDatumTransformation> hvs = new List<HVDatumTransformation>() { hv1, hv2 };
// 从枚举创建
CompositeHVDatumTransformation compositehv =
CompositeHVDatumTransformation.Create(hvs);
int count = compositehv.Count; // 数量 = 2
List<HVDatumTransformation> transforms =
compositehv.Transformations as List<HVDatumTransformation>;
HVDatumTransformation transform = transforms[0];
// transform.Wkid = 108034
// 获取反向变换
CompositeHVDatumTransformation inverse_compositehv =
compositehv.GetInverse() as CompositeHVDatumTransformation;
// 从 XML 创建
string xml = compositehv.ToXml();
// 在 2.x 版本中 - CompositeHVDatumTransformation xml_compositehv =
// CompositeHVDatumTransformation.CreateFromXML(xml);
var xml_compositehv = CompositeHVDatumTransformation.CreateFromXml(xml);
// 从 JSON 创建
string json = compositehv.ToJson();
CompositeHVDatumTransformation json_compositehv =
DatumTransformation.CreateFromJson(json) as CompositeHVDatumTransformation;
6 决定转换
// 方法需要在 MCT 上运行
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
() =>
{
//
// 查找在空间参考 4267 和 4326 之间使用的第一个转换
//
SpatialReference sr4267 =
SpatialReferenceBuilder.CreateSpatialReference(4267);
SpatialReference sr4326 = SpatialReferences.WGS84;
List<ProjectionTransformation> transformations =
ProjectionTransformation.FindTransformations(sr4267, sr4326);
// transformations.Count = 1
ProjectionTransformation projTrans = transformations[0];
CompositeGeographicTransformation compositeGT =
projTrans.Transformation as CompositeGeographicTransformation;
GeographicTransformation gt = compositeGT[0];
// gt.Wkid = 15851
// gt.Name = "NAD_1927_To_WGS_1984_79_CONUS"
// gt.IsForward = true
//
// 查找在空间参考 4267 和 4326 之间使用的前五个转换
//
transformations = ProjectionTransformation.FindTransformations(
sr4267, sr4326, numResults: 5);
// transformations.Count = 5
projTrans = transformations[0];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 1
// compositeGT[0].Wkid = 15851
// compositeGT[0].Name = "NAD_1927_To_WGS_1984_79_CONUS"
// compositeGT[0].IsForward = true
projTrans = transformations[1];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 1
// compositeGT[0].Wkid = 1173
// compositeGT[0].Name = "NAD_1927_To_WGS_1984_4"
// compositeGT[0].IsForward = true
projTrans = transformations[2];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 1
// compositeGT[0].Wkid = 1172
// compositeGT[0].Name = "NAD_1927_To_WGS_1984_3"
// compositeGT[0].IsForward = true
projTrans = transformations[3];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 2
// compositeGT[0].Wkid = 1241
// compositeGT[0].Name = "NAD_1927_To_NAD_1983_NADCON"
// compositeGT[0].IsForward = true
// compositeGT[1].Wkid = 108190
// compositeGT[1].Name = "WGS_1984_(ITRF00)_To_NAD_1983"
// compositeGT[1].IsForward = false
projTrans = transformations[4];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 2
// compositeGT[0].Wkid = 1241
// compositeGT[0].Name = "NAD_1927_To_NAD_1983_NADCON"
// compositeGT[0].IsForward = true
// compositeGT[1].Wkid = 1515
// compositeGT[1].Name = "NAD_1983_To_WGS_1984_5"
// compositeGT[1].IsForward = true
//
// 查找在空间参考 4267 和 4326 之间在阿拉斯加区域使用的第一个转换
//
Coordinate2D coord = new Coordinate2D(-148, 60);
List<ProjectionTransformation> alaskatransforms =
ProjectionTransformation.FindTransformations(sr4267, sr4326, coord);
// alaskatransforms.Count = 1
projTrans = alaskatransforms[0];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
gt = compositeGT[0];
// gt.Wkid = 1243
// gt.Name = "NAD_1927_To_WGS_1984_11"
// gt.IsForward = true
//
// 查找在空间参考 3857 和 4326 之间使用的第一个转换
//
SpatialReference sr3857 = SpatialReferences.WebMercator;
transformations = ProjectionTransformation.FindTransformations(sr3857, sr4326);
// transformations.Count = 1
projTrans = transformations[0];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 0
//
// 查找在空间参考 4326 和 3857 之间使用的第一个转换
//
transformations = ProjectionTransformation.FindTransformations(sr4326, sr3857);
// transformations.Count = 1
projTrans = transformations[0];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 0
//
// 查找在空间参考 4326 和 7030 之间使用的第一个转换
//
SpatialReference sr7030 = SpatialReferenceBuilder.CreateSpatialReference(7030);
transformations = ProjectionTransformation.FindTransformations(sr4326, sr7030);
// transformations.Count = 1
projTrans = transformations[0];
compositeGT = projTrans.Transformation as CompositeGeographicTransformation;
// compositeGT.Count = 1
// compositeGT[0].Wkid = 108190
// compositeGT[0].Name = "WGS_1984_(ITRF00)_To_NAD_1983"
// compositeGT[0].IsForward = true
});
7 地图点 - 地理坐标字符串转换
SpatialReference sr = SpatialReferences.WGS84;
SpatialReference sr2 = SpatialReferences.WebMercator;
// 创建一些点
MapPoint point0 = MapPointBuilderEx.CreateMapPoint(0, 0, sr);
MapPoint point1 = MapPointBuilderEx.CreateMapPoint(10, 20, sr);
MapPoint point2 = GeometryEngine.Instance.Project(point1, sr2) as MapPoint;
MapPoint pointEmpty = MapPointBuilderEx.CreateMapPoint(sr);
MapPoint pointwithNoSR = MapPointBuilderEx.CreateMapPoint(1, 1);
MapPoint pointZM = MapPointBuilderEx.CreateMapPoint(1, 2, 3, 4, sr);
// 转换为 MGRS
ToGeoCoordinateParameter mgrsParam =
new ToGeoCoordinateParameter(GeoCoordinateType.MGRS);
// 31NAA6602100000
string geoCoordString = point0.ToGeoCoordinateString(mgrsParam);
// 使用构建器从字符串创建一个新点。
// 坐标相同
// outPoint.x = 0; outPoint.Y = 0
MapPoint outPoint =
MapPointBuilderEx.FromGeoCoordinateString(
geoCoordString, sr, GeoCoordinateType.MGRS);
// 32QPH0460911794
// outPoint.X = 10; outPoint.Y = 20
geoCoordString = point1.ToGeoCoordinateString(mgrsParam);
outPoint = MapPointBuilderEx.FromGeoCoordinateString(
geoCoordString, sr, GeoCoordinateType.MGRS);
// z, m 没有转换
// outPoint.X = 1; outPoint.Y = 2; outPoint.Z = Nan; outPoint.M = Nan;
geoCoordString = pointZM.ToGeoCoordinateString(mgrsParam);
outPoint = MapPointBuilderEx.FromGeoCoordinateString(
geoCoordString, sr, GeoCoordinateType.MGRS);
// 设置数字位数为 2 并转换
// 32QPH0512
// outPoint.X = 10; outPoint.Y = 20
mgrsParam.NumDigits = 2;
geoCoordString = point1.ToGeoCoordinateString(mgrsParam);
outPoint = MapPointBuilderEx.FromGeoCoordinateString(
geoCoordString, sr, GeoCoordinateType.MGRS);
// 转换为 UTM
ToGeoCoordinateParameter utmParam =
new ToGeoCoordinateParameter(GeoCoordinateType.UTM);
// 31N 166021 0000000
geoCoordString = point0.ToGeoCoordinateString(utmParam);
// 32Q 604609 2211793
geoCoordString = point1.ToGeoCoordinateString(utmParam);
// 转换为 DMS
ToGeoCoordinateParameter dmsParam =
new ToGeoCoordinateParameter(GeoCoordinateType.DMS);
// 00 00 00.00N 000 00 00.00E
geoCoordString = point0.ToGeoCoordinateString(dmsParam);
// 20 00 00.00N 010 00 00.00E
geoCoordString = point1.ToGeoCoordinateString(dmsParam);
// 转换为 DDM
ToGeoCoordinateParameter ddmParam =
new ToGeoCoordinateParameter(GeoCoordinateType.DDM);
// 00 00.0000N 000 00.0000E
geoCoordString = point0.ToGeoCoordinateString(ddmParam);
// 20 00.0000N 010 00.0000E
geoCoordString = point1.ToGeoCoordinateString(ddmParam);
// 转换为 DD
ToGeoCoordinateParameter ddParam =
new ToGeoCoordinateParameter(GeoCoordinateType.DD);
// 00.000000N 000.000000E
geoCoordString = point0.ToGeoCoordinateString(ddParam);
// 20.000000N 010.000000E
geoCoordString = point1.ToGeoCoordinateString(ddParam);