AE一些代码

来自:http://www.pudn.com/downloads153/sourcecode/graph/detail672576.html
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.IO; 
using System.Runtime.InteropServices; 
 
using ESRI.ArcGIS.esriSystem; 
using ESRI.ArcGIS.Carto; 
using ESRI.ArcGIS.Controls; 
using ESRI.ArcGIS.ADF; 
using ESRI.ArcGIS.SystemUI; 
using ESRI.ArcGIS.Geodatabase; 
using ESRI.ArcGIS.Display; 
using ESRI.ArcGIS.GeoAnalyst; 
 
using ESRI.ArcGIS.DataSourcesRaster; 
using ESRI.ArcGIS.Geometry; 
using ESRI.ArcGIS.Output;//输出栅格 
namespace RasterInterpolate 
{ 
    public sealed partial class MainForm : Form 
    { 
        #region class private members 
        private IMapControl3 m_mapControl = null; 
        private string m_mapDocumentName = string.Empty; 
        #endregion 
 
        #region class constructor 
        public MainForm() 
        { 
            InitializeComponent(); 
        } 
        #endregion 
        string[] PtName; 
        //double[] Cx; 
        //double[] Cy; 
        double[] Ptx; 
        double[] Pty; 
        double[] Railfall; 
        double[] Ptz; 
        double[] lat; 
        double[] lon; 
        double[] di;//大圆距离 
        private void MainForm_Load(object sender, EventArgs e) 
        { 
            //get the MapControl 
            m_mapControl = (IMapControl3)axMapControl1.Object; 
            m_mapControl.AddShapeFile(Application.StartupPath + @"\shp", "sheet2.shp"); 
           // m_mapControl.LoadMxFile(Application.StartupPath + @"\data\站点.mxd","",""); 
            //disable the Save menu (since there is no document yet) 
            menuSaveDoc.Enabled = false; 
 
             dt=oledata.operate(); 
             PtName = new string[dt.Rows.Count]; 
             //Cx = new double[dt.Rows.Count]; 
             //Cy = new double[dt.Rows.Count]; 
             Ptx = new double[dt.Rows.Count]; 
             Pty = new double[dt.Rows.Count]; 
             Railfall = new double[dt.Rows.Count]; 
             Ptz = new double[dt.Rows.Count]; 
             lon = new double[dt.Rows.Count]; 
             lat=new double[dt.Rows.Count]; 
             di = new double[dt.Rows.Count]; 
             Init();//数据赋值 
        } 
        //private int m_ColumnCount; 
        //private int m_RowCount; 
        //private int m_size; 
        #region Main Menu event handlers 
        private void menuNewDoc_Click(object sender, EventArgs e) 
        { 
            //execute New Document command 
            ICommand command = new CreateNewDocument(); 
            command.OnCreate(m_mapControl.Object); 
            command.OnClick(); 
        } 
 
        private void menuOpenDoc_Click(object sender, EventArgs e) 
        { 
            //execute Open Document command 
            ICommand command = new ControlsOpenDocCommandClass(); 
            command.OnCreate(m_mapControl.Object); 
            command.OnClick(); 
        } 
 
        private void menuSaveDoc_Click(object sender, EventArgs e) 
        { 
            //execute Save Document command 
            if (m_mapControl.CheckMxFile(m_mapDocumentName)) 
            { 
                //create a new instance of a MapDocument 
                IMapDocument mapDoc = new MapDocumentClass(); 
                mapDoc.Open(m_mapDocumentName, string.Empty); 
 
                //Make sure that the MapDocument is not readonly 
                if (mapDoc.get_IsReadOnly(m_mapDocumentName)) 
                { 
                    MessageBox.Show("Map document is read only!"); 
                    mapDoc.Close(); 
                    return; 
                } 
 
                //Replace its contents with the current map 
                mapDoc.ReplaceContents((IMxdContents)m_mapControl.Map); 
 
                //save the MapDocument in order to persist it 
                mapDoc.Save(mapDoc.UsesRelativePaths, false); 
 
                //close the MapDocument 
                mapDoc.Close(); 
            } 
        } 
 
        private void menuSaveAs_Click(object sender, EventArgs e) 
        { 
            //execute SaveAs Document command 
            ICommand command = new ControlsSaveAsDocCommandClass(); 
            command.OnCreate(m_mapControl.Object); 
            command.OnClick(); 
        } 
 
        private void menuExitApp_Click(object sender, EventArgs e) 
        { 
            //exit the application 
            Application.Exit(); 
        } 
        #endregion 
 
        //listen to MapReplaced evant in order to update the statusbar and the Save menu 
        private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e) 
        { 
            //get the current document name from the MapControl 
            m_mapDocumentName = m_mapControl.DocumentFilename; 
 
            //if there is no MapDocument, diable the Save menu and clear the statusbar 
            if (m_mapDocumentName == string.Empty) 
            { 
                menuSaveDoc.Enabled = false; 
                statusBarXY.Text = string.Empty; 
            } 
            else 
            { 
                //enable the Save manu and write the doc name to the statusbar 
                menuSaveDoc.Enabled = true; 
                statusBarXY.Text = System.IO.Path.GetFileName(m_mapDocumentName); 
            } 
        } 
 
        private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) 
        { 
            statusBarXY.Text = string.Format("{0}, {1}  {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4)); 
        } 
        public void CreateContourListFromShapefile()//内插生成高程信息 
        { 
            //用反距离IDW插值生成的栅格图像 
            IInterpolationOp pInterpolationOp; 
            pInterpolationOp = new RasterInterpolationOpClass(); 
            // Create the input point object 
 
            IFeatureClass pFeatureClass; 
            IFeatureLayer pFeaturelayer; 
            pFeaturelayer = this.m_mapControl.Map.get_Layer(0) as IFeatureLayer;//这里我的数据在第八层 
            pFeatureClass = pFeaturelayer.FeatureClass; 
 
            // Define the search radius 
            IRasterRadius pRadius; 
            pRadius = new RasterRadiusClass(); 
            object maxDistance = Type.Missing; 
            pRadius.SetVariable(12, ref maxDistance); 
 
            //Create FeatureClassDescriptor using a value field 
            IFeatureClassDescriptor pFCDescriptor; 
            pFCDescriptor = new FeatureClassDescriptorClass(); 
            pFCDescriptor.Create(pFeatureClass, null, "高程(米)"); 
 
            //set { cellsize for output raster in the environment 
            object dCellSize = cellSize; 
            IRasterAnalysisEnvironment pEnv; 
            pEnv = new RasterAnalysis(); 
            pEnv = pInterpolationOp as IRasterAnalysisEnvironment; 
            pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dCellSize); 
 
            object objectbarrier = Type.Missing; 
            //Perform the interpolation 
            IGeoDataset rasDataset; 
            rasDataset = pInterpolationOp.IDW((IGeoDataset)pFCDescriptor, 2, pRadius, ref objectbarrier); 
            //IRasterDataset rsDt;//这里就是调用Esri函数的地方 克里金 双线性 那些都可以的 
            //rsDt.CreateDefaultRaster(); 
            IRaster pOutRaster; 
            pOutRaster = rasDataset as IRaster; 
             
            //Add output into ArcMap as a raster layer 
            IRasterLayer pOutRasLayer; 
            
            pOutRasLayer = new RasterLayerClass(); 
            pOutRasLayer.CreateFromRaster(pOutRaster); 
            pOutRasLayer.Name = "Contour"; 
            //m_RowCount = pOutRasLayer.RowCount; 
            //m_ColumnCount = pOutRasLayer.ColumnCount; 
 
            this.m_mapControl.AddLayer(pOutRasLayer, 0);// 
 
            UsingRasterClassifyColorRampRenderer(); 
 
    
            IRawPixels rawPixels = null; 
            IRasterBandCollection rasterBandCollection; 
            IRasterProps rasterProps; 
            // QI for IRawPixels and IRasterProps     
            rasterBandCollection = (IRasterBandCollection)rasDataset; 
            rawPixels = (IRawPixels)rasterBandCollection.Item(0); 
            rasterProps = (IRasterProps)rawPixels; 
          //  int dHeight = rasterProps.Height;//当前栅格数据集的行数 
          //  int dWidth = rasterProps.Width; //当前栅格数据集的列数 
           // double dX = rasterProps.MeanCellSize().X; //栅格的宽度 
           // double dY = rasterProps.MeanCellSize().Y; //栅格的高度 
            IEnvelope extent = rasterProps.Extent; //当前栅格数据集的范围 
            //IPoint pt=new  PointClass(); 
            IPnt pixelBlockSize = null; 
            IPnt pixelBlockOrigin = null; 
            // Create pixelblock     
            pixelBlockOrigin = new DblPntClass(); 
            pixelBlockOrigin.SetCoords(0, 0); 
            IPixelBlock3 pixelBlock3; 
            pixelBlockSize = new DblPntClass(); 
            pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height); 
            pixelBlock3 = rawPixels.CreatePixelBlock(pixelBlockSize) as IPixelBlock3; 
            // Read pixelblock     
            rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); 
            // Get pixeldata array      
            System.Array pixelData; 
            pixelData = (System.Array)pixelBlock3.get_PixelDataByRef(0); 
           // IRasterDataset RsDateset; 
            createFileRasterDataset("c:\\temp", RainfallValue, extent.LowerLeft, rasterProps.Width, rasterProps.Height, pixelData); 
 
            //IRasterLayer pRasterLayer = new RasterLayerClass(); 
            //pRasterLayer.CreateFromDataset(RsDateset); 
            //ILayer pLayer; 
            //pLayer = pRasterLayer; 
            //pLayer.Name = "Result"; 
            //axMapControl1.Map.AddLayer(pLayer); 
            //axMapControl1.ActiveView.Refresh(); 
             
            UsingRasterClassifyColorRampRenderer();//这里是要创建刚另一个图层(结果) 在c:\temp 
            //IWorkspaceFactory pWSF;//保存栅格 
            //IWorkspace pWS; 
            //pWSF = new RasterWorkspaceFactory(); 
            //pWS = pWSF.OpenFromFile("c:\\temp", 0); 
            //IRaster raster; 
            //raster = RsDateset.CreateDefaultRaster(); 
            //ISaveAs isa; 
            //isa = raster as ISaveAs; 
            //isa.SaveAs("test2.tif",pWS, "TIFF"); 
        } 
        public int cellSize; 
        public int classifyNum; 
        public string RainfallValue; 
        private void gIDS法ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FrmSet frmSet = new FrmSet(); 
             
            if(DialogResult.OK==frmSet.ShowDialog()) 
            { 
                classifyNum = frmSet.classifyNum; 
                cellSize = frmSet.cellSize; 
                RainfallValue=frmSet.RailfallValue; 
                if (RainfallValue=="降雨量50") 
                { 
                    Cx = 0.0006; Cy = 0.0005; Cz = 0.4175; 
                } 
                else if (RainfallValue == "降雨量100") 
                { 
                    Cx = 0.0007; 
                    Cy = 0.0006; 
                    Cz = 0.5199; 
                } 
                else if (RainfallValue == "降雨量500") 
                { 
                    Cx = 0.001; 
                    Cy = 0.0009; 
                    Cz = 0.769; 
                } 
                else if (RainfallValue == "降雨量1000") 
                { 
                    Cx = 0.0011; 
                    Cy = 0.001; 
                    Cz = 0.8789; 
                } 
                ResetRailfall(); 
                CreateContourListFromShapefile(); 
            } 
 
        } 
        
        
 
        public static IColor ConvertColorToIColor(Color color) 
        { 
 
            IColor pColor = new RgbColorClass(); 
            pColor.RGB = (int)color.R | (int)(color.G) < 8 | (int)(color.B) < 16; 
            return pColor; 
        }  
    
        private void UsingRasterClassifyColorRampRenderer() //分级渲染  
        { 
            //get { Map 
             
            IMap pMap; 
            pMap = m_mapControl.Map; 
            //get { raster input from layer 
            IRasterLayer pRLayer; 
            pRLayer = (IRasterLayer)pMap.get_Layer(0); 
            IRaster pRaster; 
            pRaster = pRLayer.Raster; 
 
            //Create classfy renderer and QI RasterRenderer interface 
            IRasterClassifyColorRampRenderer pClassRen; 
            pClassRen = new RasterClassifyColorRampRenderer(); 
            IRasterRenderer pRasRen; 
            pRasRen = pClassRen as IRasterRenderer; 
 
            //set { raster for the render and update 
            pRasRen.Raster = pRaster; 
            pClassRen.ClassCount = classifyNum; 
            pRasRen.Update(); 
 
            //Create a color ramp to use 
            IAlgorithmicColorRamp pRamp; 
            pRamp = new AlgorithmicColorRamp(); 
            Color clr = new Color(); 
            clr = Color.FromArgb(255, 255, 128); 
            pRamp.FromColor = ConvertColorToIColor(clr); 
            clr = Color.FromArgb(135, 42, 8); 
            pRamp.ToColor = ConvertColorToIColor(clr); 
            pRamp.Size = 10; 
            bool flag = true; 
            pRamp.CreateRamp(out flag); 
 
            //Create symbol for the classes 
            IFillSymbol pFSymbol; 
            pFSymbol = new SimpleFillSymbol(); 
 
            //loop through the classes and apply the color and label 
            int I; 
            for (I = 0; I = pClassRen.ClassCount - 1; I++) 
            { 
                pFSymbol.Color = pRamp.get_Color(I);//  .Color(I); 
                pClassRen.set_Symbol(I, pFSymbol as ISymbol); 
                pClassRen.set_Label(I, "等级" + Convert.ToString(I + 1)); 
            } // I 
 
            //Update the renderer and plug into layer 
            pRasRen.Update(); 
            pRLayer.Renderer = (IRasterRenderer)pClassRen; 
            m_mapControl.ActiveView.Refresh(); 
            // m_mapControl; 
            this.axTOCControl1.Update(); 
 
            //Release memeory 
 
            pMap = null; 
            pRLayer = null; 
            pRaster = null; 
            pRasRen = null; 
            pClassRen = null; 
            pRamp = null; 
            pFSymbol = null; 
        } 
 
        private void rasterDataToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            ExportImage(this.m_mapControl.ActiveView); 
        } 
        private void ExportImage(IActiveView pActiveView) 
        { 
            SaveFileDialog pSaveFileDialog = new SaveFileDialog(); 
            pSaveFileDialog.Filter = "JPEG(*.jpg)|*.jpg|AI(*.ai)|*.ai|BMP(*.BMP)|*.bmp|EMF(*.emf)|*.emf|GIF(*.gif)|*.gif|PDF(*.pdf)|*.pdf|PNG(*.png)|*.png|EPS(*.eps)|*.eps|SVG(*.svg)|*.svg|TIFF(*.tif)|*.tif"; 
            pSaveFileDialog.Title = "输出地图"; 
            pSaveFileDialog.RestoreDirectory = true; 
            pSaveFileDialog.FilterIndex = 1; 
 
            if (pSaveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) 
            { 
                return; 
            } 
            string sFilePath = pSaveFileDialog.FileName; 
            string sPathName = System.IO.Path.GetDirectoryName(sFilePath); 
            string sFileName = System.IO.Path.GetFileNameWithoutExtension(sFilePath); 
            IExport pExporter = null; 
 
            switch (pSaveFileDialog.FilterIndex) 
            { 
 
                case 1: 
                    pExporter = new ExportJPEGClass(); 
                    break; 
                case 2: 
                    pExporter = new ExportBMPClass(); 
                   break; 
                case 3: 
                    pExporter = new ExportEMFClass(); 
                    break; 
                case 4: 
                    pExporter = new ExportGIFClass(); 
                    break; 
                case 5: 
 
                    pExporter = new ExportAIClass(); 
                    break; 
                case 6: 
                    pExporter = new ExportPDFClass(); 
                    break; 
                case 7: 
                    pExporter = new ExportPNGClass(); 
                    break; 
                case 8: 
                    pExporter = new ExportPSClass(); 
                    break; 
                case 9: 
                    pExporter = new ExportSVGClass(); 
                    break; 
                case 10: 
                    pExporter = new ExportTIFFClass(); 
                    break; 
                default: 
                    MessageBox.Show("输出格式错误"); 
                    return; 
            } 
            IEnvelope pEnvelope = new EnvelopeClass(); 
            ITrackCancel pTrackCancel = new CancelTrackerClass(); 
            tagRECT ptagRECT; 
            ptagRECT.left = 0; 
            ptagRECT.top = 0; 
            ptagRECT.right = (int)pActiveView.Extent.Width; 
            ptagRECT.bottom = (int)pActiveView.Extent.Height; 
            int pResolution = (int)(pActiveView.ScreenDisplay.DisplayTransformation.Resolution); 
            pEnvelope.PutCoords(ptagRECT.left, ptagRECT.bottom, ptagRECT.right, ptagRECT.top); 
 
            pExporter.Resolution = pResolution; 
            pExporter.ExportFileName = sFilePath; 
            pExporter.PixelBounds = pEnvelope; 
            pActiveView.Output(pExporter.StartExporting(), pResolution, ref ptagRECT, pActiveView.Extent, pTrackCancel); 
            pExporter.FinishExporting(); 
        } 
       // IRasterDataset 
        private void createFileRasterDataset(string directoryName, string fileName, IPoint originPoint, int rowCount, int columCount, System.Array CountourpixelVale)//创建输出结果图层 
        { 
            //The file must not be there. Otherwise, dataset can not be got. 
            if (File.Exists(directoryName + fileName)) 
            { 
                MessageBox.Show("File Exist!"); 
                IRasterLayer pRasterLayer = new RasterLayerClass(); 
                pRasterLayer.CreateFromFilePath(directoryName + fileName); 
                ILayer pLayer; 
                pLayer = pRasterLayer; 
                pLayer.Name = "Result"; 
                axMapControl1.Map.AddLayer(pLayer); 
                axMapControl1.ActiveView.Refresh(); 
                return;// null; 
            } 
            System.IO.FileStream fs = File.Create("C:\\temp\\" + RainfallValue + "均方差.txt"); 
            StreamWriter sw = new StreamWriter((System.IO.Stream)fs); 
            sw.WriteLine("站点名,x坐标,y坐标," + RainfallValue + "均方差"); 
            // This function creates a new img file in the given workspace   
            // and then assigns pixel values 
            try 
            { 
                IRasterDataset rasterDataset = null; 
               // double cellSize = 500.0;//栅格大小xugy 
              //  IPoint originPoint = new PointClass(); 
                double minX = originPoint.X - cellSize / 2; 
                double minY = originPoint.Y - cellSize / 2; 
                //double minX=GetMinX(); 
                //double minY=GetMinY(); 
                //double maxX=GetMaxX(); 
                //double maxY=GetMaxY(); 
                double minLon = GetMinLon(); 
                double minLat = GetMinLat(); 
                double maxLon = GetMaxLon(); 
                double maxLat = GetMaxLat(); 
               // originPoint.PutCoords(minX - cellSize / 2, minY - cellSize / 2); 
                // Create the dataset    
                IRasterWorkspace2 rasterWorkspace2 = null; 
                // IGeographicCoordinateSystem m_GeographicCoordinateSystem;  
                ISpatialReference SpRef = new UnknownCoordinateSystem() as ISpatialReference; 
               // IProjectedCoordinateSystem m_ProjectedCoordinateSystem; 
                //ISpatialReferenceFactory2 spatRefFact = new SpatialReferenceEnvironmentClass(); 
                //m_ProjectedCoordinateSystem = spatRefFact.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_49N); 
                rasterWorkspace2 = createRasterWorkspace(directoryName); 
 
                 
               // int rowCount = (int)Math.Round((maxX - minX)/cellSize)+1; 
             //   int columCount = (int)Math.Round((maxY - minY) / cellSize)+1; 
                //经纬度计算 
                double dLon = (maxLon-minLon)/rowCount; 
                double dlat=(maxLat-minLat)/columCount; 
                rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "GRID", originPoint, rowCount, columCount, cellSize, cellSize, 1, rstPixelType.PT_FLOAT, SpRef, true);//"IMAGINE Image",rstPixelType.PT_UCHAR,new UnknownCoordinateSystemClass() 
                IRawPixels rawPixels = null; 
                IPixelBlock3 pixelBlock3 = null; 
                IPnt pixelBlockOrigin = null; 
                IPnt pixelBlockSize = null; 
                IRasterBandCollection rasterBandCollection; 
                IRasterProps rasterProps; 
                // QI for IRawPixels and IRasterProps     
                rasterBandCollection = (IRasterBandCollection)rasterDataset; 
                rawPixels = (IRawPixels)rasterBandCollection.Item(0); 
                rasterProps = (IRasterProps)rawPixels; 
 
                // Create pixelblock     
                pixelBlockOrigin = new DblPntClass(); 
                pixelBlockOrigin.SetCoords(0, 0); 
                pixelBlockSize = new DblPntClass(); 
                pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height); 
                pixelBlock3 = rawPixels.CreatePixelBlock(pixelBlockSize) as IPixelBlock3; 
                // Read pixelblock     
                rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); 
                // Get pixeldata array      
                System.Array pixelData; 
                pixelData = (System.Array)pixelBlock3.get_PixelDataByRef(0); 
                // Loop through all the pixels and assign value    
                for (int i = 0; i  rasterProps.Width; i++)//这个数组中就是输入的Value值 
                { 
                    for (int j = 0; j  rasterProps.Height; j++) 
                    { 
                        //tempPt.X = minX + i * cellSize; 
                      //  object oValue =CountourpixelVale.GetValue(0, 0); 
                      //  tempPt.Y = minY + j * cellSize; 
                        Filldi(minLon + i * dLon, minLat + j * dlat); 
                        float temp = (float)GIDS(minX + i * cellSize, minY + j * cellSize, Convert.ToDouble(CountourpixelVale.GetValue(i, j))); 
                        ////求均方差 
                        //for (int k=0;k<dt.Rows.Count;k++) 
                        //{ 
                        //    MeanSquareDeviation += (temp - Railfall[k])*(temp - Railfall[k]); 
                        //} 
                        //MeanSquareDeviation = Math.Sqrt(MeanSquareDeviation / dt.Rows.Count); 
                        ////均方差写入文件 
                        //sw.WriteLine(i + "," + j + "," + MeanSquareDeviation); 
                        
                        pixelData.SetValue(temp, i, j);//Convert.ToByte((i * j) % 255) 
                    } 
                } 
 
               for (int k=0;k<dt.Rows.Count;k++) 
               { 
                   int rr = Convert.ToInt32(Math.Ceiling((Ptx[k] - minX) / cellSize)); 
                   int cc=Convert.ToInt32(Math.Ceiling((Pty[k]-minY)/cellSize)); 
                   double pp; 
                   if (rr  rowCount && cc  columCount) 
                   { 
                       pp = Convert.ToDouble(pixelData.GetValue(rr, cc)); 
                       sw.WriteLine(PtName[k]+","+Ptx[k] + "," + Pty[k] + "," + Math.Abs(pp - Railfall[k])); 
                   } 
                } 
                sw.Close(); 
                pixelBlock3.set_PixelData(0, (System.Object)pixelData); 
                // Write the pixeldata back      
                //System.Object cachePointer; 
                //cachePointer = rawPixels.AcquireCache(); 
                rawPixels.Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3); 
                //rawPixels.ReturnCache(cachePointer); 
                 
                //IRasterLayer pRasterLayer = new RasterLayerClass(); 
                //pRasterLayer.CreateFromDataset(rasterDataset); 
                //ILayer pLayer; 
                //pLayer = pRasterLayer; 
                //pLayer.Name = "Result"; 
                //axMapControl1.Map.AddLayer(pLayer); 
                //axMapControl1.ActiveView.Refresh(); 
 
                //解除引用 
                rawPixels = null; 
                rasterDataset= null; 
                pixelBlock3= null; 
                rasterBandCollection= null; 
                rasterProps = null; 
                GC.Collect();//强制回收 
                IRasterLayer m_raster = new RasterLayerClass(); 
                m_raster.CreateFromFilePath("c:\\temp\\" + RainfallValue); 
                axMapControl1.AddLayer(m_raster);    
                //IRasterEdit rasterEdit; //保存 
                //rasterEdit = pRasterLayer.Raster as IRasterEdit; 
                //rasterEdit.Write(pixelBlockOrigin, pixelBlock3 as IPixelBlock); 
                //rasterEdit.Refresh(); 
 
                //return rasterDataset; 
            } 
            catch (Exception ex) 
            { 
                sw.Close(); 
                MessageBox.Show("My God, Error!" + ex.Message); 
                //System.Diagnostics.Debug.WriteLine(ex.Message); 
                return;// null; 
            } 
        } 
        double Cx = 0.0007; 
        double Cy = 0.0006; 
        double Cz = 0.5199; 
        private double GIDS(double x,double y,double z)//反梯度法求值 
        { 
            double diSum=0; 
            double tempSum=0; 
            for (int i = 0; i  dt.Rows.Count; i++) 
            { 
                diSum += 1 / (di[i] * di[i]); 
                tempSum += (Railfall[i] + (x - Ptx[i]) * Cx + (y - Pty[i]) * Cy + Cz*(z - Ptz[i])) / (di[i] * di[i]); 
            } 
            tempSum = tempSum / diSum; 
            return tempSum; 
        } 
        private void Filldi(double Templon,double Templat) 
        { 
            for (int i = 0; i  dt.Rows.Count; i++) 
            { 
                di[i] = GetDistance(Templon, Templat,lon[i],lat[i]); 
            } 
        } 
        public IRasterWorkspace2 createRasterWorkspace(string pathName) 
        { 
            // Create RasterWorkspace   
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); 
            return workspaceFactory.OpenFromFile(pathName, 0) as IRasterWorkspace2; 
        } 
        OleData oledata = new OleData(); 
        DataTable dt =new DataTable();  
 
        private void Init()//初始化复制,从数据库 
        { 
            for(int i = 0; i  dt.Rows.Count; i++) 
            { 
                 PtName[i]=dt.Rows[i][1].ToString(); 
                 //Cx[i] = Convert.ToDouble(dt.Rows[i][2]);//转换前X坐标 
                 //Cy[i] = Convert.ToDouble(dt.Rows[i][3]); 
                 Ptx[i] = Convert.ToDouble(dt.Rows[i][4]); 
                 Pty[i] = Convert.ToDouble(dt.Rows[i][5]); 
                 lon[i] = Convert.ToDouble(dt.Rows[i][6]); 
                 lat[i] = Convert.ToDouble(dt.Rows[i][7]); 
                 Ptz[i] = Convert.ToDouble(dt.Rows[i][8]); 
                 Railfall[i] = Convert.ToDouble(dt.Rows[i][9]);//降雨量100 
            } 
        } 
        private void ResetRailfall() 
        { 
            for (int i = 0; i  dt.Rows.Count; i++) 
            { 
                Railfall[i] = Convert.ToDouble(dt.Rows[i][RainfallValue]);//降雨量100 
            } 
        } 
        private double GetMinX() 
        { 
            double min = Ptx[0]; 
            for (int i = 1; i  dt.Rows.Count;i++ ) 
            { 
                if (Ptx[i]  min) 
                    min = Ptx[i]; 
            } 
            return min; 
        } 
        private double GetMinY() 
        { 
            double min = Pty[0]; 
            for (int i = 1; i  dt.Rows.Count; i++) 
            { 
                if (Pty[i]  min) 
                    min = Pty[i]; 
            } 
            return min; 
        } 
        private double GetMaxX() 
        { 
            double max = Ptx[0]; 
            for (int i = 1; i  dt.Rows.Count; i++) 
            { 
                if (Ptx[i] > max) 
                    max = Ptx[i]; 
            } 
            return max; 
        } 
        private double GetMaxY() 
        { 
            double max = Pty[0]; 
            for (int i = 1; i  dt.Rows.Count; i++) 
            { 
                if (Pty[i] > max) 
                    max = Pty[i]; 
            } 
            return max; 
        } 
        private double GetMinLon() 
        { 
            double min = lon[0]; 
            for (int i = 1; i  dt.Rows.Count; i++) 
            { 
                if (lon[i]  min) 
                    min = lon[i]; 
            } 
            return min; 
        } 
        private double GetMinLat() 
        { 
            double min = lat[0]; 
            for (int i = 1; i  dt.Rows.Count; i++) 
            { 
                if (lat[i]  min) 
                    min = lat[i]; 
            } 
            return min; 
        } 
        private double GetMaxLon() 
        { 
            double max = lon[0]; 
            for (int i = 1; i  dt.Rows.Count; i++) 
            { 
                if (lon[i] > max) 
                    max = lon[i]; 
            } 
            return max; 
        } 
        private double GetMaxLat() 
        { 
            double max = lat[0]; 
            for (int i = 1; i  dt.Rows.Count; i++) 
            { 
                if (lat[i] > max) 
                    max = lat[i]; 
            } 
            return max; 
        } 
        const double PAI=3.1415926535; 
        const double EARTH_RADUS = 6371004; 
        double GetDistance(double a_dFirLon, double a_dFirLat, double a_dSecdLon, double a_dSecdLat) 
        { 
            double dRes = 0; 
            double dTemp = 0, dTemp2 = 0; 
 
            dTemp = Math.Cos(a_dSecdLat * PAI / 180) * Math.Cos(a_dFirLat * PAI / 180) * Math.Cos((a_dFirLon - a_dSecdLon) * PAI / 180) + Math.Sin(a_dSecdLat * PAI / 180) * Math.Sin(a_dFirLat * PAI / 180); 
            dTemp2 = EARTH_RADUS * Math.Acos(dTemp); 
	        dRes = dTemp2; 
	        return dRes; 
        } 
    } 
}
原文地址:https://www.cnblogs.com/gisoracle/p/2417035.html