作业题:将西安80坐标点: 5221821.930,44699943.739(示例数据,无真实地点)计算转换成CGCS2000下坐标点。
这是个坐标转换的问题,如果我们把它当作一个数学题,那就得先去找地理数据,地球半径、曲率、7参数等等,然后还得发挥出色的几何计算能力,巴拉巴拉巴拉...... 很明显,作为一个GISer伪程序猿,我是没有这个自信。只有找现成的工具外援了。手里只有ArcGIS库,写个小代码实现吧。
分析:题目前后比脸都干净的这个坐标是个什么类型的坐标?ArcGIS坐标转换的接口是哪个?
解:
IPoint pFromPoint = new PointClass();
pFromPoint.X = 44699943.739;
pFromPoint.Y = 5221821.930;
根据 pFromPoint.X 得原坐标类型为投影为3° 45带(不要问为什么要定这个投影分带坐标!学过地图学的一眼就抽出来了)投影分带必须正确,否则gis计算会抛出异常。给pFromPoint设置投影坐标系:
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
int sr1num = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_45;
ISpatialReference sr1 = (ISpatialReference)pSpatialReferenceFactory.CreateProjectedCoordinateSystem(sr1num);
(pFromPoint as IGeometry).SpatialReference= sr1 ;
设置转换目标投影坐标系:
// 4490 为CGCS2000 SRID, ersi类库中9.x 中没有对应枚举类型, 当前10.4环境中可用
ISpatialReference sr2 = (ISpatialReference)pSpatialReferenceFactory.CreateGeographicCoordinateSystem(4490);
如果实在找不到CGCS2000 的SRID, 也可以通过找一个CGCS2000的.prj文件来进行读取坐标系。
// 地理Geo CGCS2000
string filepath = @"..\Coordinate Systems\Geographic Coordinate Systems\Asia\China Geodetic Coordinate System 2000.prj";
sr2 = (ISpatialReference)pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(filepath);
开始坐标系转换:
(pFromPoint as IGeometry).Project(sr2);
执行过后此时pFromPoint就已经改变,
完整代码如下:
public void TestSpatialProjectionTransfer()
{
new AOInitialLicense();
double x, y;
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
IPoint pt = new PointClass() as IPoint;
pt.PutCoords(44699943.739701, 5221821.93020041);
System.Diagnostics.Debug.WriteLine("变化前: X: " + pt.X + "_____Y: " + pt.Y);
int sr1num = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_45;
int sr2num = (int)4490;
ISpatialReference sr1 = (ISpatialReference)pSpatialReferenceFactory.CreateProjectedCoordinateSystem(sr1num);
ISpatialReference sr2 = (ISpatialReference)pSpatialReferenceFactory.CreateGeographicCoordinateSystem(sr2num);
// ISpatialReference sr2 = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(@"E:\arcgis\Engine\z idingyi.prj");
(pt as IGeometry).SpatialReference= sr1 ;
(pt as IGeometry).Project(sr2);
System.Diagnostics.Debug.WriteLine("变化后:");
pt.QueryCoords(out x, out y);
System.Diagnostics.Debug.WriteLine("X:" + x + "_____Y:" + y);
}