ARCGIS Engine空间参照系处理

题记:本文部分内容为转载,部分内容为原创


1.创建空间参考

[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 根据prj文件创建空间参考  
  3. /// </summary>  
  4. /// <param name="strProFile">空间参照文件</param>  
  5. /// <returns></returns>  
  6. public static ISpatialReference CreateSpatialReference(string strProFile)  
  7. {  
  8.     ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();  
  9.     ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(strProFile);  
  10.     return pSpatialReference;             
  11. }  
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 创建地理坐标系  
  3. /// </summary>  
  4. /// <param name="gcType">esriSRProjCS4Type</param>  
  5. /// <returns></returns>  
  6. public static ISpatialReference CreateGeographicCoordinate(esriSRProjCS4Type gcsType)  
  7. {  
  8.     ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();  
  9.     ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)gcsType);  
  10.     return pSpatialReference;  
  11. }  
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 创建投影坐标系  
  3. /// </summary>  
  4. /// <param name="pcType">esriSRProjCS4Type</param>  
  5. /// <returns></returns>  
  6. public static ISpatialReference CreateProjectedCoordinate(esriSRProjCS4Type pcsType)  
  7. {  
  8.     ISpatialReferenceFactory2 pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();  
  9.     ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem((int)pcsType);  
  10.     return pSpatialReference;  
  11. }  
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 获取空投影  
  3. /// </summary>  
  4. /// <returns></returns>  
  5. public static ISpatialReference CreateUnKnownSpatialReference()  
  6. {  
  7.     ISpatialReference pSpatialReference = new UnknownCoordinateSystemClass();  
  8.     pSpatialReference.SetDomain(0, 99999999, 0, 99999999);//设置空间范围  
  9.     return pSpatialReference;  
  10. }  

2.获取空间参考

[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 获取要素集空间参考  
  3. /// </summary>  
  4. /// <param name="pFeatureDataset">要素集</param>  
  5. /// <returns></returns>  
  6. public static ISpatialReference GetSpatialReference(IFeatureDataset pFeatureDataset)  
  7. {  
  8.     IGeoDataset pGeoDataset = pFeatureDataset as IGeoDataset;  
  9.     ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;  
  10.     return pSpatialReference;             
  11. }  
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 获取要素层空间参考  
  3. /// </summary>  
  4. /// <param name="pFeatureLayer">要素层</param>  
  5. /// <returns></returns>  
  6. public static ISpatialReference GetSpatialReferenc(IFeatureLayer pFeatureLayer)  
  7. {IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;  
  8.     IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;  
  9.     ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;  
  10.     return pSpatialReference;             
  11. }  
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 获取要素类空间参考  
  3. /// </summary>  
  4. /// <param name="pFeatureClass">要素类</param>  
  5. /// <returns></returns>  
  6. public static ISpatialReference GetSpatialReference(IFeatureClass pFeatureClass)  
  7. {  
  8.     IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;  
  9.     ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;  
  10.     return pSpatialReference;             
  11. }  

3.修改空间参考

[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 修改要素集空间参考  
  3. /// </summary>  
  4. /// <param name="pFeatureDataset">要素集</param>  
  5. /// <param name="pSpatialReference">新空间参考</param>  
  6. public static void AlterSpatialReference(IFeatureDataset pFeatureDataset, ISpatialReference pSpatialReference)  
  7. {  
  8.     IGeoDataset pGeoDataset = pFeatureDataset as IGeoDataset;  
  9.     IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit;  
  10.     if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true)  
  11.         pGeoDatasetSchemaEdit.AlterSpatialReference(pSpatialReference);  
  12. }  
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 修改要素类空间参考  
  3. /// </summary>  
  4. /// <param name="pFeatureClass">要素类</param>  
  5. /// <param name="pSpatialReference">新空间参考</param>  
  6. public static void AlterSpatialReference(IFeatureClass pFeatureClass, ISpatialReference pSpatialReference)  
  7. {  
  8.     IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;  
  9.     IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit;  
  10.     if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true)  
  11.         pGeoDatasetSchemaEdit.AlterSpatialReference(pSpatialReference);  
  12. }  
栅格数据椭球变换
      <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; 
  }
}


 
  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值