C#+ArcEngine:加载打开Shp矢量数据和栅格数据(VS窗体+代码)

时间:2024-03-30 19:51:04
C#+ArcEngine:加载打开Shp矢量数据和栅格数据(VS2010窗体+代码)C#+ArcEngine:加载打开Shp矢量数据和栅格数据(VS2010窗体+代码)C#+ArcEngine:加载打开Shp矢量数据和栅格数据(VS2010窗体+代码)using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesRaster;



namespace 加载打开Shp矢量和栅格数据
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            InitializeComponent();
        }


        //打开Shp矢量数据
        private void 打开矢量数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog xjOpenShpFileDialog = new OpenFileDialog();
            xjOpenShpFileDialog.Title = "打开矢量数据";
            xjOpenShpFileDialog.Filter = "矢量文件(*.shp)|*.shp";


            if (xjOpenShpFileDialog.ShowDialog() == DialogResult.OK)
            {
                string xjShpPath = xjOpenShpFileDialog.FileName;
                string xjShpFolder = System.IO.Path.GetDirectoryName(xjShpPath);
                string xjShpFileName = System.IO.Path.GetFileName(xjShpPath);
                //工作工厂+工作空间
                IWorkspaceFactory xjShpWsF = new ShapefileWorkspaceFactory();
                IFeatureWorkspace xjShpFWs = (IFeatureWorkspace)xjShpWsF.OpenFromFile(xjShpFolder, 0);
                //新建矢量图层:要素+名称
                IWorkspace xjShpWs = xjShpWsF.OpenFromFile(xjShpFolder, 0);
                IFeatureClass xjShpFeatureClass = xjShpFWs.OpenFeatureClass(xjShpFileName);
                IFeatureLayer xjShpFeatureLayer = new FeatureLayer();
                xjShpFeatureLayer.FeatureClass = xjShpFeatureClass;
                xjShpFeatureLayer.Name = xjShpFeatureClass.AliasName;
                //加载刷新
                this.axMapControl1.AddLayer(xjShpFeatureLayer);
                this.axMapControl1.ActiveView.Refresh();
            }
        }


        //打开栅格数据
        private void 打开栅格数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.axMapControl1.ActiveView.Clear();//清空当前
            OpenFileDialog xjRasterOpenFileDialog = new OpenFileDialog();
            xjRasterOpenFileDialog.Title = "打开栅格数据";
            xjRasterOpenFileDialog.Filter = "栅格数据(*.tiff;*.tif;*.jpep;*.jpg;*.png;*.bmp)|*.tiff;*.tif;*.jpep;*.jpg;*.png;*.bmp";


            if (xjRasterOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                string xjRasterPath = XjRasterOpenFileDialog.FileName;
                string xjRasterFolder = System.IO.Path.GetDirectoryName(xjRasterPath);
                string xjRasterFileName = System.IO.Path.GetFileName(xjRasterPath);
                //工作空间(实例化)
                IWorkspaceFactory xjRasterWsF = new RasterWorkspaceFactory();
                IWorkspace xjRasterWs = RasterWsF.OpenFromFile(xjRasterFolder, 0);
                IRasterWorkspace xjRasterWs = xjRasterWs as IRasterWorkspace;//强制转换
                IRasterDataset xjRasterDS = IRasterWs.OpenRasterDataset(xjRasterFileName);
                //影像金字塔的判断和创建(可以没有)
                IRasterPyramid xjRasterPyramid = xjRasterDS as IRasterPyramid;
                if (xjRasterPyramid != null)
                {
                    if (!(xjRasterPyramid.Present))
                    {
                        xjRasterPyramid.Create();
                    }
                }
                //新建栅格图层
                IRasterLayer xjRasterLayer = new RasterLayer();//引用Carto
                xjRasterLayer.CreateFromRaster(xjRasterDS.CreateDefaultRaster());
                //加载显示
                this.axMapControl1.AddLayer(xjRasterLayer, 0);
                this.axMapControl1.ActiveView.Refresh();
            }
        }
    }

}


VS2010+ArcEngine10.1具体窗体+代码见:点击打开链接