空间参考(三)-ArcGIS Engine的支持
上一篇 / 下一篇 2011-08-30 22:24:59 / 个人分类:ArcGIS Engine之路
ArcGIS Engine提供了一系列对象供开发者管理GIS系统的坐标系统。对大部分开发者而言了解ProjectedCoordinateSystem, GeographicCoordinateSystem, SpatialReference Environment这三个组件类是非常有必要的,对于高级开发者而言,可能需要自定义坐标系统可以使用这些对象Projection,Datum,AngularUnit,Spheriod,PrimeMeridian和GeoTransformation等。
同一坐标系转换
private IPoint GetpProjectPoint(IPoint pPoint, bool pBool)
{
SpatialReferenceEnvironment pSpatialReferenceEnvironemnt = new SpatialReferenceEnvironment();
ISpatialReference pFromSpatialReference = pSpatialReferenceEnvironemnt.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980);//西安80
ISpatialReference pToSpatialReference = pSpatialReferenceEnvironemnt.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_34);//西安80
if (pBool == true)//球面转平面
{
IGeometry pGeo = (IGeometry)pPoint;
pGeo.SpatialReference = pFromSpatialReference;
pGeo.Project(pToSpatialReference);
return pPoint;
}
else //平面转球面
{
IGeometry pGeo = (IGeometry)pPoint;
pGeo.SpatialReference = pToSpatialReference;
pGeo.Project(pFromSpatialReference);
return pPoint;
}
}
不同基准面转换(需要转换参数)
public void ProjectExExample()
{
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
// ISpatialReference pFromCustom = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(@"E:\arcgis\Engine\zidingyi.prj");
IPoint pFromPoint = new PointClass();
pFromPoint.X = 518950.788;
pFromPoint.Y = 4335923.97;
IZAware pZAware = pFromPoint as IZAware;
pZAware.ZAware = true;
pFromPoint.Z = 958.4791;
// ((IGeometry)pFromPoint).SpatialReference = pFromCustom;
//自定义投影WGS84下的北京6度19带。
((IGeometry)pFromPoint).SpatialReference = CreateCustomProjectedCoordinateSystem();
//目标投影
IProjectedCoordinateSystem projectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_19);
//因为目标基准面和原始基准面不在同一个上,所以牵扯到参数转换,我用7参数转换
ICoordinateFrameTransformation pCoordinateFrameTransformation = new CoordinateFrameTransformationClass();
pCoordinateFrameTransformation.PutParameters(7个参数略);
pCoordinateFrameTransformation.PutSpatialReferences(CreateCustomProjectedCoordinateSystem(), projectedCoordinateSystem as ISpatialReference);
//投影转换
IGeometry2 pGeometry = pFromPoint as IGeometry2;
pGeometry.ProjectEx(projectedCoordinateSystem as ISpatialReference, esriTransformDirection.esriTransformForward, pCoordinateFrameTransformation, false, 0, 0);
}
private IProjectedCoordinateSystem CreateCustomProjectedCoordinateSystem()
{
ISpatialReferenceFactory2 pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
IProjectionGEN pProjection = pSpatialReferenceFactory.CreateProjection((int) esriSRProjectionType.esriSRProjection_GaussKruger) as IProjectionGEN;
IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
ILinearUnit pUnit = pSpatialReferenceFactory.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter) as ILinearUnit;
IParameter[] pParameters = pProjection.GetDefaultParameters();
IProjectedCoordinateSystemEdit pProjectedCoordinateSystemEdit = new ProjectedCoordinateSystemClass();
object pName = "WGS-BeiJing1954";
object pAlias = "WGS-BeiJing1954";
object pAbbreviation = "WGS-BeiJing1954";
object pRemarks = "WGS-BeiJing1954";
object pUsage = "Calculate Meter From lat and lon";
object pGeographicCoordinateSystemObject = pGeographicCoordinateSystem as object;
object pUnitObject = pUnit as object;
object pProjectionObject = pProjection as object;
object pParametersObject = pParameters as object;
pProjectedCoordinateSystemEdit.Define(ref pName, ref pAlias, ref pAbbreviation, ref pRemarks, ref pUsage, ref pGeographicCoordinateSystemObject, ref pUnitObject,ref pProjectionObject, ref pParametersObject);
IProjectedCoordinateSystem5 pProjectedCoordinateSystem = pProjectedCoordinateSystemEdit as IProjectedCoordinateSystem5;
pProjectedCoordinateSystem.FalseEasting = 500000;
pProjectedCoordinateSystem.LatitudeOfOrigin = 0;
pProjectedCoordinateSystem.set_CentralMeridian(true,111);
pProjectedCoordinateSystem.ScaleFactor=1;
pProjectedCoordinateSystem.FalseNorthing=0;
return pProjectedCoordinateSystem;
}