C#+ArcEgine开发(4)三维分析

(75) 2024-04-03 11:01:01

1、新建一个窗体文件
C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第1张
主菜单栏有4个功能:数据加载、拉伸、地形渲染、坡度分析

2、数据加载功能
(1)导入DEM数据
 private void 导入DEMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ISceneGraph pSceneGraph = this.axSceneControl1.SceneGraph;
            IScene pScene = pSceneGraph.Scene;
            IRasterLayer pRasterLayer = new RasterLayerClass();
            ILayer pLayer;

            this.openFileDialog1.Title = "加载DEM数据";
            this.openFileDialog1.DefaultExt = ".TIF";
            this.openFileDialog1.Filter = "(*.tif)|*.tif";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pPathName = this.openFileDialog1.FileName;
                string pPath = pPathName.Substring(0, pPathName.LastIndexOf('\\'));
                string fileName = pPathName.Substring(pPath.Length + 1, pPathName.Length - pPath.Length - 1);
                IWorkspaceFactory pwsf = new RasterWorkspaceFactoryClass();
                IRasterWorkspace pRasterWorkspace;
                if (pwsf.IsWorkspace(pPath))
                {
                    pRasterWorkspace = pwsf.OpenFromFile(pPath, 0) as IRasterWorkspace;
                    IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(fileName);
                    //pRasterDataset.OpenFromFile(pPath);
                    pRasterLayer.CreateFromDataset(pRasterDataset);
                    pLayer = pRasterLayer as ILayer;
                    pScene.AddLayer(pLayer);
                    //pScene.ExaggerationFactor = 6;
                    axTOCControl1.Update();
                    pSceneGraph.RefreshViewers();

                }
            }
            
        }

(2)导入TIN数据

 //导入TIN数据,与加载其他数据略有不同,TIN数据包含若干文件
        private void 导入TINToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ISceneGraph pSceneGraph = this.axSceneControl1.SceneGraph;
            IScene pScene = pSceneGraph.Scene;
            ITinLayer tinLayer = new TinLayerClass();
            
            FileInfo fileInfo;
            string tinPath;
            IWorkspaceFactory tinWorkspaceFactory = new TinWorkspaceFactoryClass();
            ITinWorkspace tinWorkspace;
            ITin tin;

            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                tinPath = this.folderBrowserDialog1.SelectedPath;
                fileInfo = new FileInfo(tinPath);
                if (tinWorkspaceFactory.IsWorkspace(fileInfo.DirectoryName))
                {
                    tinWorkspace = tinWorkspaceFactory.OpenFromFile(fileInfo.DirectoryName, 0) as ITinWorkspace;
                    //tinWorkspace.OpenTin(fileInfo.DirectoryName);
                    tin = tinWorkspace.OpenTin(fileInfo.Name);
                    tinLayer.Dataset = tin;
                    tinLayer.Visible = false;
                    pScene.AddLayer(tinLayer as ILayer, true);
                    pSceneGraph.RefreshViewers();
                }
            }
        }

加载DEM的结果
C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第2张

3 拉伸功能

(1)新建一个拉伸的窗体
C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第3张

(2)在ComBox的Items属性里添加可选择的数据
C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第4张
(3)代码如下:
 public partial class stretch : Form
    {
       private threeD threed;   //获取父窗体的控件
       public stretch(threeD threed)
        {
         
            InitializeComponent();
            this.threed = threed;  //这行代码很重要        
        }

       
        private void button1_Click(object sender, EventArgs e)
        {
      
           try
            {
             ISceneGraph scenegraph = threed.axSceneControl1.SceneGraph;
             IScene scene = scenegraph.Scene;
             ILayer layer = scene.get_Layer(0);
             IRasterLayer pLayer = layer as IRasterLayer;
             IRasterSurface pSurface = new RasterSurface();
             IRaster raster = (IRaster)pLayer.Raster;
             IRasterBandCollection rasterbands = raster as IRasterBandCollection;
             IRasterBand rasterband = rasterbands.Item(0);
             pSurface.RasterBand = rasterband;
             ISurface surface = pSurface as ISurface;
             ILayerExtensions layerextensions = layer as ILayerExtensions;
             I3DProperties properties = new Raster3DPropertiesClass();
             object p3d;
             for (int i = 0; i < layerextensions.ExtensionCount; i++)
             {
                 p3d = layerextensions.get_Extension(i);
                 if (p3d != null)
                 {
                     properties = (I3DProperties)p3d;
                     break;
                 }
             }
                properties.ZFactor = Convert.ToDouble(comboBox1.Text);
                properties.BaseOption = esriBaseOption.esriBaseSurface;
                properties.BaseSurface = surface;
                properties.Apply3DProperties(layer);
                threed.axSceneControl1.SceneGraph.RefreshViewers();
 

         }
         catch
         {
             return;
         }
             
        }

        private void stretch_Load(object sender, EventArgs e)
        {

        }
    }

C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第5张

4 地形渲染功能
直接上代码
 //地形渲染
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            //Render render = new Render(this);
           //
            //render.Show();
            try
            {
                IScene scene = axSceneControl1.Scene;
                ILayer layer = scene.get_Layer(0);
                IRasterLayer pRasterLayer = layer as IRasterLayer;
                IRaster raster = (IRaster)pRasterLayer.Raster;
                IRasterBandCollection rasterbands = raster as IRasterBandCollection;
                IRasterBand rasterband = rasterbands.Item(0);
                IRasterStretchColorRampRenderer pRStretchRender = new RasterStretchColorRampRendererClass();
                //创建两个起始颜色
                IRgbColor pFromRgbColor = new RgbColorClass();
                pFromRgbColor.Red = 40;
                pFromRgbColor.Green = 30;
                pFromRgbColor.Blue = 250;
                IRgbColor pToRgbColor = new RgbColorClass();
                pToRgbColor.Red = 240;
                pToRgbColor.Green = 80;
                pToRgbColor.Blue = 40;
                //创建起始色带
                IAlgorithmicColorRamp pAlgorithmicColorRamp = new AlgorithmicColorRampClass();
                pAlgorithmicColorRamp.Size = 255;
                pAlgorithmicColorRamp.FromColor = pFromRgbColor as IColor;
                pAlgorithmicColorRamp.ToColor = pToRgbColor as IColor;
                bool btrue = true;
                pAlgorithmicColorRamp.CreateRamp(out btrue);
                //选择拉伸颜色带符号化的波段
                pRStretchRender.BandIndex = 1;
                //设置拉伸颜色带符号化所采用的颜色带
                pRStretchRender.ColorRamp = pAlgorithmicColorRamp as IColorRamp;
                IRasterRenderer pRasterRender = pRStretchRender as IRasterRenderer;
                pRasterLayer = scene.get_Layer(0) as IRasterLayer;
                pRasterRender.Raster = pRasterLayer as IRaster;
                pRasterRender.Update();
                //符号化RasterLayer
                pRasterLayer.Renderer = pRasterRender;
                //渲染的刷新
                axSceneControl1.Scene.SceneGraph.Invalidate(pRasterLayer, true, false);
                axSceneControl1.SceneViewer.Redraw(true);
                axSceneControl1.Scene.SceneGraph.RefreshViewers();
                axTOCControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, pRasterLayer, axSceneControl1.Scene.Extent);
                axTOCControl1.Update();
            }
            catch(Exception Err)
            {
                MessageBox.Show(Err.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第6张

5 坡度分析功能
(1)新建坡度分析窗体

C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第7张
(2)输入栅格代码
  string inPutPath;
        private void btnInput_Click(object sender, EventArgs e)
        {
            ISceneGraph pSceneGraph = threed.axSceneControl1.SceneGraph;
            IScene pScene = pSceneGraph.Scene;
            IRasterLayer pRasterLayer = new RasterLayerClass();
            ILayer pLayer;

            this.openFileDialog1.Title = "加载DEM数据";
            this.openFileDialog1.DefaultExt = ".TIF";
            this.openFileDialog1.Filter = "(*.tif)|*.tif";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                inPutPath = this.openFileDialog1.FileName;
                string pPath = inPutPath.Substring(0, inPutPath.LastIndexOf('\\'));
                string fileName = inPutPath.Substring(pPath.Length + 1, inPutPath.Length - pPath.Length - 1);
                IWorkspaceFactory pwsf = new RasterWorkspaceFactoryClass();
                IRasterWorkspace pRasterWorkspace;
                if (pwsf.IsWorkspace(pPath))
                {
                    pRasterWorkspace = pwsf.OpenFromFile(pPath, 0) as IRasterWorkspace;
                    IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(fileName);
                    //pRasterDataset.OpenFromFile(pPath);
                    pRasterLayer.CreateFromDataset(pRasterDataset);
                    pLayer = pRasterLayer as ILayer;
                    cmbRasterInput.Text = inPutPath;
                    //pScene.AddLayer(pLayer);
                    pScene.ExaggerationFactor = 6;
                    //threed.axTOCControl1.Update();
                    //pSceneGraph.RefreshViewers();

                }
            }
        }

(3)输出栅格代码

string outPutPath = "";
        private void btnOutput_Click(object sender, EventArgs e)
        {          
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "TIFF(*tif)|*.tif";
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                outPutPath = sfd.FileName.ToString();
                string fileNameExt = outPutPath.Substring(outPutPath.LastIndexOf("\\") + 1);
            }
            cmbRasterOutput.Text = outPutPath;
        }

(4)输出测量单位的Items属性
C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第8张

(5)直接上代码

  private void btnOK_Click(object sender, EventArgs e)
        {
            //定义一个栅格工作空间工厂
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            //FileInfo—提供创建、复制、删除、移动和打开文件的实力方法,获取进行坡度分析的文件名
            FileInfo fileInfo = new FileInfo(inPutPath);//字符串inPutPath是上文加载TIFF文件时传入的文件名
            FileInfo fileOut=new FileInfo(outPutPath);
            //定义获取目录的完整路径的字符串变量
            string filePath = fileInfo.DirectoryName;
            string filep = fileOut.DirectoryName;
            //定义获取文件名的字符串变量
            string fileName = fileInfo.Name;
           
            //打开指定工作空间工厂的文件名
            IRasterWorkspace workspace = workspaceFactory.OpenFromFile(filePath, 0) as IRasterWorkspace;
            IRasterWorkspace work = workspaceFactory.OpenFromFile(filep, 0) as IRasterWorkspace;
            //定义ILayer变量,获取SceneControl中第0层的影像
            ILayer layer = threed.axSceneControl1.Scene.get_Layer(0);
            //定义IRasterLayer变量,把layer强制转换成IRasterLayer
            IRasterLayer rasterLayer = layer as IRasterLayer;
            //在工作空间工厂内打开一个RasterDataset,并指定其名称
            IRasterDataset rasterDataset = workspace.OpenRasterDataset(fileName);
            //栅格表面分析
            ISurfaceOp surfaceOp = new RasterSurfaceOpClass();
            //定义了提供访问控制栅格分析环境的成员
            IRasterAnalysisEnvironment rasterAnalysisEnveronment;
            //栅格分析环境设置,设置为表面分析
            rasterAnalysisEnveronment = surfaceOp as IRasterAnalysisEnvironment;
            //设置输出的工作目录工作空间,目录
            rasterAnalysisEnveronment.OutWorkspace = work as IWorkspace;
            object zFactor = new object();
            //IGeoDtaaset—提供对成员的访问,提供有关地理数据集的信息
            IGeoDataset geoDataset, rasterGeoDataset;
            rasterGeoDataset = rasterDataset as IGeoDataset;
            //通过Slope方法,设置相应参数,获取坡度分析
            //得到坡度分析数据集

            geoDataset = surfaceOp.Slope(rasterGeoDataset, esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, Type.Missing);

            IRasterLayer pSARLayer = new RasterLayerClass();
            //当栅格数据为单个栅格数据时
            pSARLayer.CreateFromRaster(geoDataset as IRaster);
            //当栅格数据为栅格数据集时使用该代码显示
            //pSARLayer.CreateFromDataset((IRasterDataset)geoDataset);
            threed.axSceneControl1.Scene.AddLayer(pSARLayer);
            threed.axSceneControl1.Refresh();

            一个栅格数据集由一个或者多个波段(RasterBand)的数据组成,一个波段就是一个数据矩阵
            IRasterBandCollection rasterBandCollection = geoDataset as IRasterBandCollection;
            将坡度分析得到的数据存储于Workspace读取的工作目录中
            rasterBandCollection.SaveAs("slope.tif", work as IWorkspace, "TIFF");


        }

C#+ArcEgine开发(4)三维分析 (https://mushiming.com/)  第9张

THE END

发表回复