ArcGIS Pro SDK (八)几何 1 空间参考
文章目录
环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0
1 构造空间参考 - 从已知 ID
// 使用构建器便捷方法或构建器构造函数。
// 构建器便捷方法不需要在MCT上运行。
SpatialReference sr3857 = SpatialReferenceBuilder.CreateSpatialReference(3857);
// 构建器构造函数需要在MCT上运行。
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
() =>
{
using (SpatialReferenceBuilder srBuilder = new SpatialReferenceBuilder(3857))
{
// 使用构建器做一些事情
sr3857 = srBuilder.ToSpatialReference();
}
});
2 构造空间引用 - 从字符串
// 使用构建器便捷方法或构建器构造函数。
string wkt = "GEOGCS[\"MyGCS84\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Radian\",1.0]]";
// 构建器便捷方法不需要在MCT上运行。
SpatialReference sr = SpatialReferenceBuilder.CreateSpatialReference(wkt);
// 构建器构造函数需要在MCT上运行。
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
using (SpatialReferenceBuilder builder = new SpatialReferenceBuilder(wkt))
{
// 使用构建器做一些事情
SpatialReference anotherSR = builder.ToSpatialReference();
}
});
3 使用 WGS84 空间参考
SpatialReference wgs84 = SpatialReferences.WGS84;
bool isProjected = wgs84.IsProjected; // false
bool isGeographic = wgs84.IsGeographic; // true
4 使用垂直坐标系构造空间参考 - 从已知 ID
// 使用构建器便捷方法或构建器构造函数。
// 查看垂直坐标系列表,访问 http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Vertical_coordinate_systems/02r3000000rn000000/
// 构建器便捷方法不需要在MCT上运行。
// 4326 = GCS_WGS_1984
// 115700 = 垂直 WGS_1984
SpatialReference sr4326_115700 = SpatialReferenceBuilder.CreateSpatialReference(4326, 115700);
// 构建器构造函数需要在MCT上运行。
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
using (SpatialReferenceBuilder sb = new SpatialReferenceBuilder(4326, 115700))
{
// SpatialReferenceBuilder 属性
// sb.wkid == 4326
// sb.Wkt == "GEOGCS["MyGCS84",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT[\"Radian\",1.0]]"
// sb.name == GCS_WGS_1984
// sb.vcsWkid == 115700
// sb.VcsWkt == "VERTCS["WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PARAMETER["Vertical_Shift",0.0],PARAMETER["Direction",1.0],UNIT["Meter",1.0]]
// 使用构建器做一些事情
sr4326_115700 = sb.ToSpatialReference();
}
});
5 使用垂直坐标系构造空间参考 - 从字符串
// 使用构建器便捷方法或构建器构造函数。
// 自定义VCS - 使用垂直偏移-1.23而不是0
string custom_vWkt = @"VERTCS[""SHD_height"",VDATUM[""Singapore_Height_Datum""],PARAMETER[""Vertical_Shift"",-1.23],PARAMETER[""Direction"",-1.0],UNIT[""Meter"",1.0]]";
// 构建器便捷方法不需要在MCT上运行。
SpatialReference sr4326_customVertical = SpatialReferenceBuilder.CreateSpatialReference(4326, custom_vWkt);
// SpatialReferenceBuilder 属性
// sr4326_customVertical.wkid == 4326
// sr4326_customVertical.vert_wkid == 0
// sr4326_customVertical.vert_wkt == custom_vWkt
// sr4326_customVertical.hasVcs == true
// 构建器构造函数需要在MCT上运行。
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
using (SpatialReferenceBuilder sb = new SpatialReferenceBuilder(4326, custom_vWkt))
{
// 使用构建器做一些事情
sr4326_customVertical = sb.ToSpatialReference();
}
});
6 使用自定义 PCS 构造空间参考 - 从字符串
// 使用构建器便捷方法或构建器构造函数。
// 自定义 PCS,预定义 GCS
string customWkt = "PROJCS[\"WebMercatorMile\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Mile\",1609.344000614692]]";
// 构建器便捷方法不需要在MCT上运行。
SpatialReference spatialReference = SpatialReferenceBuilder.CreateSpatialReference(customWkt);
// SpatialReferenceBuilder 属性
// spatialReference.Wkt == customWkt
// spatialReference.Wkid == 0
// spatialReference.VcsWkid == 0
// spatialReference.GcsWkid == 4326
SpatialReference gcs = spatialReference.Gcs;
// gcs.Wkid == 4326
// gcs.IsGeographic == true
// spatialReference.GcsWkt == gcs.Wkt
// 构建器构造函数需要在MCT上运行。
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
using (SpatialReferenceBuilder sb = new SpatialReferenceBuilder(customWkt))
{
// 使用构建器做一些事情
spatialReference = sb.ToSpatialReference();
}
});
7 空间参考属性
// 构建器构造函数需要在MCT上运行。
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// 使用构建器构造函数
using (SpatialReferenceBuilder srBuilder = new SpatialReferenceBuilder(3857))
{
// 空间参考构建器属性
int builderWkid = srBuilder.Wkid;
string builderWkt = srBuilder.Wkt;
string builderName = srBuilder.Name;
double xyScale = srBuilder.XYScale;
double xyTolerance = srBuilder.XYTolerance;
double xyResolution = srBuilder.XYResolution;
Unit unit = srBuilder.Unit;
double zScale = srBuilder.ZScale;
double zTolerance = srBuilder.ZTolerance;
Unit zUnit = srBuilder.ZUnit;
double mScale = srBuilder.MScale;
double mTolerance = srBuilder.MTolerance;
double falseX = srBuilder.FalseX;
double falseY = srBuilder.FalseY;
double falseZ = srBuilder.FalseZ;
double falseM = srBuilder.FalseM;
// 获取空间参考
SpatialReference sr3857 = srBuilder.ToSpatialReference();
// 空间参考属性
int srWkid = sr3857.Wkid;
string srWkt = sr3857.Wkt;
string srName = sr3857.Name;
xyScale = sr3857.XYScale;
xyTolerance = sr3857.XYTolerance;
xyResolution = sr3857.XYResolution;
unit = sr3857.Unit;
zScale = sr3857.ZScale;
zTolerance = sr3857.ZTolerance;
zUnit = sr3857.ZUnit;
mScale = sr3857.MScale;
mTolerance = sr3857.MTolerance;
falseX = sr3857.FalseX;
falseY = sr3857.FalseY;
falseZ = sr3857.FalseZ;
falseM = sr3857.FalseM;
bool hasVcs = sr3857.HasVcs;
}
});
8 导入和导出空间参考
SpatialReference srWithVertical = SpatialReferenceBuilder.CreateSpatialReference(4326, 6916);
string xml = srWithVertical.ToXml();
SpatialReference importedSR = SpatialReferenceBuilder.FromXml(xml);
// importedSR.Wkid = 4326
// importedSR.VcsWkid = 6916
string json = srWithVertical.ToJson();
importedSR = SpatialReferenceBuilder.FromJson(json);
// importedSR.Wkid = 4326
// importedSR.VcsWkid = 6916
9 确定给定点处空间参考的格网收敛
Coordinate2D coordinate = new Coordinate2D(10, 30);
double angle = SpatialReferences.WGS84.GetConvergenceAngle(coordinate);
// angle = 0
SpatialReference srUTM30N = SpatialReferenceBuilder.CreateSpatialReference(32630);
coordinate.X = 500000;
coordinate.Y = 550000;
angle = srUTM30N.GetConvergenceAngle(coordinate);
// angle = 0
MapPoint pointWGS84 = MapPointBuilderEx.CreateMapPoint(10, 50, SpatialReferences.WGS84);
MapPoint pointUTM30N = GeometryEngine.Instance.Project(
pointWGS84, srUTM30N) as MapPoint;
coordinate = (Coordinate2D)pointUTM30N;
// 获取收敛角并转换为度数
angle = srUTM30N.GetConvergenceAngle(coordinate) * 180 / Math.PI;
// angle = 10.03
10 基准
var cimMapDefinition = MapView.Active.Map.GetDefinition();
// 使用如果地图的 sr 没有垂直坐标系
var datumTransformations = cimMapDefinition.DatumTransforms;
// 使用如果地图的 sr 有垂直坐标系
var hvDatumTransformations = cimMapDefinition.HVDatumTransforms;
11 空间参照基准和基准属性
// 获取空间参考的基准
SpatialReference srWgs84 = SpatialReferences.WGS84;
Datum datum = srWgs84.Datum;
// datum.Name = "D_WGS_1984"
// datum.Wkid = 6326
// datum.SpheroidName = "WGS_1984"
// datum.SpheroidWkid = 7030
// datum.SpheroidFlattening = 0.0033528106647474805
// datum.SpheroidSemiMajorAxis = 6378137.0
// datum.SpheroidSemiMinorAxis = 6356752.3142451793
// 自定义 WKT
string wyomingWkt = "PROJCS[\"Wyoming_State_Pl_NAD_1927\",GEOGCS[\"GCS_North_American_1927\",DATUM[\"D_North_American_1927_Perry\",SPHEROID[\"Clarke_1866_Chase\",6378210.0,250.0]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"false_easting\",500000.0],PARAMETER[\"false_northing\",0.0],PARAMETER[\"central_meridian\",-107.3333333],PARAMETER[\"scale_factor\",0.9999412],PARAMETER[\"latitude_of_origin\",40.66666667],UNIT[\"Foot_US\",0.3048006096012192]]";
SpatialReference srFromWkt = SpatialReferenceBuilder.CreateSpatialReference(wyomingWkt);
datum = srWgs84.Datum;
// datum.Name = "D_North_American_1927_Perry"
// datum.Wkid = 0
// datum.SpheroidName = "Clarke_1866_Chase"
// datum.SpheroidWkid = 0
// datum.SpheroidFlattening = 0.004
// datum.SpheroidSemiMajorAxis = 6378210.0
// datum.SpheroidSemiMinorAxis = 6352697.16