1. 程式人生 > >根據離散點內插生成等值線(c#+ArcGIS Engine )完整

根據離散點內插生成等值線(c#+ArcGIS Engine )完整

幾點說明:
根據離散點生成等值線:
1.可判斷等值線shapefile圖層臨時存放路徑是否存在,如果不存在則建立,如果存在並不為空則刪除重建;
2.可加【降雨量】欄位並賦值,生成等值線後刪除所加的欄位;
3.根據所選的欄位利用IDW內插生成raster,再設定間距生成contour並標註。
---------------------------------以下是原始碼----------------------------------
private void button1_Click(object sender, EventArgs e)
        {            
            //定義等值線臨時shapefile圖層存放路徑
            string ls_TempSavePath = @"C:\HNYTTEMP" ;

            if (!Directory.Exists(ls_TempSavePath))//如果目錄不存在,則建立
                Directory.CreateDirectory(ls_TempSavePath);

            DirectoryInfo di = new DirectoryInfo(ls_TempSavePath);
            FileInfo[] fi = null;
            fi = di.GetFiles();
            if (fi.Length != 0)//如果目錄不為空,則刪除目錄,然後重建
            {
                Directory.Delete(ls_TempSavePath, true);
                Directory.CreateDirectory(ls_TempSavePath);
            }           

             IMap pMap  = axMapControl1.Map;  
             IInterpolationOp pInterpolationOp  = new RasterInterpolationOpClass();
  
             // Create the input point object
             IGeoDataset pInputDataset ;
             //雨量站圖層【點】
             IFeatureLayer pFeatLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
             // Calls function to open the point dataset from disk
  
             IFeatureClass pFeatCla  = pFeatLayer.FeatureClass;  
            
             IFeature pFeature ;  
             IField pField  = new FieldClass();
             IFieldEdit pFieldEdit = pField as IFieldEdit;
  
             pFieldEdit .Name_2 = "降雨量";
             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
             pFieldEdit.Length_2 = 12;
             pFieldEdit.IsNullable_2 = false;
             pFieldEdit.DefaultValue_2 = 1111;

            pFeatCla.AddField(pFieldEdit);   //新增那個要用的欄位進去

        for(int ii=0;ii<pFeatCla.FeatureCount(null) - 1;ii++)
            {
               pFeature = pFeatCla.GetFeature(ii);
               pFeature.set_Value(pFeature.Fields.FindField("降雨量"), 22+1999/(ii+2));
              //要素欄位賦值,這裡是自己隨便賦的值,僅做測試用
          pFeature.Store();
            }
           pInputDataset = pFeatLayer as IGeoDataset;
           // Define the search radius
           IRasterRadius pRadius  = new RasterRadiusClass();
           
           object Missing = Type.Missing;
           pRadius.SetVariable(12, ref Missing);  
           //Create FeatureClassDescriptor using a value field

          IFeatureClassDescriptor pFCDescriptor = new FeatureClassDescriptorClass();
          pFCDescriptor.Create( pFeatLayer.FeatureClass, null, "降雨量");  

          //Set cellsize for output raster in the environment
          object cellSizeProvider = 20;
   
         IRasterAnalysisEnvironment pEnv  = pInterpolationOp as IRasterAnalysisEnvironment;
         pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref  cellSizeProvider);
     
         //Perform the interpolation
         IRaster pOutRaster = pInterpolationOp.IDW(pFCDescriptor as IGeoDataset, 2, pRadius, ref Missing) as IRaster;
         
         //Add output into ArcMap as a raster layer   
         RasterLayer pOutRasLayer = new RasterLayerClass();
         pOutRasLayer.CreateFromRaster(pOutRaster);
         pOutRasLayer.Name = "柵格";
         //pMap.AddLayer(pOutRasLayer);
   
         IGeoDataset pGeoDataSet = pOutRaster as IGeoDataset;
         IWorkspaceFactory pWorkspaceFactory  = new ShapefileWorkspaceFactory();
         IWorkspace pShpWorkspace = pWorkspaceFactory.OpenFromFile(ls_TempSavePath, 0);
         ISurfaceOp2 pSurfaceOp2 = new RasterSurfaceOpClass();
         IRasterAnalysisEnvironment pRasterAnalysisEnvironment = pSurfaceOp2  as IRasterAnalysisEnvironment;

         pRasterAnalysisEnvironment.Reset();
         pRasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
         pRasterAnalysisEnvironment.OutWorkspace = pShpWorkspace;
         double  dInterval   =5;  //間距
         IGeoDataset pOutputDataSet   = pSurfaceOp2.Contour(pGeoDataSet, dInterval,ref Missing,ref Missing);

         IFeatureClass pFeatureClass=  pOutputDataSet as IFeatureClass;
         IFeatureLayer pFeatureLayer = new FeatureLayerClass();
         pFeatureLayer.FeatureClass = pFeatureClass;

         IGeoFeatureLayer pGeoFeatureLayer  = pFeatureLayer as IGeoFeatureLayer;
         pGeoFeatureLayer.DisplayAnnotation = true;
         pGeoFeatureLayer.DisplayField = "Contour";
         pGeoFeatureLayer.Name = "降雨量等值線";

         pMap.AddLayer( pGeoFeatureLayer);
         axMapControl1.Refresh();
        
         //刪除【降雨量】欄位
      IFields pFields = pFeatCla.Fields;
         int lFieldnumber = pFields.FindField("降雨量");
         IField pField1 = pFields.get_Field(lFieldnumber);
         pFeatCla.DeleteField(pField);
        }