天天看点

ArcEngine为数据添加坐标参考

版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/16117115

为数据添加坐标参考的方式有很多,下面介绍两种方式进行设置。

1、使用IGeoDatasetSchemaEdit接口进行修改。

直接上代码:

public static bool DefineProject(string datasetFile, string tempDatasetFile)
{
    IGPUtilities myGpUt = new GPUtilitiesClass();
    IGeoDataset geoDataset = (IGeoDataset)myGpUt.OpenDatasetFromLocation(datasetFile);
    IGeoDataset tempGeoDataset = (IGeoDataset)myGpUt.OpenDatasetFromLocation(tempDatasetFile);
    if (geoDataset == null || tempGeoDataset == null)
    {
        return false;
    }
   
    ISpatialReference spatialReference = tempGeoDataset.SpatialReference;
    if (spatialReference.Name == "Unknown")
    {
        return false;
    }
 
    IGeoDatasetSchemaEdit geoDatasetSchemaEdit = (IGeoDatasetSchemaEdit)geoDataset;            
    if (!geoDatasetSchemaEdit.CanAlterSpatialReference)
    {
        return false;
    }
 
    geoDatasetSchemaEdit.AlterSpatialReference(spatialReference);

    return true;
}           

2、调用GP工具进行修改

public bool GP_DefineProjection(string sNoProjectDataPath, ISpatialReference pSpatialReference)
        {
            try
            {
                //创建工具
                DefineProjection myDefFeatureProject = new DefineProjection();
                myDefFeatureProject.in_dataset = sNoProjectDataPath;
                myDefFeatureProject.coor_system = pSpatialReference;

                m_pResult = (IGeoProcessorResult)m_Gp.Execute(myDefFeatureProject, null);

                if (m_pResult == null)
                {
                    //执行出错
                    return false;
                }

                if (m_pResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                {
                    //执行成功
                    return true;
                }
                else
                {
                    //执行失败
                    return false;
                }

                return true;
            }
            catch (System.Exception ex)
            {
                //执行出现异常
                return false;
            }
        }