改变空间参考,文本框显示prj文件内容

prj文件的读取是通过IESRISpatialReferenceGEN2接口的ExportToESRISpatialReference2()函数来实现,做法是把prj文件读到缓冲的string中,然后可以通过textBox来显示,以下是完整代码:textBox1和textBox2是两个文本框。

private void SelectSRP_Click(object sender, EventArgs e)
        {

            ISpatialReferenceFactory3 pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            string str = "";

            //没有图层就提示加载图层
            MainFrm mainFrm1 = System.Windows.Forms.Application.OpenForms[0] as MainFrm;
            IMap pMap = mainFrm1.axMapControl1.Map;
            if (pMap.LayerCount < 1)
            {
                MessageBox.Show("请先加载图层!");
                return;
            }

            IFeatureLayer pLayer;
            pLayer = pMap.get_Layer(0) as IFeatureLayer;  //实现了从ILayer到IFeatureLayer的跳转,因为有一个共同的类:FeatureLayer
            IFeatureClass pFeatureClass;
            pFeatureClass = pLayer.FeatureClass;
            //接口跳转,从要素类得到数据集,IGeoDataset和IFeatureClass有一个共同的类:FeatureClass   
            IGeoDataset pGeoDataset;
            pGeoDataset = pFeatureClass as IGeoDataset;
            //接口跳转,从地理数据集得到GeoDatasetSchemaEdit,IgeoDataset和IGeoDatasetSchemaEdit有一个共同的类:FeatureDataset
            IGeoDatasetSchemaEdit pGeoDatasetEdit;
            pGeoDatasetEdit = pGeoDataset as IGeoDatasetSchemaEdit;

            //按选择的文件来建立空间参考
            OpenFileDialog ogeoSRP = new OpenFileDialog();
            ogeoSRP.Filter = "坐标参考系(*.prj)|*.prj";
            ogeoSRP.InitialDirectory = @"C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems";
            ogeoSRP.Multiselect = false;
            ogeoSRP.ShowDialog();   
            str = ogeoSRP.FileName;
            //如果用户没有选择则返回
            if (str == string.Empty)
            {
                return;
            }
            //选择了投影文件txtBox1上显示投影文件名称
            textBox1.Text = System.IO.Path.GetFileNameWithoutExtension(str);
            //this"str" for  CreateESRISpatialReferenceFromPRJFile Focuntion
            str = System.IO.Path.GetFullPath(str);

            if (pGeoDatasetEdit.CanAlterSpatialReference == true)
            {
                ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(str);
                try
                {
                    pGeoDatasetEdit.AlterSpatialReference(pSpatialReference);
                    MessageBox.Show("改变空间参考成功!目前的空间参考是" + pSpatialReference.Name, "提示");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("改变空间参考不成功!");
                    return;
                }
                //把空间参考的信息写到字符串中
                int bytes = 0;
                string buffer = null;
                IESRISpatialReferenceGEN2 parameterExport = pSpatialReference as IESRISpatialReferenceGEN2;
                parameterExport.ExportToESRISpatialReference2(out buffer,out bytes);
                //在文本框中显示
                textBox2.Text = buffer;
            }

            //刷新地图
            IActiveView pActiveView = pMap as IActiveView;
            pActiveView.Refresh(); 

}        

转载请注明出处。

原文地址:https://www.cnblogs.com/chuang8/p/2759274.html