C#+Arcengine實現GP工具中Data Management Tool》raster》raster processing中的clip功能(向量資料對柵格資料的裁剪)

chhdxzq發表於2015-01-28

Arcgisdesktop中Data Management Tool中有一個clip的功能,即可以實現根據向量資料的範圍對柵格資料的裁剪的功能,在這裡它有一個可選項use input features for Clipping Geometry,即是否按照所輸入shp資料的圖形範圍對柵格資料進行裁剪,根據其是否勾選,clip有兩種實現方法,具體如下:

1,、勾選,即按照向量資料的範圍對柵格資料進行裁剪(裁剪後輸出範圍和輸入的shp相同):

 /// <summary>

        /// 按shp範圍裁剪Raster資料
        /// </summary>
        /// <param name="inShp">輸入shp資料</param>
        /// <param name="inRaster">輸入Raster資料</param>
        private string ClipDem(string inShp, string inRaster)
        {
            try
            {
                //呼叫GP工具

                Geoprocessor clipGeoprocessor = new Geoprocessor();

                 //這個工作空間大家電腦上都有,可以都寫這個,不必更換路徑(只要你裝了arcgis)

                clipGeoprocessor.SetEnvironmentValue("workspace", @"C:\Users\TOMATO\Documents\ArcGIS\Default.gdb");

                clipGeoprocessor.OverwriteOutput = true;


               //要儲存的裁剪raster名稱

                string clipRasterName = "clipRaster.tif";

               //路徑+raster名稱(newFolder是你想儲存裁剪影像的路徑)

                string clipRaster = newFolder + "\\" + clipRasterName;


                //判斷此柵格檔案是否存在,若存在,刪除
                if (File.Exists(clipRaster))
                {
                    File.Delete(clipRaster);

                }


                //初始化clip工具
                Clip newClipRaster = new Clip()
                {
                    in_raster = inRaster,   // 輸入柵格資料(路徑+名稱)
                    out_raster = clipRaster, //要輸出的裁剪柵格資料的路徑+名稱
                    clipping_geometry = "ClippingGeometry",   //注意,此處就是arcgis中use input features for Clipping Geometry勾選的狀態,不勾選為none
                    rectangle = "#",    //此處為輸出矩形的範圍,注意,這裡若給出範圍,則裁剪影象按給出的範圍輸出,不管你上面的勾是否勾選
                    in_template_dataset = inShp // 輸入的向量資料的路徑+名稱(應該必須為polygon,未驗證)

                };


                IGeoProcessorResult result = (IGeoProcessorResult)clipGeoprocessor.Execute(newClipRaster, null);

              //判斷是否裁剪成功

                if (!File.Exists(clipRaster))
                {
                    return null;
                }
                return clipRaster;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }

        }


2、不勾選,即按照向量資料的外包矩形的範圍對柵格資料進行裁剪(裁剪後輸出的範圍是輸入shp影象的外包矩形的範圍):


         /// <summary>    

      /// 按shp外包矩形範圍裁剪Raster資料
        /// </summary>
        /// <param name="inShp">輸入shp資料</param>
        /// <param name="inRaster">輸入Raster資料</param>
        private string ClipDem(string inShp, string inRaster)
        {
            try
            {
                        //呼叫GP工具
                        Geoprocessor clipGeoprocessor = new Geoprocessor();
                        clipGeoprocessor.OverwriteOutput = true;


                        //建立工作空間
                        IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
                        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(shapefilePath, 0);
                      

                     ////得到使用者所選shp檔案中的featureClass
                        IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(inShpName));
                      
                        //得到feature,進而得到shp外包矩形的範圍
                        IFeatureCursor featureCursor = featureClass.Search(null, false);
                        IFeature feature = featureCursor.NextFeature();
                        while (feature != null)
                        {

                            //此處即為shp外包矩形的範圍
                           rectangle = string.Format("{0} {1} {2} {3}", feature.ShapeCopy.Envelope.XMin, feature.ShapeCopy.Envelope.YMin, feature.ShapeCopy.Envelope.XMax,  feature.ShapeCopy.Envelope.YMax);
                            feature = featureCursor.NextFeature();
                        }


                        string clipRasterName = "clipRaster.tif";//要輸出的裁剪raster名稱
                        string outputrasterpath = staruoPath + "\\" + clipRasterName;     //路徑+raster名稱
                        //判斷此檔案是否存在,若存在,刪除
                        if (File.Exists(outputrasterpath))
                        {
                            File.Delete(outputrasterpath);
                        }


                        //初始化clip工具
                        Clip clipRaster = new Clip()
                        {
                            in_raster = inRaster,//同上
                            out_raster = outputrasterpath,//同上
                            clipping_geometry = "none",//此處即為不勾選的情況
                            rectangle =rectangle  //輸出範圍為外包矩形

                       
                        };
                        IGeoProcessorResult result = (IGeoProcessorResult)clipGeoprocessor.Execute(clipRaster, null);
                        if (File.Exists(outputrasterpath))
                        {
                            MessageBox.Show("裁剪成功!");
                        }
                    }
              
            
            }
            catch (Exception ex)
            {
                MessageBox.Show("失敗!");
                Console.WriteLine(ex.Message);
            }

}


注意,輸入shp檔案的路徑+名稱和要輸入柵格資料的路徑+名稱,一定要寫正確,不然GP工具呼叫的時候容易報出“對com組建的呼叫返回了錯誤的HRESULT_FAIL”這個錯誤,如確定輸入路徑正確,還是報錯,可以參考http://llsshh1985.blog.163.com/blog/static/14006860320145174550560/(arcgisengine中呼叫GP報錯的問題)


2015-12-22,最近又用到這個功能,發現有的影像用以上的程式碼並沒有按照多邊形的輪廓裁剪影像,輸出結果還是多邊形的外包矩形,經研究發現,當影像中有大量Nodata值時,clip gp裁剪需新增 nodata_value = "-9999"程式碼,原因猜測是存在大量Nodata的畫素值,需對其進行賦值才能根據多邊形的輪廓裁剪出相應的影像。


相關文章