首页 > 编程学习 > arcengine实现挖填方Cut Fill分析以及读取栅格值

    arcengine实现挖填方Cut Fill分析以及读取栅格值

       stirng demCutfill="";//分析后的挖填方文件

        private void CutFill()
        {
            try
            {
                Random rand = new Random();
                 string dem = "BTFX" + rand.Next();//不能用中文名称,并且为局部变量,否则分析很可能失败
                Geoprocessor GP = new Geoprocessor();
                ESRI.ArcGIS.SpatialAnalystTools.CutFill cuffill = new ESRI.ArcGIS.SpatialAnalystTools.CutFill();
                cuffill.in_before_surface = “”;//旧的dem
                cuffill.in_after_surface = “”;//新的dem
                cuffill.out_raster =  "D:\\" + dem; ; //对比变化
                GP.ToolExecuted += new EventHandler<ToolExecutedEventArgs>(executedCutfill);
                GP.ExecuteAsync(cuffill);
                 demCutfill= dem;//文件赋值,须放在执行分析后面,否则分析很可能失败
            }
            catch (Exception ex)
            {
                MessageBox.Show("分析失败!\n1.选择的不是有效影像数据;\n2.两个文件格式保持一致;\n"+ex.Message);
            }

        }

      //分析完成

        private void executedCuTfill(object e, ToolExecutedEventArgs ToolExecuted)
        {
            try
            {
                getReadDataset(CommonVariable.tempShpPath, demCullfill);
            }catch(Exception ex){
                MessageBox.Show(ex.Message);
            }
            FrmstatuCuffill.Close();
           

        }

       //读取栅格值

        private void getReadDataset(string path ,string name )
        {
            IRasterDataset rastDataset;
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
            IWorkspace workspace;
            workspace = workspaceFactory.OpenFromFile(path, 0); //inPath栅格数据存储路径
            if (workspace == null)
            {
                return;
            }
            IRasterWorkspace rastWork = (IRasterWorkspace)workspace;

            rastDataset = rastWork.OpenRasterDataset(name);//inName栅格文件名
            if (rastDataset == null)
            {
                return;
            }
            IRasterProps rasterProps = (IRasterProps)rastDataset.CreateDefaultRaster();
            IGeoDataset pGeodataset = rastDataset as IGeoDataset;
            IRasterBandCollection pRsBandCol = pGeodataset as IRasterBandCollection;
            double area = 0.0;//涉及面积
            double volume = 0.0;//填方方量
            for (int i = 0; i < pRsBandCol.Count; i++)
            {
                IRasterBand pBand = pRsBandCol.Item(0);
                ITable pRTable = pBand.AttributeTable;
                ICursor pCursor = pRTable.Search(null, false);
                //获取像素块中的值
                IRow pRrow = pCursor.NextRow();
                while (pRrow != null)
                {
                  //  for (int t = 0; t < pRrow.Fields.FieldCount; t++)
                   // {
                       // string v = pRrow.get_Value(pRrow.Fields.FindField("AREA")).ToString();
                     
                  //  }
                     area+=Convert.ToDouble( pRrow.get_Value(pRrow.Fields.FindField("AREA")).ToString());
                     volume += Convert.ToDouble(pRrow.get_Value(pRrow.Fields.FindField("VOLUME")).ToString());
                    pRrow = pCursor.NextRow();
                }
            }
           string countVolume = Math.Round(volume,2) + "m³";
            string countArea= Math.Round(area,2) + "㎡";
        }
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000