题记:本文部分内容为转载,部分内容为原创
1.创建空间参考
- /// <summary>
- /// 根据prj文件创建空间参考
- /// </summary>
- /// <param name="strProFile">空间参照文件</param>
- /// <returns></returns>
- public static ISpatialReference CreateSpatialReference(string strProFile)
- {
- ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
- ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(strProFile);
- return pSpatialReference;
- }
- /// <summary>
- /// 创建地理坐标系
- /// </summary>
- /// <param name="gcType">esriSRProjCS4Type</param>
- /// <returns></returns>
- public static ISpatialReference CreateGeographicCoordinate(esriSRProjCS4Type gcsType)
- {
- ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
- ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)gcsType);
- return pSpatialReference;
- }
- /// <summary>
- /// 创建投影坐标系
- /// </summary>
- /// <param name="pcType">esriSRProjCS4Type</param>
- /// <returns></returns>
- public static ISpatialReference CreateProjectedCoordinate(esriSRProjCS4Type pcsType)
- {
- ISpatialReferenceFactory2 pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
- ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem((int)pcsType);
- return pSpatialReference;
- }
- /// <summary>
- /// 获取空投影
- /// </summary>
- /// <returns></returns>
- public static ISpatialReference CreateUnKnownSpatialReference()
- {
- ISpatialReference pSpatialReference = new UnknownCoordinateSystemClass();
- pSpatialReference.SetDomain(0, 99999999, 0, 99999999);//设置空间范围
- return pSpatialReference;
- }
2.获取空间参考
- /// <summary>
- /// 获取要素集空间参考
- /// </summary>
- /// <param name="pFeatureDataset">要素集</param>
- /// <returns></returns>
- public static ISpatialReference GetSpatialReference(IFeatureDataset pFeatureDataset)
- {
- IGeoDataset pGeoDataset = pFeatureDataset as IGeoDataset;
- ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
- return pSpatialReference;
- }
- /// <summary>
- /// 获取要素层空间参考
- /// </summary>
- /// <param name="pFeatureLayer">要素层</param>
- /// <returns></returns>
- public static ISpatialReference GetSpatialReferenc(IFeatureLayer pFeatureLayer)
- {IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
- IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
- ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
- return pSpatialReference;
- }
- /// <summary>
- /// 获取要素类空间参考
- /// </summary>
- /// <param name="pFeatureClass">要素类</param>
- /// <returns></returns>
- public static ISpatialReference GetSpatialReference(IFeatureClass pFeatureClass)
- {
- IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
- ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
- return pSpatialReference;
- }
3.修改空间参考
- /// <summary>
- /// 修改要素集空间参考
- /// </summary>
- /// <param name="pFeatureDataset">要素集</param>
- /// <param name="pSpatialReference">新空间参考</param>
- public static void AlterSpatialReference(IFeatureDataset pFeatureDataset, ISpatialReference pSpatialReference)
- {
- IGeoDataset pGeoDataset = pFeatureDataset as IGeoDataset;
- IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit;
- if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true)
- pGeoDatasetSchemaEdit.AlterSpatialReference(pSpatialReference);
- }
- /// <summary>
- /// 修改要素类空间参考
- /// </summary>
- /// <param name="pFeatureClass">要素类</param>
- /// <param name="pSpatialReference">新空间参考</param>
- public static void AlterSpatialReference(IFeatureClass pFeatureClass, ISpatialReference pSpatialReference)
- {
- IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
- IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit;
- if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true)
- pGeoDatasetSchemaEdit.AlterSpatialReference(pSpatialReference);
- }
栅格数据椭球变换
<pre name="code" class="csharp"> /// <summary>
/// 栅格椭球转换
/// </summary>
/// <param name="rasterDataset">已知参照系的椭球</param>
/// <param name="outSR">输出的栅格的空间参考</param>
/// <param name="geoTrans">转换七参数</param>
public static void ProjectRasterWithDatumTransformation(IRasterDataset2 rasterDataset, ISpatialReference outSR, esriSRGeoTransformation2Type geoTrans)
{
IRaster raster = rasterDataset.CreateFullRaster();
IRasterProps rasterProps = (IRasterProps)raster;
rasterProps.SpatialReference = outSR;
//七参数设置
ISpatialReferenceFactory2 srFactory = new SpatialReferenceEnvironmentClass();
IGeoTransformation geoTransformation = (IGeoTransformation)
srFactory.CreateGeoTransformation((int)geoTrans);
IGeoTransformationOperationSet operationSet = new
GeoTransformationOperationSetClass();
operationSet.Set(esriTransformDirection.esriTransformForward, geoTransformation);
operationSet.Set(esriTransformDirection.esriTransformReverse, geoTransformation);
//设置转换
IRaster2 raster2 = (IRaster2)raster;
raster2.GeoTransformations = operationSet;
//保存转换记过结果
ISaveAs saveas = (ISaveAs)raster;
saveas.SaveAs(@"c:\temp\outputRaster.img", null, "IMAGINE Image");
}
创建空间参考系,导出,导入等操作判断两个空间参考系是否相同
/// <summary>
/// 本代码介绍了定义一个投影坐标系,定义格网、容差、值等。然后介绍了导入导出两种方式。导出是讲参照系找出成一个*.prj文件,
/// 然后应用*.prj文件内容创建一个坐标系
///
/// </summary>
private void ImportExportSR_Example()
{
// 创建参照系工作空间
Type factoryType = Type.GetTypeFromProgID(
"esriGeometry.SpatialReferenceEnvironment");
System.Object obj = Activator.CreateInstance(factoryType);
ISpatialReferenceFactory3 spatialReferenceFactory = obj as
ISpatialReferenceFactory3;
// 创建投影参照系UTM_10,椭球为WGS84椭球.
IProjectedCoordinateSystem projectedCoordinateSystem =
spatialReferenceFactory.CreateProjectedCoordinateSystem((int)
esriSRProjCSType.esriSRProjCS_WGS1984UTM_10N);
ISpatialReferenceResolution spatialReferenceResolution =
projectedCoordinateSystem as ISpatialReferenceResolution;
ISpatialReferenceTolerance spatialReferenceTolerance = projectedCoordinateSystem
as ISpatialReferenceTolerance;//设置容差默认0.001米
spatialReferenceResolution.ConstructFromHorizon();
spatialReferenceTolerance.SetDefaultXYTolerance();
// 导出参照系
String fileName = "c:\\temp\\utm10.prj";
spatialReferenceFactory.ExportESRISpatialReferenceToPRJFile(fileName,
projectedCoordinateSystem);
// 根据投影文件在创建一个新的坐标系
ISpatialReference projectedCoordinateSystem2 =
spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(fileName);
// 检查两个坐标系是否相同
IClone comparison = projectedCoordinateSystem as IClone;
System.Windows.Forms.MessageBox.Show((comparison.IsEqual
(projectedCoordinateSystem2 as IClone)).ToString());
ISpatialReference2 comparePrecisions = projectedCoordinateSystem as
ISpatialReference2;
// Should be false, .prj files do not persist coordinate grid information.
System.Windows.Forms.MessageBox.Show((comparePrecisions.IsXYPrecisionEqual
(projectedCoordinateSystem2)).ToString());
}
修改地图的空间参考
///<summary>Use the SpatialReferenceDialog to change the coordinate system or spatial reference of the map.</summary>
///
///<param name="hWnd">The application window handle.</param>
///<param name="map">An IMap interface.</param>
///
///<remarks></remarks>
public void ChangeMapSpatialReference(System.Int32 hWnd, ESRI.ArcGIS.Carto.IMap map)
{
if(map == null)
{
return;
}
ESRI.ArcGIS.CatalogUI.ISpatialReferenceDialog2 spatialReferenceDialog = new ESRI.ArcGIS.CatalogUI.SpatialReferenceDialogClass();
ESRI.ArcGIS.Geometry.ISpatialReference spatialReference = spatialReferenceDialog.DoModalCreate(true, false, false, hWnd);
if ((!(map.SpatialReferenceLocked)))
{
map.SpatialReference = spatialReference;
}
}