改变空间参考,文本框显示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/archive/2012/11/07/2759274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值