GIS中的坐标系一般分为地理坐标系和投影坐标系。地理坐标系就是用经纬度来表示位置,投影坐标系则是按照一定的数学规则,将球体投影到平面上,一般用X、Y表示位置,单位为米。若要进行投影转换,如果转换前和转换后的空间参考是基于同一参考椭球体,则可在ArcGIS中直接进行转换,如果转换前和转换后的空间参考基于不同的参考椭球体,则首先需要计算七参数,然后才能在ArcGIS中进行转换。下面是ArcEngine中关于ISpatialReference
的部分操作代码:
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace EngineConsoleApplication1
{
class Program
{
private static LicenseInitializer m_AOLicenseInitializer = new EngineConsoleApplication1.LicenseInitializer();
[STAThread()]
static void Main(string[] args)
{
m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine }, new esriLicenseExtensionCode[] { });
m_AOLicenseInitializer.ShutdownApplication();
// 根据prj文件获取空间参考
ISpatialReference pSpatialReference = GetSpatialReference(@"E:\Users\dsf\Desktop\data\a.prj");
Console.WriteLine(pSpatialReference.Name);
Console.WriteLine("---------------------------");
// 根据要素类获取空间参考
IFeatureClass pFeatureClass = GetFeatureClass(@"E:\Users\dsf\Desktop\data\b.shp");
pSpatialReference = GetSpatialReference(pFeatureClass);
Console.WriteLine(pSpatialReference.Name);
Console.WriteLine("---------------------------");
// 创建地理坐标系
pSpatialReference = GetSpatialReference(esriSRGeoCSType.esriSRGeoCS_WGS1984);
Console.WriteLine(pSpatialReference.Name);
Console.WriteLine("---------------------------");
// 创建投影坐标系
pSpatialReference = GetSpatialReference(esriSRProjCSType.esriSRProjCS_Beijing1954GK_13N);
Console.WriteLine(pSpatialReference.Name);
Console.WriteLine("---------------------------");
// 定义投影
pFeatureClass = GetFeatureClass(@"E:\Users\dsf\Desktop\data\c.shp");
pSpatialReference = GetSpatialReference(esriSRGeoCSType.esriSRGeoCS_WGS1984);
bool ok = DefineProjection(pFeatureClass, pSpatialReference);
if (ok)
{
Console.WriteLine("定义投影成功");
}
else
{
Console.WriteLine("定义投影失败");
}
Console.WriteLine("---------------------------");
// 同一基准面下的坐标系转换
ConvertProjection();
Console.WriteLine("---------------------------");
// 获取投影信息\
GetSpatialReferenceInfo(pSpatialReference);
Console.ReadKey();
}
// 根据prj文件获取空间参考
static ISpatialReference GetSpatialReference(string filePath)
{
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(filePath);
return pSpatialReference;
}
// 根据要素类获取空间参考
static ISpatialReference GetSpatialReference(IFeatureClass pFeatureClass)
{
IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
return pSpatialReference;
}
// 创建地理坐标系
static ISpatialReference GetSpatialReference(esriSRGeoCSType type)
{
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)type);
return pSpatialReference;
}
// 创建投影坐标系
static ISpatialReference GetSpatialReference(esriSRProjCSType type)
{
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem((int)type);
return pSpatialReference;
}
// 定义空间参考
static bool DefineProjection(IFeatureClass pFeatureClass, ISpatialReference pSpatialReference)
{
IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit;
if (pGeoDatasetSchemaEdit.CanAlterSpatialReference)
{
pGeoDatasetSchemaEdit.AlterSpatialReference(pSpatialReference);
return true;
}
else
{
return false;
}
}
// 同一基准面下的坐标系转换
static void ConvertProjection()
{
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pFromSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(4326);
ISpatialReference pToSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(3395);
// 设置坐标
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.SpatialReference = pFromSpatialReference;
pPoint.X = 30;
pPoint.Y = 30;
// 地理坐标转换为投影坐标
pPoint.Project(pToSpatialReference);
Console.WriteLine(pPoint.X + "," + pPoint.Y);
// 投影坐标转换为地理坐标
pPoint.Project(pFromSpatialReference);
Console.WriteLine(pPoint.X + "," + pPoint.Y);
}
// 不同基准面下的坐标系转换
static void ConvertProjection(IPoint pPoint, double dx, double dy, double dz, double rx, double ry, double rz, double s)
{
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pFromSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(2384); // Xian_1980_3_Degree_GK_CM_117E
ISpatialReference pToSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(3395); // Beijing_1954_GK_Zone_19N
// 设置七参数
IGeoTransformation pGeoTransformation = new CoordinateFrameTransformation() as IGeoTransformation;
ICoordinateFrameTransformation pCoordinateFrameTransformation = pGeoTransformation as ICoordinateFrameTransformation;
pCoordinateFrameTransformation.PutSpatialReferences(pFromSpatialReference, pToSpatialReference);
pCoordinateFrameTransformation.PutParameters(dx, dy, dz, rx, ry, rz, s);
// 投影转换
IGeometry2 pGeometry = pPoint as IGeometry2;
pGeometry.SpatialReference = pFromSpatialReference;
pGeometry.ProjectEx(pToSpatialReference, esriTransformDirection.esriTransformForward, pGeoTransformation, false, 0, 0);
}
// 空间参考信息
static void GetSpatialReferenceInfo(ISpatialReference pSpatialReference)
{
if (pSpatialReference is IProjectedCoordinateSystem)
{
IProjectedCoordinateSystem2 pProjectedCoordinateSystem = pSpatialReference as IProjectedCoordinateSystem2;
Console.WriteLine(pProjectedCoordinateSystem.FalseNorthing);
Console.WriteLine(pProjectedCoordinateSystem.FalseEasting);
Console.WriteLine(pProjectedCoordinateSystem.CentralMeridian[true]);
Console.WriteLine(pProjectedCoordinateSystem.ScaleFactor);
Console.WriteLine(pProjectedCoordinateSystem.LatitudeOfOrigin);
Console.WriteLine(pProjectedCoordinateSystem.GeographicCoordinateSystem.Datum.Spheroid.Name);
Console.WriteLine(pProjectedCoordinateSystem.Projection.Name);
}
else
{
IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReference as IGeographicCoordinateSystem;
Console.WriteLine(pGeographicCoordinateSystem.Datum.Name);
Console.WriteLine(pGeographicCoordinateSystem.Datum.Spheroid.Name);
Console.WriteLine(pGeographicCoordinateSystem.Datum.Spheroid.SemiMajorAxis);
Console.WriteLine(pGeographicCoordinateSystem.Datum.Spheroid.SemiMinorAxis);
Console.WriteLine(pGeographicCoordinateSystem.Datum.Spheroid.Flattening);
}
}
// 获取要素类
static IFeatureClass GetFeatureClass(string filePath)
{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IWorkspaceFactoryLockControl pWorkspaceFactoryLockControl = pWorkspaceFactory as IWorkspaceFactoryLockControl;
if (pWorkspaceFactoryLockControl.SchemaLockingEnabled)
{
pWorkspaceFactoryLockControl.DisableSchemaLocking();
}
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(System.IO.Path.GetFileName(filePath));
return pFeatureClass;
}
}
}