• C#+AE9.2生成多波段栅格数据功能函数


    C3+Ae9.2写的生成多波段栅格数据的函数

    参数的实例为:

                        //savepath = "C:\\tmpdata\\";
                        //savename = "testfile.img";

                        //FYIR3FileInfo 为我保存的数据。

    具体到其他使用要修改的,这个是生成四波段的,如果用的不是四波段,要修改里面的波段数据。

    生成的效果是可以的,既可以在ae的mapcontrol控件中显示,也可以输出为img格式的文件。

             struct RasterInfo
            {
                public int lNumbands;
            }
            /// <summary>  
            /// 生成多波段IR3L栅格文件数据功能  
            /// </summary>  
            /// <param name="FYIR3FileInfo"></param>
            /// <param name="savepath"></param> 
            /// <param name="savefilename"></param>
            /// <returns></returns> 
            public ILayer CreateThreeBandIR3L(IR3LFile FYIR3FileInfo, string savepath, string savefilename)
            {
                //The file must not be there. Otherwise, dataset can not be got.
                if (File.Exists(savepath + savefilename))
                {
                    MessageBox.Show("File Exist!");
                    IRasterLayer pRasterLayer = new RasterLayerClass();
                    pRasterLayer.CreateFromFilePath(savepath + savefilename);
                    ILayer pLayer;
                    pLayer = pRasterLayer;
                    pLayer.Name = "New Raster";

                    return pLayer;
                }
                // This function creates a new img file in the given workspace 
                // and then assigns pixel values
                try
                {
                    IRasterDataset rasterDataset = null;
                    IPoint originPoint = new PointClass();
                    originPoint.PutCoords(FYIR3FileInfo.dblLeftDownLong, FYIR3FileInfo.dblLeftDownLat);
                    // Create the dataset  
                    IRasterWorkspace2 rasterWorkspace2 = null;
                    double xPixelSize;
                    double yPixelSize;
                    //像元大小
                    xPixelSize = (FYIR3FileInfo.dblcenterLong - FYIR3FileInfo.dblLeftDownLong) / (FYIR3FileInfo.intNumX / 2);
                    yPixelSize = (FYIR3FileInfo.dblcenterLat - FYIR3FileInfo.dblLeftDownLat) / (FYIR3FileInfo.intNumY / 2);

                    //坐标系
                    ISpatialReferenceFactory2 spatRefFact = new SpatialReferenceEnvironmentClass();
                    //m_ProjectedCoordinateSystem = spatRefFact.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_49N);
                    ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem m_ProjectedCoordinateSystem;
                    m_ProjectedCoordinateSystem = spatRefFact.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
                    rasterWorkspace2 = createRasterWorkspace(savepath);
                    rasterDataset = rasterWorkspace2.CreateRasterDataset(savefilename, "IMAGINE Image", originPoint, FYIR3FileInfo.intNumX, FYIR3FileInfo.intNumY, xPixelSize, yPixelSize, 4, rstPixelType.PT_UCHAR, m_ProjectedCoordinateSystem, true);//rstPixelType.PT_UCHAR,new UnknownCoordinateSystemClass()

                    RasterInfo rstInfo = new RasterInfo();
                    rstInfo.lNumbands = 4;

                    IRawPixels[] rawPixels = new IRawPixels[rstInfo.lNumbands];
                    IPixelBlock3[] pixelBlock3 = new IPixelBlock3[rstInfo.lNumbands];
                    IPnt pixelBlockOrigin = null;
                    IPnt pixelBlockSize = null;
                    IRasterBandCollection rasterBandCollection;
                    IRasterProps rasterProps;
                    // QI for IRawPixels and IRasterProps   
                    rasterBandCollection = (IRasterBandCollection)rasterDataset;

                    for (int i = 0; i < rstInfo.lNumbands; i ++ )
                    {
                        rawPixels[i] = (IRawPixels)rasterBandCollection.Item(i);
                    }

                    //调用调色板
                    string PalFilePath;
                    CreateIMG.Class.Clsfengyun.Pal TmppalInfo;
                    TmppalInfo = new CreateIMG.Class.Clsfengyun.Pal();
                    PalFilePath = Application.StartupPath + "\\系统文件\\pal\\I-01.pal";
                    ReadPal(PalFilePath, ref TmppalInfo);

                    int tmpColorValue;
                    System.Array[] pixelData = new System.Array[rstInfo.lNumbands];

                    rasterProps = (IRasterProps)rawPixels[0];
                    for (int i = 0; i < rstInfo.lNumbands; i++)
                    {
                        rasterProps = (IRasterProps)rawPixels[i];
                        // Create pixelblock   
                        pixelBlockOrigin = new DblPntClass();
                        pixelBlockOrigin.SetCoords(0, 0);
                        pixelBlockSize = new DblPntClass();
                        pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);

                        pixelBlock3[i] = (IPixelBlock3)rawPixels[i].CreatePixelBlock(pixelBlockSize);
                        // Read pixelblock   
                        rawPixels[i].Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3[i]);
                        // Get pixeldata array    
                        pixelData[i] = (System.Array)pixelBlock3[i].get_PixelDataByRef(0);

                        for (int ii = 0; ii < rasterProps.Width; ii++)
                            for (int jj = 0; jj < rasterProps.Height; jj++)
                            {
                                if (i == 0)
                                {
                                    tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpR;
                                    pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                                }
                                else if (i == 1)
                                {
                                    tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpG;
                                    pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                                }
                                else if (i == 2)
                                {
                                    tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpB;
                                    pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                                }
                                else
                                {
                                    tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpValue;
                                    pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                                }
                            }
                        pixelBlock3[i].set_PixelData(0, (System.Object)pixelData[i]);

                        System.Object cachePointer;
                        cachePointer = rawPixels[i].AcquireCache();
                        rawPixels[i].Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3[i]);
                        rawPixels[i].ReturnCache(cachePointer);
                    }

                    IRasterLayer pRasterLayer = new RasterLayerClass();
                    pRasterLayer.CreateFromDataset(rasterDataset);
                    ILayer pLayer;
                    pLayer = pRasterLayer;
                    pLayer.Name = "New Raster";

                    return pLayer;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    //System.Diagnostics.Debug.WriteLine(ex.Message);
                    return null;
                }
            }

  • 相关阅读:
    python基础练习题(题目 画菱形)
    python基础练习题(题目 两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单)
    python基础练习题(题目 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少)
    python基础练习题(题目 一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数)
    python基础练习题(题目 求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制)
    酒库重构测试—半自动化验证
    python基础练习题(题目 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数)
    python基础练习题(题目 将一个整数分解质因数。例如:输入90,打印出90=2*3*3*5)
    python基础练习题(题目 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身)
    python基础练习题(题目 判断101-200之间有多少个素数,并输出所有素数。)
  • 原文地址:https://www.cnblogs.com/yuxuetaoxp/p/1716300.html
Copyright © 2020-2023  润新知