Exemplo n.º 1
0
 /// <summary>
 /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels.
 /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling.
 /// The log raster is the natural log of the raster.
 /// </summary>
 /// <param name="pTlc">Point to start the reading from in the Raster</param>
 /// <param name="pRaster">Reference Raster for the PixelBlock</param>
 /// <param name="pPixelBlock">PixelBlock to be filled in</param>
 public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock)
 {
     try
     {
         // Call Read method of the Raster Function Helper object.
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         int  pBHeight = pPixelBlock.Height;
         int  pBWidth  = pPixelBlock.Width;
         IPnt pbSize   = new PntClass();
         pbSize.SetCoords(pBWidth, pBHeight);
         //IPixelBlock3 inVlsPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);
         //myFunctionHelperCoef.Read(pTlc,null,myFunctionHelperCoef.Raster, (IPixelBlock)inVlsPb);
         IPixelBlock3          outPixelBlock = (IPixelBlock3)pPixelBlock;
         System.Array          outArr        = (System.Array)outPixelBlock.get_PixelData(0);
         System.Array[]        inArr         = new System.Array[inrs.RasterInfo.BandCount];
         IRasterBandCollection rsBc          = (IRasterBandCollection)inrs;
         for (int b = 0; b < inrs.RasterInfo.BandCount; b++)
         {
             IRasterBand rsB  = rsBc.Item(b);
             IRawPixels  rPix = (IRawPixels)rsB;
             IPixelBlock pb   = rPix.CreatePixelBlock(pbSize);
             rPix.Read(pTlc, pb);
             inArr[b] = (System.Array)pb.get_SafeArray(b);
         }
         updateOutArr(outPixelBlock, inArr, outArr);
         outPixelBlock.set_PixelData(0, outArr);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels.
 /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling.
 /// The log raster is the natural log of the raster.
 /// </summary>
 /// <param name="pTlc">Point to start the reading from in the Raster</param>
 /// <param name="pRaster">Reference Raster for the PixelBlock</param>
 /// <param name="pPixelBlock">PixelBlock to be filled in</param>
 public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock)
 {
     try
     {
         // Call Read method of the Raster Function Helper object.
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         int  pBHeight = pPixelBlock.Height;
         int  pBWidth  = pPixelBlock.Width;
         IPnt pbSize   = new PntClass();
         pbSize.SetCoords(pBWidth, pBHeight);
         IPixelBlock3          ipPixelBlock = (IPixelBlock3)pPixelBlock;
         System.Array[]        inArr        = new System.Array[inrsBands.RasterInfo.BandCount];
         IRasterBandCollection rsBc         = (IRasterBandCollection)inrsBands;
         for (int p = 0; p < inArr.Length; p++)
         {
             IRasterBand  rsB = rsBc.Item(p);
             IRawPixels   rP  = (IRawPixels)rsB;
             IPixelBlock  pb  = rP.CreatePixelBlock(pbSize);
             IPixelBlock3 pb3 = (IPixelBlock3)pb;
             rP.Read(pTlc, pb);
             inArr[p] = (System.Array)pb3.get_PixelData(0);
         }
         System.Array outArr = (System.Array)pPixelBlock.get_SafeArray(0);
         rstPixelType rsPt   = pPixelBlock.get_PixelType(0);
         for (int r = 0; r < ipPixelBlock.Height; r++)
         {
             for (int c = 0; c < ipPixelBlock.Width; c++)
             {
                 object outVlObj = ipPixelBlock.GetVal(0, c, r);
                 if (outVlObj != null)
                 {
                     float outVl;
                     if (getOutPutVl(inArr, c, r, out outVl))
                     {
                         object newVl = rasterUtil.getSafeValue(outVl, rsPt);//convertVl(outVl,rsPt);
                         outArr.SetValue(newVl, c, r);
                     }
                 }
             }
         }
         ipPixelBlock.set_PixelData(0, outArr);
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method of localMean Function. " + exc.Message, exc);
         Console.Write(exc.ToString());
         throw myExc;
     }
 }
Exemplo n.º 3
0
        private double[][] getValues(IPoint pnt, out double[] rwVls)
        {
            rwVls = new double[bndCnt];
            double[][] outJagArr = new double[(geoerro * geoerro)][];
            for (int i = 0; i < outJagArr.Length; i++)
            {
                outJagArr[i] = new double[bndCnt];
            }
            IRasterBandCollection rsBc = (IRasterBandCollection)FunctionRasterDataset;
            IRasterBand           rsB;
            IPnt pSize = new PntClass();

            pSize.SetCoords(geoerro, geoerro);
            IPnt      pLoc = new PntClass();
            int       clm, rw;
            IGeometry geo      = (IGeometry)FunctionRasterDataset.RasterInfo.Extent;
            IPoint    ul       = FunctionRasterDataset.RasterInfo.Extent.UpperLeft;
            IPnt      cellSize = FunctionRasterDataset.RasterInfo.CellSize;
            int       sub      = (geoerro / 2);

            rsUtil.getClmRw(ul, cellSize, pnt, out clm, out rw);
            int nclm = clm - sub;
            int nrw  = rw - sub;

            pLoc.SetCoords(System.Convert.ToDouble(nclm), System.Convert.ToDouble(nrw));
            for (int i = 0; i < bndCnt; i++)
            {
                rsB = rsBc.Item(i);
                IRawPixels  rpix = (IRawPixels)rsB;
                IPixelBlock pb   = rpix.CreatePixelBlock(pSize);
                rpix.Read(pLoc, pb);
                int pbCnt = 0;
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object vlObj = pb.GetVal(0, c, r);
                        if (vlObj != null)
                        {
                            outJagArr[pbCnt][i] = System.Convert.ToDouble(vlObj);
                        }
                        pbCnt++;
                    }
                }
            }
            rwVls = outJagArr[(geoerro * geoerro / 2)];
            return(outJagArr);
        }
Exemplo n.º 4
0
 public static IRasterDataset createFileRasterDataset(IRasterWorkspace2 irasterWorkspace2_0, string string_0,
                                                      int int_0, rstPixelType rstPixelType_0, ISpatialReference ispatialReference_0)
 {
     try
     {
         IRasterDataset dataset = null;
         IPoint         origin  = new Point();
         origin.PutCoords(0.0, 0.0);
         if (ispatialReference_0 == null)
         {
             ispatialReference_0 = new UnknownCoordinateSystem() as ISpatialReference;
         }
         dataset = irasterWorkspace2_0.CreateRasterDataset(string_0, "IMAGINE Image", origin, 200, 100, 1.0, 1.0,
                                                           int_0, rstPixelType_0, ispatialReference_0, true);
         IRawPixels            pixels = null;
         IPixelBlock3          block  = null;
         IPnt                  tlc    = null;
         IPnt                  size   = null;
         IRasterBandCollection bands  = (IRasterBandCollection)dataset;
         pixels = (IRawPixels)bands.Item(0);
         IRasterProps props = (IRasterProps)pixels;
         tlc = new DblPnt();
         tlc.SetCoords(0.0, 0.0);
         size = new DblPnt();
         size.SetCoords((double)props.Width, (double)props.Height);
         block = (IPixelBlock3)pixels.CreatePixelBlock(size);
         pixels.Read(tlc, (IPixelBlock)block);
         object[,] objArray = (object[, ])block.get_PixelDataByRef(0);
         for (int i = 0; i < props.Width; i++)
         {
             for (int j = 0; j < props.Height; j++)
             {
                 objArray[i, j] = (i * j) % 255;
             }
         }
         object cache = pixels.AcquireCache();
         pixels.Write(tlc, (IPixelBlock)block);
         pixels.ReturnCache(cache);
         return(dataset);
     }
     catch (Exception exception)
     {
         Debug.WriteLine(exception.Message);
         return(null);
     }
 }
Exemplo n.º 5
0
        private void button1_Click(object sender, EventArgs e)
        {
            string jieguo2;

            for (int i = 0; i < frm.mainMapControl.LayerCount; i++)
            {
                ////////    if (frm.axMapControl1.get_Layer(i).Name == input)
                ////////    {
                ////////        temp = i;
                ////////        break;
                ////////    }
                ////////}
                ////////IRasterLayer pRasterLayer = frm.axMapControl1.get_Layer(temp) as IRasterLayer;
                string inputname = frm.mainMapControl.get_Layer(i).Name;
                int    location2 = inputname.LastIndexOf('.');
                string sss2      = inputname;
                jieguo2 = sss2.Substring(0, location2);////"."后文件名称
                IRasterLayer pRasterLayer = frm.mainMapControl.get_Layer(i) as IRasterLayer;

                IRaster2 raster = pRasterLayer.Raster as IRaster2;

                System.Array array;
                //IRaster2 raster = pRasterLayer.Raster as IRaster2;
                IRasterDataset rasterDataset = raster.RasterDataset;
                IPoint         pPointOrign   = new PointClass();
                pPointOrign.X = 0;
                pPointOrign.Y = 0;
                IPnt pixelBlockOrigin = null;
                pixelBlockOrigin = new DblPntClass();
                pixelBlockOrigin.SetCoords(pPointOrign.X, pPointOrign.Y);
                IRasterBandCollection dirBandCollection = (IRasterBandCollection)rasterDataset;
                IRasterBand           dirRasterBand     = dirBandCollection.Item(0);
                IRawPixels            dirRawPixels      = (IRawPixels)dirRasterBand;
                IRasterProps          dirProps          = (IRasterProps)dirRawPixels;
                int    dirColumns   = dirProps.Width;                 //列数
                int    dirRows      = dirProps.Height;                //行数
                double cellSizeX    = dirProps.MeanCellSize().X;      //得到x方向栅格大小
                double cellSizeY    = dirProps.MeanCellSize().Y;      ////得到y方向栅格大小
                IPnt   dirBlockSize = new DblPntClass();
                dirBlockSize.X = dirColumns;
                dirBlockSize.Y = dirRows;
                IPixelBlock3 dirPixelBlock = (IPixelBlock3)dirRawPixels.CreatePixelBlock(dirBlockSize);
                dirRawPixels.Read(pixelBlockOrigin, (IPixelBlock)dirPixelBlock);
                array = (System.Array)dirPixelBlock.get_PixelDataByRef(0);

                string ss = "";
                double[,] b = new double[dirRows, dirColumns];
                for (int row = dirRows - 1; row < dirRows; row++)
                {
                    for (int col = 0; col < dirColumns; col++)
                    {
                        b[row, col] = Convert.ToSingle(array.GetValue(col, row));
                    }
                }

                int minnum, maxnum, chuangkou;

                if (textBox1.Text == "")
                {
                    minnum = 2;
                }
                else
                {
                    minnum = Convert.ToInt32(textBox1.Text);
                }

                if (textBox2.Text == "")
                {
                    maxnum = 1;
                }
                else
                {
                    maxnum = Convert.ToInt32(textBox2.Text);
                }
                chuangkou = minnum;

                //////////////////////////////////////////////////////////////////////////
                if (textBox3.Text == "")
                {
                    MessageBox.Show("文件保存名不能为空");
                }


                String savename = textBox3.Text + "\\" + jieguo2 + ".txt";
                //////////////////////////////////////////////////////////////////////////

                for (int c = 0; c < maxnum; c++)
                {
                    double result, sum, mean, fangcha, ste, a;////////修改程序2011年11月15日
                    a       = 0;
                    ste     = 0;
                    sum     = 0;
                    fangcha = 0;


                    for (int row = dirRows - 1; row < dirRows; row++)              ////窗口大小与图像行列数关系
                    {
                        for (int col = 0; col < dirColumns - chuangkou + 1; col++) ////窗口大小与图像行列数关系
                        {
                            for (int k = row; k < row + 1; k++)                    ////窗口大小与图像栅格和的关系
                            {
                                for (int l = col; l < col + chuangkou; l++)
                                {
                                    sum = sum + b[k, l];
                                }
                            }
                            mean = sum / (chuangkou);/////一维图像就像是窗口大小

                            for (int m = row; m < row + 1; m++)
                            {
                                for (int n = col; n < col + chuangkou; n++)
                                {
                                    fangcha = fangcha + (b[m, n] - mean) * (b[m, n] - mean);
                                }
                            }

                            result = System.Math.Sqrt(fangcha / (chuangkou));/////一维图像就像是窗口大小

                            fangcha = 0;
                            sum     = 0;
                            ste     = ste + result;
                            a       = a + 1;
                        }
                    }


                    //ss += ste / a + "    " + chuangkou + "\r\n";
                    ss += ste / a + "  " + ste + "  " + a + "\r\n";
                    //string varString = Convert.ToString(chuangkou);

                    //////////////////////////////////////////////////////////////////////////
                    ///////////////改写内容
                    String Strsavefile;
                    Strsavefile = savename;
                    ////////FileStream fs = new FileStream(Strsavefile);

                    StreamWriter sw = new StreamWriter(Strsavefile);
                    sw.WriteLine(ss);
                    sw.Close();
                    //////////////////////////////////////////////////////////////////////////
                    chuangkou = chuangkou + 1;
                    //MessageBox.Show(varString,"OK");
                    //this.Hide();
                }
            }
            MessageBox.Show("OK");
            this.Hide();
        }
Exemplo n.º 6
0
        /// <summary>
        /// 创建栅格数据集,本地
        /// </summary>
        /// <param name="directoryName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private IRasterDataset CreateFileRasterDataset(string directoryName, string fileName)
        {
            // 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(0, 0);

                // Create the dataset
                IRasterWorkspace2 rasterWorkspace2 = null;
                rasterWorkspace2 = CreateRasterWorkspace(directoryName);

                rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, 200, 100, 1, 1, 1, rstPixelType.PT_UCHAR, new UnknownCoordinateSystemClass(), true);

                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 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize);



                // Read pixelblock
                rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3);

                // Get pixeldata array
                System.Object[,] pixelData;
                pixelData = (System.Object[, ])pixelBlock3.get_PixelDataByRef(0);

                // Loop through all the pixels and assign value
                for (int i = 0; i < rasterProps.Width; i++)
                {
                    for (int j = 0; j < rasterProps.Height; j++)
                    {
                        pixelData[i, j] = (i * j) % 255;
                    }
                }



                // Write the pixeldata back
                System.Object cachePointer;

                cachePointer = rawPixels.AcquireCache();

                rawPixels.Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3);

                rawPixels.ReturnCache(cachePointer);

                // Return raster dataset
                return(rasterDataset);
            }
            catch (Exception ex)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(ex, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(ex, null, DateTime.Now);
                }
                //********************************************************************

                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(null);
            }
        }
Exemplo n.º 7
0
        public void DEMToTIN(IRaster iRaster, string tinFileName)
        {
            try
            {
                //***************生成TIN模型*********************************************
                IGeoDataset           pGeoData   = iRaster as IGeoDataset;
                IEnvelope             pExtent    = pGeoData.Extent;
                IRasterBandCollection pRasBC     = iRaster as IRasterBandCollection;
                IRasterBand           pRasBand   = pRasBC.Item(0);
                IRawPixels            pRawPixels = pRasBand as IRawPixels;
                IRasterProps          pProps     = pRawPixels as IRasterProps;
                int    iWid       = pProps.Width;
                int    iHei       = pProps.Height;
                double w          = iWid / 1000.0f;
                double h          = iHei / 1000.0f;
                IPnt   pBlockSize = new DblPntClass();
                bool   IterationFlag;

                if (w < 1 && h < 1) //横纵都小于1000个像素
                {
                    pBlockSize.X  = iWid;
                    pBlockSize.Y  = iHei;
                    IterationFlag = false;
                }
                else
                {
                    pBlockSize.X  = 1001.0f;
                    pBlockSize.Y  = 1001.0f;
                    IterationFlag = true;
                }
                double cellsize = 0.0f;             //栅格大小

                IPnt pPnt1 = pProps.MeanCellSize(); //栅格平均大小
                cellsize = pPnt1.X;
                ITinEdit pTinEdit = new TinClass() as ITinEdit;
                pTinEdit.InitNew(pExtent);
                ISpatialReference pSpatial = pGeoData.SpatialReference;
                pExtent.SpatialReference = pSpatial;
                IPnt pOrigin           = new DblPntClass();
                IPnt pPixelBlockOrigin = new DblPntClass();
                //栅格左上角像素中心坐标
                double bX = pBlockSize.X;
                double bY = pBlockSize.Y;
                pBlockSize.SetCoords(bX, bY);
                IPixelBlock   pPixelBlock   = pRawPixels.CreatePixelBlock(pBlockSize);
                object        nodata        = pProps.NoDataValue; //无值标记
                ITinAdvanced2 pTinNodeCount = pTinEdit as ITinAdvanced2;
                int           nodeCount     = pTinNodeCount.NodeCount;
                object        vtMissing     = Type.Missing;
                object        vPixels       = null; //格子
                double        m_zTolerance  = 0;
                if (!IterationFlag)                 //当为一个处理单元格子时;(w < 1 && h < 1) //横纵都小于1000个像素
                {
                    pPixelBlockOrigin.SetCoords(0.0f, 0.0f);
                    pRawPixels.Read(pPixelBlockOrigin, pPixelBlock);
                    vPixels = pPixelBlock.get_SafeArray(0);
                    double xMin = pExtent.XMin;
                    double yMax = pExtent.YMax;
                    pOrigin.X = xMin + cellsize / 2;
                    pOrigin.Y = yMax - cellsize / 2;
                    bX        = pOrigin.X;
                    bY        = pOrigin.Y;
                    pTinEdit.AddFromPixelBlock(bX, bY, cellsize, cellsize, nodata, vPixels, m_zTolerance, ref vtMissing, out vtMissing);
                }
                else //当有多个处理单元格时,依次循环处理每个单元格
                {
                    int i = 0, j = 0, count = 0;
                    int FirstGoNodeCount = 0;

                    //while (nodeCount != FirstGoNodeCount)
                    //{
                    count++;
                    nodeCount = pTinNodeCount.NodeCount;
                    int bwidth = 0; int bheight = 0;
                    //依次循环处理
                    for (i = 0; i < (int)h + 1; i++)
                    {
                        if (i < (int)h)
                        {
                            bheight = 1000;
                        }
                        else
                        {
                            bheight = iHei - 1000 * i;
                        }
                        for (j = 0; j < (int)w + 1; j++)
                        {
                            if (j < (int)w)
                            {
                                bwidth = 1000;
                            }
                            else
                            {
                                bwidth = iWid - 1000 * j;
                            }
                            pBlockSize.SetCoords(bwidth, bheight);
                            pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize);


                            double bX1, bY1, xMin1, yMax1;
                            bX1 = pBlockSize.X;
                            bY1 = pBlockSize.Y;
                            pPixelBlockOrigin.SetCoords(j * 1000, i * 1000);

                            pRawPixels.Read(pPixelBlockOrigin, pPixelBlock);
                            vPixels = pPixelBlock.get_SafeArray(0);
                            xMin1   = pExtent.XMin;
                            yMax1   = pExtent.YMax;
                            //bX1 = pBlockSize.X;
                            //bY1 = pBlockSize.Y;
                            pOrigin.X = xMin1 + j * 1000 * cellsize + cellsize / 2.0f;
                            pOrigin.Y = yMax1 - i * 1000 * cellsize - cellsize / 2.0f;
                            bX1       = pOrigin.X;
                            bY1       = pOrigin.Y;
                            pTinEdit.AddFromPixelBlock(bX1, bY1, cellsize, cellsize, nodata, vPixels, m_zTolerance, ref vtMissing, out vtMissing);
                            FirstGoNodeCount = pTinNodeCount.NodeCount;
                        }
                    }
                    //}
                }
                //保存TIN文件
                pTinEdit.SaveAs(tinFileName, ref vtMissing);
                pTinEdit.StopEditing(true);
                m_pTin = pTinEdit as ITin;
                MessageBox.Show("转换成功!");
            }
            catch (SystemException e)
            {
                MessageBox.Show(e.Message);
            }
        }
Exemplo n.º 8
0
        //分析的主要实现函数
        private IRasterLayer Analyze(IRasterLayer pRasterLayer)
        {
            IRaster      pRaster     = pRasterLayer.Raster;
            IRasterProps rasterProps = (IRasterProps)pRaster;

            //设置栅格数据起始点
            IPnt pBlockSize = new Pnt();

            pBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);

            //选取整个范围
            IPixelBlock pPixelBlock = pRaster.CreatePixelBlock(pBlockSize);

            //左上点坐标
            IPnt tlp = new Pnt();

            tlp.SetCoords(0, 0);

            //读入栅格
            IRasterBandCollection pRasterBands = pRaster as IRasterBandCollection;
            IRasterBand           pRasterBand  = pRasterBands.Item(0);
            IRawPixels            pRawRixels   = pRasterBands.Item(0) as IRawPixels;

            pRawRixels.Read(tlp, pPixelBlock);

            //将PixBlock的值组成数组
            Array pSafeArray = pPixelBlock.get_SafeArray(0) as Array;

            //Array转数组
            double[,] myDoubleArr = new double[pSafeArray.GetLength(0), pSafeArray.GetLength(1)];
            for (int i = 0; i < myDoubleArr.GetLength(0); i++)
            {
                for (int j = 0; j < myDoubleArr.GetLength(1); j++)
                {
                    myDoubleArr[i, j] = Convert.ToDouble(pSafeArray.GetValue(i, j));
                }
            }

            for (int i = 0; i < myDoubleArr.GetLength(0); i++)
            {
                for (int j = 0; j < myDoubleArr.GetLength(1); j++)
                {
                    if (myDoubleArr[i, j] == 255)
                    {
                        myDoubleArr[i, j] = 0;
                    }
                }
            }
            double[,] ZeroArray = GetArray(myDoubleArr, Convert.ToInt32(textBox4.Text));
            double[,] OArray    = SumArray(ZeroArray, Convert.ToInt32(textBox4.Text));
            double[,] LastArray = ReturnLastArray(OArray, Convert.ToInt32(textBox4.Text));
            pPixelBlock.set_SafeArray(0, LastArray);

            //StreamWriter sw = File.AppendText(@"E:\GIS底层实验\WorkSpace\result\arrry.txt");
            //for (int y = 0; y < rasterProps.Height; y++)
            //{
            //    for (int x = 0; x < rasterProps.Width; x++)
            //    {
            //        //int value = Convert.ToInt32(pSafeArray.GetValue(x, y));
            //        Byte value = Convert.ToByte(pSafeArray.GetValue(x, y));
            //        string TxtCon = ("X:" + Convert.ToString(x) + "," + "Y:" + Convert.ToString(y) + "," + "Value" + pSafeArray.GetValue(x, y) + "\n");
            //        sw.Write(TxtCon);
            //    }
            //}
            //sw.Flush();
            //sw.Close();

            // 编辑raster,将更新的值写入raster中
            IRasterEdit rasterEdit = pRaster as IRasterEdit;

            rasterEdit.Write(tlp, pPixelBlock);
            rasterEdit.Refresh();
            return(pRasterLayer);
        }
Exemplo n.º 9
0
        private List <byte> GetBlockUniqueValue(IRasterLayer rasterLyr, IElement element)
        {
            //get top visable raster
            if (rasterLyr == null)
            {
                XtraMessageBox.Show("读取分类结果文件失败!");
                return(null);
            }
            IRaster2 raster2 = rasterLyr.Raster as IRaster2;
            //element.Geometry.Project(rasterProps.SpatialReference);
            //if(element.Geometry.SpatialReference ==null)
            //    element.Geometry.SpatialReference = EnviVars.instance.MapControl.SpatialReference ;
            //element.Geometry.Project(rasterProps.SpatialReference);
            IRasterBandCollection rasterBandCollection = raster2.RasterDataset as IRasterBandCollection;
            IRasterBand           rasterBand           = rasterBandCollection.Item(0);
            IRawPixels            rawPixels            = rasterBand as IRawPixels;

            IPnt         pixelBlockOrigin = new DblPntClass();
            IPnt         pixelBlockSize   = new DblPntClass();
            IPixelBlock3 pixelBlock3      = null;
            //calculate element column and row in raster
            int       startColum, startRow, endColum, endRow = -1;
            IEnvelope envelope = element.Geometry.Envelope;
            //当前空间参考与栅格不一致时,重投影绘制坐标。
            IPoint            upleft        = new PointClass();
            IPoint            lowright      = new PointClass();
            ISpatialReference fromReference = EnviVars.instance.MapControl.Map.SpatialReference;
            ISpatialReference toReference   = (raster2 as IGeoDataset).SpatialReference;

            upleft.X = envelope.XMin;
            upleft.Y = envelope.YMax;
            upleft.SpatialReference = fromReference;
            lowright.X = envelope.XMax;
            lowright.Y = envelope.YMin;
            lowright.SpatialReference = fromReference;
            if (!GFS.Common.EngineAPI.IsEqualSpatialReference(fromReference, toReference))
            {
                upleft.Project(toReference);
                lowright.Project(toReference);
            }
            raster2.MapToPixel(upleft.X, upleft.Y, out startColum, out startRow);
            //raster2.MapToPixel(envelope.XMin, envelope.YMax, out startColum, out startRow);
            if (startColum < 0 && startRow < 0)
            {
                XtraMessageBox.Show("读取起始行列失败!");
                return(null);
            }
            pixelBlockOrigin.SetCoords(startColum, startRow);
            raster2.MapToPixel(lowright.X, lowright.Y, out endColum, out endRow);
            //raster2.MapToPixel(envelope.XMax, envelope.YMin, out endColum, out endRow);
            if (endColum < 0 && endRow < 0)
            {
                XtraMessageBox.Show("读取起始行列失败!");
                return(null);
            }
            pixelBlockSize.SetCoords(endColum - startColum + 1, endRow - startRow + 1);
            pixelBlock3 = rawPixels.CreatePixelBlock(pixelBlockSize) as IPixelBlock3;
            //read pixel
            rawPixels.Read(pixelBlockOrigin, pixelBlock3 as IPixelBlock);
            byte[,] pixelData = (byte[, ])pixelBlock3.get_PixelDataByRef(0);
            //get unique value
            List <byte> uniqueValue = new List <byte>();

            foreach (byte value in pixelData)
            {
                if (!uniqueValue.Contains(value))
                {
                    uniqueValue.Add(value);
                }
            }
            //sort
            uniqueValue.Sort();
            return(uniqueValue);
        }
Exemplo n.º 10
0
        private void ShowRasterInfo(IRasterLayer rLayer)

        {
            rtxbRasterInfo.Text = "Boand Number is " + rLayer.BandCount.ToString();// "\r\n" +"Resolution is " + rLayer.DisplayResolutionFactor.ToString() + "\r\n" + "Row number is " + rLayer.RowCount.ToString();
            IRaster raster = rLayer.Raster;
            //Raster Data
            IRasterBandCollection rBandCon = raster as IRasterBandCollection;
            int numBands = rBandCon.Count;

            for (int i = 0; i < numBands; i++)
            {
                //
                IRasterBand  rBand      = rBandCon.Item(i);
                IRasterProps rasterData = rBand as IRasterProps;
                //Raster Height
                int height = rasterData.Height;
                //Raster Width
                int width = rasterData.Width;
                //Raster Cell Size
                IPnt         cell  = rasterData.MeanCellSize();
                rstPixelType rType = rasterData.PixelType;
                MessageBox.Show(rType.ToString());
                if (rType == rstPixelType.PT_LONG)
                {
                    //Error code
                    //Read Raster
                    //Wrong QI
                    //IRaster pRaster = rBand as IRaster;
                    ITable       rasterTable = rBand.AttributeTable;
                    IQueryFilter qFilter     = new QueryFilter();
                    for (int j = 0; j < rasterTable.Fields.FieldCount; j++)
                    {
                        IField aField = rasterTable.Fields.get_Field(j);
                        qFilter.AddField(aField.ToString());
                    }

                    ICursor cursor = rasterTable.Search(qFilter, true);
                    IRow    aRow   = cursor.NextRow();
                    if (aRow != null)
                    {
                        //aRow.get_Value(0);
                        MessageBox.Show(cell.X.ToString() + "x" + cell.Y + ":" + aRow.get_Value(0).ToString());
                        //Read Raster
                        // rBand.
                    }
                }
                // Create pixelblock
                IRawPixels rawPixels = rBand as IRawPixels;
                //cannot be used as  follows
                //IRawPixels rawPixels = raster as IRawPixels;
                IPnt pixelBlockOrigin = new DblPnt();
                pixelBlockOrigin.SetCoords(0, 0);

                IPnt pixelBlockSize = new DblPnt();
                pixelBlockSize.SetCoords(rasterData.Width, rasterData.Height);
                IPixelBlock3 pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize);
                //Read
                rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3);
                //GetVal( int brand, int X, int Y); brand=0
                int    x     = 3600;
                int    y     = 800;
                object cellD = pixelBlock3.GetVal(0, x, y);
                //MessageBox.Show(cellD.ToString());

                rtxbRasterInfo.Text = rtxbRasterInfo.Text + "\r\n" + rBand.Bandname.ToString() + ":" + width.ToString() + "x" + height.ToString() + ":(" + x.ToString() + "," + y.ToString() + ")=" + cellD.ToString();
            }
        }
Exemplo n.º 11
0
        private void BtnOK_Click(object sender, EventArgs e)
        {
            int i = 0; int j = 0;

            if (string.IsNullOrEmpty(this.ComboBoxInLayer.Text))
            {
                MessageBox.Show("请选择需要二值化的图层!", "栅格二值化", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            int intLyrCount = frm.mainMapControl.LayerCount;

            for (i = 0; i <= intLyrCount - 1; i++)
            {
                if ((frm.mainMapControl.Map.get_Layer(i).Name == this.ComboBoxInLayer.Text.Trim()))
                {
                    IRasterLayer   pRasterLayer  = frm.mainMapControl.get_Layer(i) as IRasterLayer;
                    IRaster2       raster        = pRasterLayer.Raster as IRaster2;
                    IRasterDataset rasterDataset = raster.RasterDataset;

                    IRasterBandCollection dirBandCollection = (IRasterBandCollection)rasterDataset;
                    IRasterBand           dirRasterBand     = dirBandCollection.Item(0);
                    IRawPixels            dirRawPixels      = (IRawPixels)dirRasterBand;

                    IPnt         pixelBlockOrigin = null;
                    IPnt         dirBlockSize     = new DblPntClass();
                    IRasterProps dirProps         = (IRasterProps)dirRawPixels;
                    int          dirColumns       = dirProps.Width;       //列数
                    int          dirRows          = dirProps.Height;      //行数

                    IPixelBlock3 dirPixelBlock = (IPixelBlock3)dirRawPixels.CreatePixelBlock(dirBlockSize);
                    dirRawPixels.Read(pixelBlockOrigin, (IPixelBlock)dirPixelBlock);

                    System.Array array;
                    array = (System.Array)dirPixelBlock.get_PixelDataByRef(0);//获取栅格数组

                    int min;
                    if (textBox1.Text == "")
                    {
                        min = 20;
                    }
                    else
                    {
                        min = Convert.ToInt32(textBox1.Text);
                    }
                    /////获得栅格数据像元值
                    double[,] b = new double[dirRows, dirColumns];
                    double value;
                    string strExcute = null;
                    for (int row = 0; row < dirRows; row++)
                    {
                        for (int col = 0; col < dirColumns; col++)
                        {
                            b[row, col] = Convert.ToSingle(array.GetValue(col, row));
                            /////阈值判断
                            if (b[row, col] > min)
                            {
                                value = 1;
                            }
                            else
                            {
                                value = 0;
                            }
                            strExcute = Convert.ToString(value);
                        }
                    }
                    IGeoDataset   tempGeodata1  = (IGeoDataset)pRster;
                    IMapAlgebraOp pMapAlgebraOp = new RasterMapAlgebraOpClass();

                    //设置栅格运算空间
                    IRasterAnalysisEnvironment pRasterAnalysisEnvironment = (IRasterAnalysisEnvironment)pMapAlgebraOp;
                    IWorkspaceFactory          pWorkspaceFactory          = new RasterWorkspaceFactoryClass();

                    string outPath      = null;
                    string outLayerName = null;
                    outPath = this.TxtBox.Text.Trim();
                    j       = this.TxtBox.Text.Trim().LastIndexOf("\\");

                    outPath      = this.TxtBox.Text.Substring(0, j);
                    outLayerName = this.TxtBox.Text.Substring(j + 1);
                    string DataSetName = this.TxtBox.Text.Substring(j + 1);

                    //设置输出空间
                    IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(outPath, 0);
                    pRasterAnalysisEnvironment.OutWorkspace = pWorkspace;
                    IGeoDataset  outGetDataset = pMapAlgebraOp.Execute(strExcute);
                    IRasterLayer pCreatRalyr   = new RasterLayerClass();
                    pCreatRalyr.CreateFromRaster((IRaster)outGetDataset);

                    pCreatRalyr.Name = outLayerName;
                    frm.mainMapControl.AddLayer(pCreatRalyr);
                    break;
                }
            }
        }
Exemplo n.º 12
0
        private void btnCaculate_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog m_FolderBrowserDialog = new FolderBrowserDialog();
            string m_DataPathName = "";
            string m_DataName     = "";
            string parentPath     = "";

            if (m_DataPathName != "" || m_DataName != "" || parentPath != "")
            {
                m_DataPathName = m_FolderBrowserDialog.SelectedPath;
                DirectoryInfo m_DirectoryInfo = new DirectoryInfo(m_DataPathName);
                m_DataName = m_DirectoryInfo.Name;
                DirectoryInfo pDirectoryInfo = m_DirectoryInfo.Parent;
                parentPath = pDirectoryInfo.FullName;
            }
            textBox3.Text = m_DataPathName;

            stw.Start();

            string jieguo2;

            for (int i = 0; i < frm.mainMapControl.LayerCount; i++)
            {
                string inputname = frm.mainMapControl.get_Layer(i).Name;
                int    location2 = inputname.LastIndexOf('.');
                string sss2      = inputname;
                jieguo2 = sss2.Substring(0, location2);////"."后文件名称
                IRasterLayer   pRasterLayer     = frm.mainMapControl.get_Layer(i) as IRasterLayer;
                IRaster2       raster           = pRasterLayer.Raster as IRaster2;
                IRasterDataset rasterDataset    = raster.RasterDataset;
                IPnt           pixelBlockOrigin = new DblPntClass();
                pixelBlockOrigin.SetCoords(0, 0);
                IRasterBandCollection dirBandCollection = (IRasterBandCollection)rasterDataset;
                IRasterBand           dirRasterBand     = dirBandCollection.Item(0);
                IRawPixels            dirRawPixels      = (IRawPixels)dirRasterBand;
                IRasterProps          dirProps          = (IRasterProps)dirRawPixels;

                int    dirColumns   = dirProps.Width;                 //列数
                int    dirRows      = dirProps.Height;                //行数
                double cellSizeX    = dirProps.MeanCellSize().X;      //得到x方向栅格大小
                double cellSizeY    = dirProps.MeanCellSize().Y;      //得到y方向栅格大小
                IPnt   dirBlockSize = new DblPntClass();
                dirBlockSize.X = dirColumns;
                dirBlockSize.Y = dirRows;
                IPixelBlock3 dirPixelBlock = (IPixelBlock3)dirRawPixels.CreatePixelBlock(dirBlockSize);
                dirRawPixels.Read(pixelBlockOrigin, (IPixelBlock)dirPixelBlock);
                System.Array array = (System.Array)dirPixelBlock.get_PixelDataByRef(0);

                string ss = "";
                double[,] b = new double[dirRows, dirColumns];
                for (int row = 0; row < dirRows; row++)
                {
                    for (int col = 0; col < dirColumns; col++)
                    {
                        b[row, col] = Convert.ToSingle(array.GetValue(col, row));/////得到按行排列的值,注意这个为GetValue(col, row))
                    }
                }

                if (textBox3.Text == "")
                {
                    MessageBox.Show("文件保存名不能为空");
                }

                String savename = textBox3.Text + "\\" + jieguo2 + ".txt";
                for (int c = 0; c < maxnum; c++)
                {
                    double sum, mean, a;//修改程序2012年7月27日
                    a    = 0;
                    sum  = 0;
                    mean = 0;

                    if (cbBDirection.Text == "左边--->右边")
                    {
                        for (int row = 0; row < dirRows; row++)            ////窗口大小与图像行列数关系
                        {
                            for (int col = 0; col < dirColumns - k; col++) ////窗口大小与图像行列数关系
                            {
                                sum += Math.Pow((b[row, col] - b[row, col + k]), 2);
                                a    = a + 1;
                            }
                        }
                    }
                    if (cbBDirection.Text == "上边--->下边")
                    {
                        for (int row = 0; row < dirRows - k; row++)    ////窗口大小与图像行列数关系
                        {
                            for (int col = 0; col < dirColumns; col++) ////窗口大小与图像行列数关系
                            {
                                sum = sum + Math.Pow((b[row, col] - b[row + k, col]), 2);
                                a   = a + 1;
                            }
                        }
                    }
                    if (cbBDirection.Text == "左下--->右上")
                    {
                        for (int row = 0; row < dirRows - k; row++)        ////窗口大小与图像行列数关系
                        {
                            for (int col = 0; col < dirColumns - k; col++) ////窗口大小与图像行列数关系
                            {
                                sum += Math.Pow((b[row, col] - b[row + k, col + k]), 2);
                                a    = a + 1;
                            }
                        }
                    }
                    if (cbBDirection.Text == "左上--->右下")
                    {
                        for (int row = 0; row < dirRows - k; row++)        ////窗口大小与图像行列数关系
                        {
                            for (int col = 0; col < dirColumns - k; col++) ////窗口大小与图像行列数关系
                            {
                                sum += Math.Pow((b[row, col + k] - b[row + k, col]), 2);
                                a    = a + 1;
                            }
                        }
                    }
                    mean = sum / (2 * a);
                    ss  += mean + " " + a + " " + k + "\r\n";

                    String       Strsavefile = savename;
                    StreamWriter sw          = new StreamWriter(Strsavefile);
                    sw.WriteLine(ss);
                    sw.Close();
                    k = k + 1;
                }
            }
            stw.Stop();
            MessageBox.Show("程序共运行时间:" + stw.Elapsed.Minutes.ToString() + "分钟" + stw.Elapsed.Seconds.ToString() + "秒");
        }