예제 #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
            //{
            //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
            myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
            int          pBHeight = pPixelBlock.Height;
            int          pBWidth = pPixelBlock.Width;
            IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
            IPnt         pbBigSize = new PntClass();
            IPnt         pbBigLoc = new PntClass();
            int          pbBigWd = pBWidth + clms - 1;
            int          pbBigHt = pBHeight + rws - 1;
            int          l, t;

            l = clms / 2;
            t = rws / 2;
            //Console.WriteLine("lt = " + (pTlc.X - l).ToString() + ":" + (pTlc.Y - t).ToString());
            pbBigSize.SetCoords(pbBigWd, pbBigHt);
            pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t));
            IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize);

            myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig);
            updatePixelRectangle(ipPixelBlock, pbBig);
            //}
            //catch (Exception e)
            //{
            //Console.WriteLine(e.ToString());
            //System.Windows.Forms.MessageBox.Show(e.ToString());
            //Console.WriteLine(pTlc.X.ToString() + ":" + pTlc.Y.ToString());

            //}
        }
예제 #2
0
        public void Write(float?[,] rasterValue, string format)
        {
            FileHelper.DeleteFile(_workSpace, _fileName, ".tif", ".tfw", ".tif.aux");
            IRasterWorkspace2 rasterWs = OpenRasterWorkspace();

            if (rasterWs == null)
            {
                throw new NullReferenceException("栅格文件打开失败");
            }
            IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(_fileName + ".tif",
                                                                        format, RasterInfo.OriginPoint, RasterInfo.Width, RasterInfo.Height,
                                                                        RasterInfo.XCellSize, RasterInfo.YCellSize, 1, rstPixelType.PT_FLOAT,
                                                                        RasterInfo.SpatialReference, true);
            IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
            var rasterBand  = rasterBands.Item(0);
            var rasterProps = (IRasterProps)rasterBand;

            //Set NoData if necessary. For a multiband image, NoData value needs to be set for each band.
            rasterProps.NoDataValue = -1;
            //Create a raster from the dataset.
            IRaster raster = rasterDataset.CreateDefaultRaster();

            //Create a pixel block.
            IPnt blocksize = new PntClass();

            blocksize.SetCoords(RasterInfo.Width, RasterInfo.Height);
            IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3;
            //Populate some pixel values to the pixel block.
            var pixels = (Array)pixelblock.get_PixelData(0);

            for (int i = 0; i < RasterInfo.Width; i++)
            {
                for (int j = 0; j < RasterInfo.Height; j++)
                {
                    if (rasterValue[i, j].HasValue)
                    {
                        pixels.SetValue((float)rasterValue[i, j], i, j);
                    }
                    else
                    {
                        pixels.SetValue(-1, i, j);
                    }
                }
            }

            pixelblock.set_PixelData(0, pixels);

            //Define the location that the upper left corner of the pixel block is to write.
            IPnt upperLeft = new PntClass();

            upperLeft.SetCoords(0, 0);

            //Write the pixel block.
            IRasterEdit rasterEdit = (IRasterEdit)raster;

            rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);

            //Release rasterEdit explicitly.
            Marshal.ReleaseComObject(rasterEdit);
        }
예제 #3
0
        /// <summary>
        /// Get the pixel values in a region of the input raster.
        /// </summary>
        /// <param name="tlCorner"></param>
        /// <param name="brCorner"></param>
        /// <param name="raster"></param>
        /// <returns></returns>
        public static double[,] GetValues(Position tlCorner, Position brCorner, IRaster raster)
        {
            int colCount = brCorner.Column - tlCorner.Column + 1;
            int rowCount = brCorner.Row - tlCorner.Row + 1;

            IPnt regionSize = new PntClass();

            regionSize.SetCoords(colCount, rowCount);
            IPixelBlock pixelBlock = raster.CreatePixelBlock(regionSize);
            IPnt        tl         = new PntClass();

            tl.SetCoords(tlCorner.Column, tlCorner.Row);
            raster.Read(tl, pixelBlock);

            double[,] values = new double[colCount, rowCount];
            for (int x = 0; x < colCount; x++)
            {
                for (int y = 0; y < rowCount; y++)
                {
                    Pixel cell = Editor.Edits[x + tlCorner.Column, y + tlCorner.Row];

                    if (cell == null)
                    {
                        values[x, y] = Convert.ToDouble(pixelBlock.GetVal(0, x, y));
                    }
                    else
                    {
                        values[x, y] = cell.NewValue;
                    }
                }
            }
            return(values);
        }
예제 #4
0
        /// <summary>
        /// 读取栅格数据。
        /// </summary>
        public static float[,] ReadRaster(IRasterLayer rasterLayer, float nullValue)
        {
            int columnCount = rasterLayer.ColumnCount;
            int rowCount    = rasterLayer.RowCount;

            IRaster2 raster  = rasterLayer.Raster as IRaster2;
            IPnt     fromPnt = new PntClass();

            fromPnt.SetCoords(0, 0);
            IPnt blockSize = new PntClass();

            blockSize.SetCoords(columnCount, rowCount);
            IPixelBlock pixedBlock = ((IRaster)raster).CreatePixelBlock(blockSize);

            rasterLayer.Raster.Read(fromPnt, pixedBlock);

            float[,] data = new float[rowCount, columnCount];

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    object value = pixedBlock.GetVal(0, j, i);
                    if (value != null)
                    {
                        data[i, j] = Convert.ToSingle(value);
                    }
                    else
                    {
                        data[i, j] = nullValue;
                    }
                }
            }
            return(data);
        }
예제 #5
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());
     }
 }
예제 #6
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.


                //Console.WriteLine("Before Read");
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                IRaster mosRs   = (IRaster)mos;
                IPnt    pntSize = new PntClass();
                pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height);
                IPixelBlock pb = mosRs.CreatePixelBlock(pntSize);
                mosRs.Read(pTlc, pb);
                for (int i = 0; i < pb.Planes; i++)
                {
                    pPixelBlock.set_SafeArray(i, pb.get_SafeArray(i));
                }
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of mosaic Function. " + exc.Message, exc);
                Console.WriteLine(exc.ToString());
            }
        }
예제 #7
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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         IPixelBlock3 pPixelBlock3 = (IPixelBlock3)pPixelBlock;
         IRaster2     inRs2 = (IRaster2)inrs;
         IRaster2     outRs2 = (IRaster2)outrs;
         double       mx, my;
         outRs2.PixelToMap((int)pTlc.X, (int)pTlc.Y, out mx, out my);
         int clm, rw;
         inRs2.MapToPixel(mx, my, out clm, out rw);
         IPnt pntLoc  = new PntClass();
         pntLoc.SetCoords(clm, rw);
         IPnt pntSize = new PntClass();
         pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height);
         IPixelBlock3 pBIn = (IPixelBlock3)inrs.CreatePixelBlock(pntSize);
         inrs.Read(pntLoc, (IPixelBlock)pBIn);
         for (int p = 0; p < pPixelBlock.Planes; p++)
         {
             pPixelBlock3.set_PixelData(p, pBIn.get_PixelData(p));
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
예제 #8
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.

                //System.Array noDataValueArr = (System.Array)((IRasterProps)inrsBandsCoef).NoDataValue;
                //Console.WriteLine("Before Read");
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                //Console.WriteLine("After Read");
                int  pBHeight = pPixelBlock.Height;
                int  pBWidth  = pPixelBlock.Width;
                IPnt pbSize   = new PntClass();
                pbSize.SetCoords(pBWidth, pBHeight);
                IPixelBlock3 outPb = (IPixelBlock3)inrsBandsCoef.CreatePixelBlock(pbSize);//independent variables
                inrsBandsCoef.Read(pTlc, (IPixelBlock)outPb);
                int          pBRowIndex   = 0;
                int          pBColIndex   = 0;
                IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                //System.Array[] pArr = new System.Array[outPb.Planes];
                //for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                //{
                //    System.Array pixelValues = (System.Array)(outPb.get_PixelData(coefnBand));
                //    pArr[coefnBand] = pixelValues;
                //}
                System.Array pValues = (System.Array)ipPixelBlock.get_PixelData(0);//(System.Array)(td);
                for (int i = pBRowIndex; i < pBHeight; i++)
                {
                    for (int k = pBColIndex; k < pBWidth; k++)
                    {
                        double[] xVls = new double[outPb.Planes];
                        bool     ndT  = true;
                        for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                        {
                            //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(coefnBand));
                            object pObj = outPb.GetVal(coefnBand, k, i);

                            if (pObj == null)
                            {
                                ndT = false;
                                break;
                            }
                            float pixelValue = Convert.ToSingle(pObj);
                            xVls[coefnBand] = pixelValue;
                        }
                        if (ndT)
                        {
                            int c = cluster.computNew(xVls);
                            pValues.SetValue(c, k, i);
                        }
                    }
                }
                ipPixelBlock.set_PixelData(0, pValues);
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of Cluster Function. " + exc.Message, exc);
                Console.WriteLine(exc.ToString());
            }
        }
예제 #9
0
        public void executeRegionGroup()
        {
            createOutRaster();
            if (OutRaster == null)
            {
                Console.WriteLine("not all inputs specified");
                return;
            }
            IWorkspaceEdit wksE   = (IWorkspaceEdit)vWks;
            bool           weEdit = true;

            if (wksE.IsBeingEdited())
            {
                weEdit = false;
            }
            else
            {
                wksE.StartEditing(false);
            }
            wksE.StartEditOperation();
            Console.WriteLine("Counter = " + counter.ToString());
            int readclmsStep = PixelBlockWidth + 2;
            int readrwsStep  = PixelBlockHeight + 2;

            IPnt outloc = new PntClass();
            IPnt inloc  = new PntClass();

            try
            {
                for (int rp = 0; rp < rws; rp += PixelBlockHeight)     //rws
                {
                    for (int cp = 0; cp < clms; cp += PixelBlockWidth) //clms
                    {
                        Console.WriteLine("Write Raster location = " + cp.ToString() + ":" + rp.ToString());
                        Console.WriteLine("Read Raster location = " + (cp - 1).ToString() + ":" + (rp - 1).ToString());
                        outloc.SetCoords(cp, rp);
                        inloc.SetCoords(cp - 1, rp - 1);
                        middleRowColumn(outloc);
                    }
                }
                IRaster2            rs2     = (IRaster2)OutRaster;
                IRasterDataset      rsDset  = rs2.RasterDataset;
                IRasterDatasetEdit2 rsDsetE = (IRasterDatasetEdit2)rsDset;
                rsDsetE.AlterAttributeTable(vatTable);
                wksE.StopEditOperation();
                if (weEdit)
                {
                    wksE.StopEditing(true);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
            }
            return;
        }
예제 #10
0
        private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock)
        {
            System.Array[]        outPixelValuesArr   = new System.Array[ipPixelBlock.Planes];
            List <System.Array[]> inPixelValuesArrLst = new List <System.Array[]>();
            List <System.Array>   inPixelNoDataArrLst = new List <System.Array>();
            IPnt pntSize = new PntClass();

            pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height);
            ISpatialFilter spFlt = new SpatialFilterClass();

            spFlt.Geometry      = (IGeometry)env;
            spFlt.GeometryField = ftrCls.ShapeFieldName;
            spFlt.SpatialRel    = esriSpatialRelEnum.esriSpatialRelOverlaps;
            IFeatureCursor fCur   = ftrCls.Search(spFlt, false);
            int            fIndex = ftrCls.FindField("catIndex");
            IFeature       ftr    = fCur.NextFeature();

            for (int i = 0; i < ipPixelBlock.Planes; i++)
            {
                outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i);
            }
            while (ftr != null)
            {
                int         rsIndex = System.Convert.ToInt32(ftr.get_Value(fIndex));
                IRaster     rs = inrs[rsIndex];
                IPixelBlock inputPb = rs.CreatePixelBlock(pntSize);
                IRaster2    rs2 = (IRaster2)rs;
                int         pClm, pRw;
                rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw);
                IPnt tlc = new PntClass();
                tlc.SetCoords(pClm, pRw);
                rs.Read(tlc, inputPb);
                System.Array[] inPixelValuesArr = new System.Array[inputPb.Planes];
                for (int i = 0; i < inputPb.Planes; i++)
                {
                    inPixelValuesArr[i] = (System.Array)inputPb.get_SafeArray(i);
                }
                inPixelNoDataArrLst.Add((System.Array)((IRasterProps)rs).NoDataValue);
                inPixelValuesArrLst.Add(inPixelValuesArr);
                ftr = fCur.NextFeature();
            }
            for (int i = 0; i < outPixelValuesArr.Length; i++)
            {
                for (int r = 0; r < ipPixelBlock.Height; r++)
                {
                    for (int c = 0; c < ipPixelBlock.Width; c++)
                    {
                        double vl = getValue(i, c, r, inPixelValuesArrLst, inPixelNoDataArrLst);
                        outPixelValuesArr[i].SetValue(vl, c, r);
                    }
                }
            }
            for (int i = 0; i < outPixelValuesArr.Length; i++)
            {
                ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]);
            }
        }
예제 #11
0
        //获取最大最小栅格值
        private void SetMaxMinValue(int flag)
        {
            if (flag == 1)
            {
                textBoxMax.Text = "高:255";
                textBoxMin.Text = "低:0";
                return;
            }
            uint valueMax = 50;
            uint valueMin = 50;

            IRasterLayer pRasterLayer = m_layer as IRasterLayer;
            IRaster      pRaster      = pRasterLayer.Raster;
            IRasterProps pRasterProps = pRaster as IRasterProps;
            int          Height       = pRasterProps.Height;
            int          Width        = pRasterProps.Width;
            double       dX           = pRasterProps.MeanCellSize().X;
            double       dY           = pRasterProps.MeanCellSize().Y; //栅格的高度
            IEnvelope    extent       = pRasterProps.Extent;           //当前栅格数据集的范围
            rstPixelType pixelType    = pRasterProps.PixelType;        //当前栅格像素类型
            IPnt         pntSize      = new PntClass();

            pntSize.SetCoords(dX, dY);


            IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
            IPnt        pnt        = new PntClass();

            for (int i = 0; i < Height; i += 10)
            {
                for (int j = 0; j < Width; j += 10)
                {
                    pnt.SetCoords(i, j);
                    pRaster.Read(pnt, pixelBlock);
                    if (pixelBlock != null)
                    {
                        object obj  = pixelBlock.GetVal(0, 0, 0);
                        uint   temp = Convert.ToUInt32(obj);

                        if (temp > valueMax)
                        {
                            valueMax = temp;
                        }
                        else if (temp < valueMin)
                        {
                            valueMin = temp;
                        }
                    }
                }
            }
            textBoxMax.Text = "高:" + valueMax.ToString();
            textBoxMin.Text = "低:" + valueMin.ToString();
        }
예제 #12
0
        /// <summary>
        /// 栅格操作修改栅格的值
        /// </summary>
        /// <param name="pRasterDataset2"></param>
        public static void ChangeRasterValue(IRasterDataset2 pRasterDataset2)
        {
            //设置读取栅格的大小
            IRaster2 pRaster2 = pRasterDataset2.CreateFullRaster() as IRaster2;
            IPnt pPntBlock = new PntClass();
            pPntBlock.X = 128;
            pPntBlock.Y = 128;
            IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(pPntBlock);
            IRasterEdit pRasterEdit = pRaster2 as IRasterEdit;

            if (pRasterEdit.CanEdit())
            {
            //循环波段,长和宽度
            IRasterBandCollection pBands = pRasterDataset2 as IRasterBandCollection;
            IPixelBlock3 pPixelblock3 = null;
            int pBlockwidth = 0;
            int pBlockheight = 0;
            System.Array pixels;
            object pValue;
            long pBandCount = pBands.Count;
            //
            IRasterProps pRasterProps = pRaster2 as IRasterProps;
            object nodata = pRasterProps.NoDataValue;
            do
            {
            pPixelblock3 = pRasterCursor.PixelBlock as IPixelBlock3;
            pBlockwidth = pPixelblock3.Width;
            pBlockheight = pPixelblock3.Height;
            for (int k = 0; k < pBandCount; k++)
            {
                pixels = (System.Array)pPixelblock3.get_PixelData(k);
                for (int i = 0; i < pBlockwidth; i++)
                {
                    for (int j = 0; j < pBlockheight; j++)
                    {
                        pValue = pixels.GetValue(i, j);
                        int value = Convert.ToInt32(pValue);
                        if (Convert.ToInt32(pValue) != 3)
                        {
                            pixels.SetValue(Convert.ToByte(0), i, j);
                        }
                    }
                }
                pPixelblock3.set_PixelData(k, pixels);
            }
            pPntBlock = pRasterCursor.TopLeft;
            pRasterEdit.Write(pPntBlock, (IPixelBlock)pPixelblock3);

            } while (pRasterCursor.Next());
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);

            }
        }
예제 #13
0
        private bool writeBlockDataToFile(Point2D ptLeftTop, byte[,] dbData, int[] nSize, IRaster pRaster /*, IRasterEdit rasterEdit*/)
        {
            if (pRaster == null)
            {
                return(false);
            }

            try
            {
                //Create a pixel block using the weight and height of the raster dataset.
                //If the raster dataset is large, a smaller pixel block should be used.
                //Refer to the topic "How to access pixel data using a raster cursor".
                int nWidth  = nSize[0];
                int nHeight = nSize[1];

                IPnt blocksize = new PntClass();
                blocksize.SetCoords(nWidth, nHeight);
                IPixelBlock3 pixelblock = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3;

                //Populate some pixel values to the pixel block.
                System.Array pixels;
                pixels = (System.Array)pixelblock.get_PixelData(0);
                for (int i = 0; i < nWidth; i++)
                {
                    for (int j = 0; j < nHeight; j++)
                    {
                        pixels.SetValue(dbData[i, j], i, j);
                    }
                }

                pixelblock.set_PixelData(0, (System.Array)pixels);

                //Define the location that the upper left corner of the pixel block is to write.
                IPnt upperLeft = new PntClass();
                upperLeft.SetCoords(ptLeftTop.X, ptLeftTop.Y);

                //Write the pixel block.
                IRasterEdit rasterEdit = (IRasterEdit)pRaster;
                rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
                rasterEdit.Refresh();

                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
                return(false);
            }

            GC.Collect();
            return(true);
        }
예제 #14
0
        /// <summary>
        /// 读取数据并记录非空数据的行列号。
        /// </summary>
        public static float[,] ReadRasterAndGetNotNullRowColumn(IRasterLayer rasterLayer, out StructRasterMetaData structRasterMetaData,
                                                                out List <int> notNullRows, out List <int> notNullColumns)
        {
            int rowCount, columnCount;

            notNullRows    = new List <int>();
            notNullColumns = new List <int>();

            structRasterMetaData.ColumnCount = rasterLayer.ColumnCount;
            columnCount = structRasterMetaData.ColumnCount;
            structRasterMetaData.RowCount = rasterLayer.RowCount;
            rowCount = structRasterMetaData.RowCount;

            IEnvelope visiableExtent = rasterLayer.VisibleExtent;

            structRasterMetaData.XMin        = visiableExtent.XMin;
            structRasterMetaData.XMax        = visiableExtent.XMax;
            structRasterMetaData.YMin        = visiableExtent.YMin;
            structRasterMetaData.YMax        = visiableExtent.YMax;
            structRasterMetaData.NoDataValue = -9999f;

            IRaster2 raster  = rasterLayer.Raster as IRaster2;
            IPnt     fromPnt = new PntClass();

            fromPnt.SetCoords(0, 0);
            IPnt blockSize = new PntClass();

            blockSize.SetCoords(columnCount, rowCount);
            IPixelBlock pixelBlock = ((IRaster)raster).CreatePixelBlock(blockSize);

            rasterLayer.Raster.Read(fromPnt, pixelBlock);
            float[,] data = new float[rowCount, columnCount];

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    object value = pixelBlock.GetVal(0, j, i);
                    if (value != null)
                    {
                        data[i, j] = Convert.ToSingle(value);
                        notNullRows.Add(i);
                        notNullColumns.Add(j);
                    }
                    else
                    {
                        data[i, j] = structRasterMetaData.NoDataValue;
                    }
                }
            }
            return(data);
        }
예제 #15
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
     {
         System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         int          pBHeight = pPixelBlock.Height;
         int          pBWidth = pPixelBlock.Width;
         IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
         IPnt         pbBigSize = new PntClass();
         IPnt         pbBigLoc = new PntClass();
         int          pbBigWd = pBWidth + clms;
         int          pbBigHt = pBHeight + rws;
         int          l, t;
         l = (clms - 1) / 2;
         t = (rws - 1) / 2;
         //Console.WriteLine("lt = " + (pTlc.X - l).ToString() + ":" + (pTlc.Y - t).ToString());
         pbBigSize.SetCoords(pbBigWd, pbBigHt);
         pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t));
         IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize);
         myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig);
         for (int nBand = 0; nBand < pbBig.Planes; nBand++)
         {
             System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand));
             rstPixelType rsp         = ipPixelBlock.get_PixelType(nBand);
             //System.Array pixelValuesBig = (System.Array)(pbBig.get_PixelData(nBand));
             for (int r = 0; r < pBHeight; r++)
             {
                 for (int c = 0; c < pBWidth; c++)
                 {
                     object objVl = ipPixelBlock.GetVal(nBand, c, r);
                     if (objVl == null)
                     {
                         continue;
                     }
                     else
                     {
                         float  outVl = System.Convert.ToSingle(getTransformedValue(pbBig, c + l, r + t, nBand));
                         object newVl = rasterUtil.getSafeValue(outVl, rsp);
                         //Console.WriteLine(outVl.ToString());
                         pixelValues.SetValue(newVl, c, r);
                     }
                 }
             }
             ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
예제 #16
0
        /// <summary>
        /// 栅格操作修改栅格的值
        /// </summary>
        /// <param name="pRasterDataset2"></param>
        public static void ChangeRasterValue(IRasterDataset2 pRasterDataset2)
        {
            //设置读取栅格的大小
            IRaster2 pRaster2  = pRasterDataset2.CreateFullRaster() as IRaster2;
            IPnt     pPntBlock = new PntClass();

            pPntBlock.X = 128;
            pPntBlock.Y = 128;
            IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(pPntBlock);
            IRasterEdit   pRasterEdit   = pRaster2 as IRasterEdit;

            if (pRasterEdit.CanEdit())
            {
                //循环波段,长和宽度
                IRasterBandCollection pBands       = pRasterDataset2 as IRasterBandCollection;
                IPixelBlock3          pPixelblock3 = null;
                int          pBlockwidth           = 0;
                int          pBlockheight          = 0;
                System.Array pixels;
                object       pValue;
                long         pBandCount = pBands.Count;
                //
                IRasterProps pRasterProps = pRaster2 as IRasterProps;
                object       nodata       = pRasterProps.NoDataValue;
                do
                {
                    pPixelblock3 = pRasterCursor.PixelBlock as IPixelBlock3;
                    pBlockwidth  = pPixelblock3.Width;
                    pBlockheight = pPixelblock3.Height;
                    for (int k = 0; k < pBandCount; k++)
                    {
                        pixels = (System.Array)pPixelblock3.get_PixelData(k);
                        for (int i = 0; i < pBlockwidth; i++)
                        {
                            for (int j = 0; j < pBlockheight; j++)
                            {
                                pValue = pixels.GetValue(i, j);
                                int value = Convert.ToInt32(pValue);
                                if (Convert.ToInt32(pValue) != 3)
                                {
                                    pixels.SetValue(Convert.ToByte(0), i, j);
                                }
                            }
                        }
                        pPixelblock3.set_PixelData(k, pixels);
                    }
                    pPntBlock = pRasterCursor.TopLeft;
                    pRasterEdit.Write(pPntBlock, (IPixelBlock)pPixelblock3);
                } while (pRasterCursor.Next());
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);
            }
        }
예제 #17
0
        /// <summary>
        /// Gets value of pixel at the specified position.
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="raster"></param>
        /// <returns></returns>
        public static double GetValue(Position pos, IRaster raster)
        {
            IPnt regionSize = new PntClass();

            regionSize.SetCoords(1, 1);
            IPixelBlock pixelBlock = raster.CreatePixelBlock(regionSize);
            IPnt        tl         = new PntClass();

            tl.SetCoords(pos.Column, pos.Row);
            raster.Read(tl, pixelBlock);

            return(Convert.ToDouble(pixelBlock.GetVal(0, 0, 0)));
        }
예제 #18
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
     {
         //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         int          pBHeight = pPixelBlock.Height;
         int          pBWidth = pPixelBlock.Width;
         IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
         IPnt         pbBigSize = new PntClass();
         IPnt         pbBigLoc = new PntClass();
         int          pbBigWd = pBWidth + clms;
         int          pbBigHt = pBHeight + rws;
         int          l, t;
         l = clms / 2;
         t = rws / 2;
         pbBigSize.SetCoords(pbBigWd, pbBigHt);
         pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t));
         IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize);
         myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig);
         noDataValue = float.MinValue;//System.Convert.ToSingle(noDataValueArr.GetValue(0));
         for (int nBand = 0; nBand < ipPixelBlock.Planes; nBand++)
         {
             rstPixelType pbt         = ipPixelBlock.get_PixelType(nBand);
             System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand));
             for (int r = 0; r < pBHeight; r++)
             {
                 for (int c = 0; c < pBWidth; c++)
                 {
                     object inVlobj = ipPixelBlock.GetVal(nBand, c, r);
                     if (inVlobj == null)
                     {
                         continue;
                     }
                     else
                     {
                         updateGLCMDic(pbBig, c, r, nBand);
                         float  outVl = System.Convert.ToSingle(getTransformedValue(countDic));
                         object newVl = rasterUtil.getSafeValue(outVl, pbt);
                         pixelValues.SetValue(newVl, c, r);
                     }
                 }
             }
             ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
예제 #19
0
        private void createDictionaryArray()
        {
            IEnvelope env1 = referenceRaster.RasterInfo.Extent;
            IEnvelope env2 = transformRaster.RasterInfo.Extent;

            env1.Intersect(env2);
            clipGeo = (IGeometry)env1;
            IFunctionRasterDataset minRs = rsUtil.calcArithmaticFunction(referenceRaster, transformRaster, esriRasterArithmeticOperation.esriRasterMinus);

            clipRs = rsUtil.clipRasterFunction(minRs, clipGeo, esriRasterClippingType.esriRasterClippingOutside);
            IPnt pntSize = new PntClass();

            pntSize.SetCoords(512, 512);
            IRasterCursor rsCur = ((IRaster2)rsUtil.createRaster(clipRs)).CreateCursorEx(pntSize);
            int           pCnt  = rsCur.PixelBlock.Planes;

            do
            {
                IPixelBlock pbMinBlock = rsCur.PixelBlock;
                for (int r = 0; r < pbMinBlock.Height; r++)
                {
                    for (int c = 0; c < pbMinBlock.Width; c++)
                    {
                        for (int p = 0; p < pCnt; p++)
                        {
                            object vlObj = pbMinBlock.GetVal(p, c, r);
                            if (vlObj == null)
                            {
                                continue;
                            }
                            else
                            {
                                int vl = System.Convert.ToInt32(vlObj);
                                Dictionary <int, int> cDic = difDic[p];
                                int cnt = 0;
                                if (!cDic.TryGetValue(vl, out cnt))
                                {
                                    cDic.Add(vl, 1);
                                }
                                else
                                {
                                    cDic[vl] = cnt + 1;
                                }
                                cellCount[p] += 1;
                            }
                        }
                    }
                }
            } while (rsCur.Next() == true);
        }
예제 #20
0
        void t_Tick(object sender, EventArgs e)
        {
            try
            {
                if (VariableMaintainer.IsNeedRefresh)
                {
                    string       simulationLayerName  = VariableMaintainer.CurrentFoucsMap.get_Layer(0).Name;
                    IRasterLayer simulationImageLayer = new RasterLayerClass();
                    if (VariableMaintainer.CurrentModel == EnumCurrentModel.Simulation_CA_LogisticRegression)
                    {
                        simulationImageLayer.CreateFromFilePath(VariableMaintainer.CurrentFormLogisticCAWizard.OutputFolder + @"\" + simulationLayerName);
                        ArcGISOperator.WriteRaster(simulationImageLayer, VariableMaintainer.CurrentFormLogisticCAWizard.SimulationImage);
                    }
                    else if (VariableMaintainer.CurrentModel == EnumCurrentModel.Simulation_CA_ANN)
                    {
                        simulationImageLayer.CreateFromFilePath(VariableMaintainer.CurrentFormANNCAWizard.OutputFolder + @"\" + simulationLayerName);
                        ArcGISOperator.WriteRaster(simulationImageLayer, VariableMaintainer.CurrentFormANNCAWizard.SimulationImage);
                    }
                    else if (VariableMaintainer.CurrentModel == EnumCurrentModel.Simulation_CA_DT)
                    {
                        simulationImageLayer.CreateFromFilePath(VariableMaintainer.CurrentFormDTCAWizard.OutputFolder + @"\" + simulationLayerName);
                        ArcGISOperator.WriteRaster(simulationImageLayer, VariableMaintainer.CurrentFormDTCAWizard.SimulationImage);
                    }

                    IRasterLayer l       = ArcGISOperator.GetRasterLayerByName(simulationLayerName);
                    IRaster2     raster  = simulationImageLayer.Raster as IRaster2;
                    IPnt         fromPnt = new PntClass();
                    fromPnt.SetCoords(0, 0);
                    IPnt blockSize = new PntClass();
                    blockSize.SetCoords(simulationImageLayer.ColumnCount, simulationImageLayer.RowCount);
                    IPixelBlock pixelBlock = ((IRaster)raster).CreatePixelBlock(blockSize);
                    l.Raster.Read(fromPnt, pixelBlock);
                    ((IRasterEdit)l.Raster).Refresh();

                    IActiveView activeView = VariableMaintainer.CurrentFoucsMap as IActiveView;
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                    VariableMaintainer.IsNeedRefresh = false;
                }

                if (VariableMaintainer.IsSimulationFinished)
                {
                    VariableMaintainer.CurrentTimer.Stop();
                    VariableMaintainer.IsSimulationFinished = false;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
예제 #21
0
        private IPixelBlock GetAllPixelBlock(int width, int height)
        {
            IRaster pRaster = GetRaster();
            IPnt    pnt     = new PntClass();

            pnt.SetCoords(0, 0);
            IPnt pntSize = new PntClass();

            pntSize.SetCoords(width, height);
            IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);

            pRaster.Read(pnt, pixelBlock);
            return(pixelBlock);
        }
예제 #22
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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         //System.Windows.Forms.MessageBox.Show("Read resized raster");
         int          pBHeight     = pPixelBlock.Height;
         int          pBWidth      = pPixelBlock.Width;
         IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
         IPnt         pbBigSize    = new PntClass();
         IPnt         pbBigLoc     = new PntClass();
         int          pbBigWd      = pBWidth * cells;
         int          pbBigHt      = pBHeight * cells;
         pbBigSize.SetCoords(pbBigWd, pbBigHt);
         pbBigLoc.SetCoords((pTlc.X * cells), (pTlc.Y * cells));
         IPixelBlock pbOrig = myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize);
         //System.Windows.Forms.MessageBox.Show("Read original Raster");
         myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, pbOrig);
         IPixelBlock3 pbBig = (IPixelBlock3)pbOrig;
         //int cnt = 0;
         for (int nBand = 0; nBand < ipPixelBlock.Planes; nBand++)
         {
             //Console.WriteLine(ipPixelBlock.get_PixelType(nBand).ToString());
             //object noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand));
             //System.Array pixelValuesBig = (System.Array)(pbBig.get_PixelData(nBand));
             rstPixelType pTy         = pbBig.get_PixelType(nBand);
             System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand));
             for (int r = 0; r < pBHeight; r++)
             {
                 for (int c = 0; c < pBWidth; c++)
                 {
                     object objVl = ipPixelBlock.GetVal(nBand, c, r);
                     if (objVl != null)
                     {
                         object outVl = getTransformedValue(pbBig, nBand, c, r, cells);
                         object newVl = rasterUtil.getSafeValue(outVl, pTy);
                         //System.Windows.Forms.MessageBox.Show(outVl.ToString());
                         pixelValues.SetValue(newVl, c, r);
                     }
                 }
             }
             ipPixelBlock.set_PixelData(nBand, pixelValues);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
예제 #23
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 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);
         myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb);
         int          pBRowIndex   = 0;
         int          pBColIndex   = 0;
         IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
         System.Array outArr       = (System.Array)ipPixelBlock.get_PixelData(0);
         rstPixelType pty          = ipPixelBlock.get_PixelType(0);
         double[]     vArr         = new double[outPb.Planes];
         for (int i = pBRowIndex; i < pBHeight; i++)
         {
             for (int k = pBColIndex; k < pBWidth; k++)
             {
                 bool checkVl = true;
                 for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                 {
                     object vlObj = outPb.GetVal(0, k, i);
                     if (vlObj == null)
                     {
                         checkVl = false;
                         break;
                     }
                     vArr[coefnBand] = System.Convert.ToDouble(vlObj);
                 }
                 if (checkVl)
                 {
                     double tVl   = glm.computeNew(vArr);
                     object newVl = rasterUtil.getSafeValue(tVl, pty);
                     outArr.SetValue(newVl, k, i);
                 }
             }
         }
         ipPixelBlock.set_PixelData(0, outArr);
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method of GLM Function. " + exc.Message, exc);
         Console.WriteLine(exc.ToString());
     }
 }
예제 #24
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;
     }
 }
예제 #25
0
        public void Write(float?[,] rasterValue, string format)
        {
            FileHelper.DeleteFile(_workSpace, _fileName, ".tif", ".tfw", ".tif.aux");  
            IRasterWorkspace2 rasterWs = OpenRasterWorkspace();
            if (rasterWs==null)
            {
                throw new NullReferenceException("栅格文件打开失败");
            }
            IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(_fileName+".tif",
            format, RasterInfo.OriginPoint, RasterInfo.Width, RasterInfo.Height, 
                RasterInfo.XCellSize, RasterInfo.YCellSize, 1, rstPixelType.PT_FLOAT,
            RasterInfo.SpatialReference, true);
            IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
            var rasterBand = rasterBands.Item(0);
            var rasterProps = (IRasterProps)rasterBand;
            //Set NoData if necessary. For a multiband image, NoData value needs to be set for each band.
            rasterProps.NoDataValue = -1;
            //Create a raster from the dataset.
            IRaster raster = rasterDataset.CreateDefaultRaster();

            //Create a pixel block.
            IPnt blocksize = new PntClass();
            blocksize.SetCoords(RasterInfo.Width, RasterInfo.Height);
            IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3;
            //Populate some pixel values to the pixel block.
            var pixels = (Array)pixelblock.get_PixelData(0);
            for (int i = 0; i < RasterInfo.Width; i++)
                for (int j = 0; j < RasterInfo.Height; j++)
                    if (rasterValue[i, j].HasValue)
                        pixels.SetValue((float)rasterValue[i, j], i, j);
                    else
                    {
                        pixels.SetValue(-1, i, j);
                    }

            pixelblock.set_PixelData(0, pixels);

            //Define the location that the upper left corner of the pixel block is to write.
            IPnt upperLeft = new PntClass();
            upperLeft.SetCoords(0, 0);

            //Write the pixel block.
            IRasterEdit rasterEdit = (IRasterEdit)raster;
            rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);

            //Release rasterEdit explicitly.
            Marshal.ReleaseComObject(rasterEdit);
        }
예제 #26
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);
        }
예제 #27
0
        public bool readBlockDataToFile(Point2D ptLeftTop, ref double[,] dbData, IRaster pRaster /*, IRasterEdit rasterEdit*/)
        {
            if (pRaster == null)
            {
                return(false);
            }

            try
            {
                //Create a pixel block using the weight and height of the raster dataset.
                //If the raster dataset is large, a smaller pixel block should be used.
                //Refer to the topic "How to access pixel data using a raster cursor".
                int nWidth  = dbData.GetLength(0);
                int nHeight = dbData.GetLength(1);

                IPnt blocksize = new PntClass();
                blocksize.SetCoords(nWidth, nHeight);

                //Define the location that the upper left corner of the pixel block is to write.
                IPnt upperLeft = new PntClass();
                upperLeft.SetCoords(ptLeftTop.X, ptLeftTop.Y);
                IPixelBlock3 pixelblock = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3;
                pRaster.Read(upperLeft, pixelblock as IPixelBlock);

                //Populate some pixel values to the pixel block.
                System.Array pixels;
                pixels = (System.Array)pixelblock.get_PixelData(0);


                for (int i = 0; i < nWidth; i++)
                {
                    for (int j = 0; j < nHeight; j++)
                    {
                        dbData[i, j] = Convert.ToDouble(pixels.GetValue(i, j));//.GetType().;
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
                return(false);
            }

            GC.Collect();
            return(true);
        }
예제 #28
0
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="xIndex">水平方向的序号</param>
        /// <param name="yIndex">垂直方向的序号</param>
        /// <returns>获取的值,如果为null,表明该位置没有数据</returns>
        public object Read(int xIndex, int yIndex)
        {
            IRaster pRaster = GetRaster();
            IPnt    pnt     = new PntClass();

            pnt.SetCoords(xIndex, yIndex);
            IPnt pntSize = new PntClass();

            pntSize.SetCoords(1, 1);
            IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);

            pRaster.Read(pnt, pixelBlock);
            object obj = pixelBlock.GetVal(0, 0, 0);

            return(obj);
        }
        //标注提取的未扩展的湿地对象的代表性点
        public static void labelObjPoint(IGeoDataset pGeodataset, int objVal, string errstring)
        {
            IRaster           pRaster      = pGeodataset as IRaster;
            IRaster2          pRaster2     = pRaster as IRaster2;
            IRasterProps      pRasterProps = pRaster as IRasterProps;
            ISpatialReference pSR          = pGeodataset.SpatialReference;
            //获取图层的行列值
            int height = pRasterProps.Height;
            int width  = pRasterProps.Width;

            //定义并初始化数组,用于存储栅格内的所有像元值
            double[,] PixelVal = new double[height, width];
            //这是像素块大小
            IPnt blocksize = new PntClass();

            blocksize.SetCoords(256, 256);
            IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(blocksize);

            //存储像素块的行列值、像素值
            System.Array pixels;
            IPixelBlock3 pPixelBlock3;

            do
            {
                int xunit = (int)pRasterCursor.TopLeft.X;
                int yunit = (int)pRasterCursor.TopLeft.Y;
                pPixelBlock3 = pRasterCursor.PixelBlock as IPixelBlock3;
                pixels       = (Array)pPixelBlock3.get_PixelData(0);
                for (int i = 0; i < pPixelBlock3.Height; i++)
                {
                    for (int j = 0; j < pPixelBlock3.Width; j++)
                    {
                        PixelVal[yunit + i, xunit + j] = Convert.ToDouble(pixels.GetValue(j, i));
                        if (PixelVal[yunit + i, xunit + j] == objVal)
                        {
                            IPoint pPoint = new PointClass();
                            pPoint.SpatialReference = pSR;
                            pPoint.X = pRaster2.ToMapX(xunit + j);
                            pPoint.Y = pRaster2.ToMapY(yunit + i);
                            ChkMarkPoint.insertChkPoint("cglc_chkmark", pPoint as IGeometry, errstring);
                            //MessageBox.Show("Neighborhood Relation Checking Complete...");
                            return;
                        }
                    }
                }
            } while (pRasterCursor.Next());
        }
예제 #30
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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         #region Load log object
         int pBHeight = pPixelBlock.Height;
         int pBWidth  = pPixelBlock.Width;
         Pnt pbSize   = new PntClass();
         pbSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height);
         IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
         System.Array outPutArr    = (System.Array)ipPixelBlock.get_PixelData(0);
         IPixelBlock3 CoefPb       = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);
         myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)CoefPb);
         for (int r = 0; r < ipPixelBlock.Height; r++)
         {
             for (int c = 0; c < ipPixelBlock.Width; c++)
             {
                 object vlObj = CoefPb.GetVal(0, c, r);
                 if (vlObj != null)
                 {
                     if ((float)vlObj > 0)
                     {
                         vlObj = CoefPb.GetVal(1, c, r);
                     }
                     else
                     {
                         vlObj = CoefPb.GetVal(2, c, r);
                     }
                     if (vlObj != null)
                     {
                         outPutArr.SetValue(vlObj, c, r);
                     }
                 }
             }
         }
         ipPixelBlock.set_PixelData(0, outPutArr);
         #endregion
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method of con Function. " + exc.Message, exc);
         throw myExc;
     }
 }
예제 #31
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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         IPnt pntSize = new PntClass();
         pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height);
         IPixelBlock vPb = myFunctionHelper2.Raster.CreatePixelBlock(pntSize);
         myFunctionHelper2.Read(pTlc, null, myFunctionHelper2.Raster, vPb);
         IPixelBlock3 pb3    = (IPixelBlock3)pPixelBlock;
         object       intArr = calcMeanShift((IPixelBlock3)vPb, pb3);
         pb3.set_PixelData(0, intArr);
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method Mean-shift Function. " + exc.Message, exc);
         throw myExc;
     }
 }
        public static bool disJointRelation(IGeoDataset pGeodataset, int disjointVal)
        {
            IRaster      pRaster      = pGeodataset as IRaster;
            IRaster2     pRaster2     = pRaster as IRaster2;
            IRasterProps pRasterProps = pRaster as IRasterProps;
            //获取图层的行列值
            int height = pRasterProps.Height;
            int width  = pRasterProps.Width;

            //定义并初始化数组,用于存储栅格内的所有像元值
            double[,] PixelVal = new double[height, width];
            //这是像素块大小
            IPnt blocksize = new PntClass();

            blocksize.SetCoords(256, 256);
            IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(blocksize);

            //存储像素块的行列值、像素值
            System.Array pixels;
            IPixelBlock3 pPixelBlock3;
            bool         disjoint = true;

            do
            {
                int xunit = (int)pRasterCursor.TopLeft.X;
                int yunit = (int)pRasterCursor.TopLeft.Y;
                pPixelBlock3 = pRasterCursor.PixelBlock as IPixelBlock3;
                pixels       = (Array)pPixelBlock3.get_PixelData(0);
                for (int i = 0; i < pPixelBlock3.Height; i++)
                {
                    for (int j = 0; j < pPixelBlock3.Width; j++)
                    {
                        PixelVal[yunit + i, xunit + j] = Convert.ToDouble(pixels.GetValue(j, i));
                        if (PixelVal[yunit + i, xunit + j] == disjointVal)
                        {
                            disjoint = false; //判断此幅数据是否满足邻近性
                            break;
                        }
                    }
                }
            } while (pRasterCursor.Next());
            return(disjoint);
        }
        /// <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());
            }
        }
예제 #34
0
 private IPixelBlock GetAllPixelBlock(int width,int height)
 {
     IRaster pRaster = GetRaster();
     IPnt pnt = new PntClass();
     pnt.SetCoords(0, 0);
     IPnt pntSize = new PntClass();
     pntSize.SetCoords(width, height);
     IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
     pRaster.Read(pnt, pixelBlock);
     return pixelBlock;
 }
 /// <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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         #region Load log object
         int pBHeight = pPixelBlock.Height;
         int pBWidth = pPixelBlock.Width;
         Pnt pbSize = new PntClass();
         pbSize.SetCoords(pPixelBlock.Width,pPixelBlock.Height);
         IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
         System.Array outPutArr = (System.Array)ipPixelBlock.get_PixelData(0);
         IPixelBlock3 CoefPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);
         myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)CoefPb);
         for (int r = 0; r < ipPixelBlock.Height; r++)
         {
             for (int c = 0; c < ipPixelBlock.Width; c++)
             {
                 object vlObj = CoefPb.GetVal(0, c, r);
                 if (vlObj != null)
                 {
                     if ((float)vlObj > 0)
                     {
                         vlObj = CoefPb.GetVal(1, c, r);
                     }
                     else
                     {
                         vlObj = CoefPb.GetVal(2, c, r);
                     }
                     if (vlObj != null)
                     {
                         outPutArr.SetValue(vlObj, c, r);
                     }
                 }
             }
         }
         ipPixelBlock.set_PixelData(0,outPutArr);
         #endregion
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method of con Function. " + exc.Message, exc);
         throw myExc;
     }
 }
        /// <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 resampeling.
        /// 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
            {
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                int pBHeight = pPixelBlock.Height;
                int pBWidth = pPixelBlock.Width;
                IPnt pbSize = new PntClass();
                pbSize.SetCoords(pBWidth, pBHeight);
                IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);//independent variables
                myFunctionHelperCoef.Read(pTlc,null,myFunctionHelperCoef.Raster, (IPixelBlock)outPb);
                int pBRowIndex = 0;
                int pBColIndex = 0;
                IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                System.Array[] cArr = new System.Array[ipPixelBlock.Planes];
                for (int outBand = 0; outBand < ipPixelBlock.Planes; outBand++)
                {
                    System.Array pixelValues = (System.Array)ipPixelBlock.get_PixelData(outBand);
                    cArr[outBand] = pixelValues;
                }
                for (int i = pBRowIndex; i < pBHeight; i++)
                {
                    for (int k = pBColIndex; k < pBWidth; k++)
                    {
                        double[] expArr = new double[slopes.Length];
                        double sumExp = 0;
                        int catCnts = 0;
                        bool noDataCheck = true;
                        foreach (double[] IntSlpArr in slopes)
                        {
                            double sumVls = IntSlpArr[0];
                            for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                            {
                                object coefnVlObj = outPb.GetVal(coefnBand, k, i);
                                //Console.WriteLine("Slope X value = " + pixelValue.ToString());
                                if (coefnVlObj==null)
                                {
                                    noDataCheck = false;
                                    break;
                                }
                                double slp = IntSlpArr[coefnBand + 1];
                                //Console.WriteLine("x = " + pixelValue.ToString() + " slope = " + slp.ToString());
                                sumVls += System.Convert.ToDouble(coefnVlObj) * slp;
                            }
                            if (noDataCheck)
                            {
                                double expSum = Math.Exp(sumVls);
                                expArr[catCnts] = expSum;
                                sumExp += expSum;
                                catCnts += 1;
                                //Console.WriteLine("sumVls = " + sumVls.ToString());
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (noDataCheck)
                        {
                            sumExp += 1;
                            double sumProb = 0;
                            catCnts = 1;
                            foreach (double expVl in expArr)
                            {
                                rstPixelType pTy = ipPixelBlock.get_PixelType(catCnts);
                                double prob = expVl / sumExp;
                                //Console.WriteLine("Probability = " + prob.ToString());
                                sumProb += prob;
                                object newVl = rasterUtil.getSafeValue(prob, pTy);
                                cArr[catCnts].SetValue(newVl, k, i);
                                //Console.WriteLine("Probability = " + cArr[catCnts].GetValue(k,i).ToString());
                                catCnts += 1;

                            }
                            rstPixelType pTy2 = ipPixelBlock.get_PixelType(0);
                            double lProb = 1 - sumProb;
                            object newVl2 = rasterUtil.getSafeValue(lProb, pTy2);
                            cArr[0].SetValue(newVl2, k, i);//base category
                        }
                        else
                        {
                            for (int b = 0; b < ipPixelBlock.Planes; b++)
                            {
                                cArr[b].SetValue(0, k, i);
                            }

                        }
                    }
                }
                for(int i=0;i<ipPixelBlock.Planes;i++)
                {
                    ipPixelBlock.set_PixelData(i,cArr[i]);
                }
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of Regression Function. " + exc.Message, exc);
                Console.WriteLine(exc.ToString());
            }
        }
        /// <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.

                //System.Array noDataValueArr = (System.Array)((IRasterProps)inrsBandsCoef).NoDataValue;
                //Console.WriteLine("Before Read");
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                //Console.WriteLine("After Read");
                int pBHeight = pPixelBlock.Height;
                int pBWidth = pPixelBlock.Width;
                IPnt pbSize = new PntClass();
                pbSize.SetCoords(pBWidth, pBHeight);
                IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);//independent variables
                myFunctionHelperCoef.Read(pTlc, null,myFunctionHelperCoef.Raster,(IPixelBlock)outPb);
                int pBRowIndex = 0;
                int pBColIndex = 0;
                IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                System.Array[] pArr = new System.Array[ipPixelBlock.Planes];
                for (int coefnBand = 0; coefnBand < ipPixelBlock.Planes; coefnBand++)
                {
                    System.Array pixelValues = (System.Array)(pPixelBlock.get_SafeArray(coefnBand));
                    pArr[coefnBand] = pixelValues;
                }
                for (int i = pBRowIndex; i < pBHeight; i++)
                {
                    for (int k = pBColIndex; k < pBWidth; k++)
                    {
                        object pObj = outPb.GetVal(0, k, i);

                        if (pObj == null)
                        {
                            continue;
                        }
                        else
                        {
                            string pixelValue = pObj.ToString();
                            double[] c;
                            if (tDic.TryGetValue(pixelValue, out c))
                            {
                                for (int v = 0; v < pArr.Length; v++)
                                {
                                    rstPixelType pty = pPixelBlock.get_PixelType(v);
                                    float vl = System.Convert.ToSingle(c[v]);
                                    object newVl = rasterUtil.getSafeValue(vl, pty);
                                    pArr[v].SetValue(newVl, k, i);
                                }
                            }
                            else
                            {
                                for (int v = 0; v < pArr.Length; v++)
                                {
                                    pArr.SetValue(0, k, i);
                                }
                            }
                        }

                    }
                }
                for (int i = 0; i < pArr.Length; i++)
                {
                    ipPixelBlock.set_PixelData(i, pArr[i]);
                }

            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of TTest Function. " + exc.Message, exc);
                Console.WriteLine(exc.ToString());
            }
        }
        /// <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 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);
                myFunctionHelperCoef.Read(pTlc,null,myFunctionHelperCoef.Raster, (IPixelBlock)outPb);
                int pBRowIndex = 0;
                int pBColIndex = 0;
                IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                System.Array outArr = (System.Array)ipPixelBlock.get_PixelData(0);
                rstPixelType pty = ipPixelBlock.get_PixelType(0);
                double[] vArr = new double[outPb.Planes];
                for (int i = pBRowIndex; i < pBHeight; i++)
                {
                    for (int k = pBColIndex; k < pBWidth; k++)
                    {
                        bool checkVl = true;
                        for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                        {
                            object vlObj = outPb.GetVal(0,k,i);
                            if(vlObj==null)
                            {
                                checkVl = false;
                                break;
                            }
                            vArr[coefnBand] = System.Convert.ToDouble(vlObj);
                        }
                        if(checkVl)
                        {
                            double tVl = lda.computeNew(vArr);
                            object newVl = rasterUtil.getSafeValue(tVl, pty);
                            outArr.SetValue(System.Convert.ToSingle(tVl), k, i);
                        }

                    }
                }
                ipPixelBlock.set_PixelData(0, outArr);
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of GLM Function. " + exc.Message, exc);
                Console.WriteLine(exc.ToString());
            }
        }
        public void executeRegionGroup()
        {
            createOutRaster();
            if (OutRaster == null)
            {
                Console.WriteLine("not all inputs specified");
                return;
            }
            IWorkspaceEdit wksE = (IWorkspaceEdit)vWks;
            bool weEdit = true;
            if (wksE.IsBeingEdited())
            {
                weEdit = false;
            }
            else
            {
                wksE.StartEditing(false);
            }
            wksE.StartEditOperation();
            Console.WriteLine("Counter = " + counter.ToString());
            int readclmsStep = PixelBlockWidth+2;
            int readrwsStep = PixelBlockHeight+2;

            IPnt outloc = new PntClass();
            IPnt inloc = new PntClass();
            try
            {
                for (int rp = 0; rp < rws; rp += PixelBlockHeight)//rws
                {
                    for (int cp = 0; cp < clms; cp += PixelBlockWidth)//clms
                    {
                        Console.WriteLine("Write Raster location = " + cp.ToString() + ":" + rp.ToString());
                        Console.WriteLine("Read Raster location = " + (cp-1).ToString() + ":" + (rp-1).ToString());
                        outloc.SetCoords(cp, rp);
                        inloc.SetCoords(cp-1,rp-1);
                        middleRowColumn(outloc);
                    }
                }
                IRaster2 rs2 = (IRaster2)OutRaster;
                IRasterDataset rsDset = rs2.RasterDataset;
                IRasterDatasetEdit2 rsDsetE = (IRasterDatasetEdit2)rsDset;
                rsDsetE.AlterAttributeTable(vatTable);
                wksE.StopEditOperation();
                if(weEdit) wksE.StopEditing(true);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
            }
            return;
        }
        /// <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
            {
                //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                int pBHeight = pPixelBlock.Height;
                int pBWidth = pPixelBlock.Width;
                IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                IPnt pbBigSize = new PntClass();
                IPnt pbBigLoc = new PntClass();
                int pbBigWd = pBWidth + clms;
                int pbBigHt = pBHeight + rws;
                int l, t;
                l = clms / 2;
                t = rws / 2;
                pbBigSize.SetCoords(pbBigWd, pbBigHt);
                pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t));
                IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize);
                myFunctionHelperOrig.Read(pbBigLoc,null,myFunctionHelperOrig.Raster, (IPixelBlock)pbBig);
                noDataValue = float.MinValue;//System.Convert.ToSingle(noDataValueArr.GetValue(0));
                for (int nBand = 0; nBand < ipPixelBlock.Planes; nBand++)
                {
                    rstPixelType pbt = ipPixelBlock.get_PixelType(nBand);
                    System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand));
                    for (int r = 0; r < pBHeight; r++)
                    {
                        for (int c = 0; c < pBWidth; c++)
                        {
                            object inVlobj = ipPixelBlock.GetVal(nBand, c, r);
                            if (inVlobj==null)
                            {
                                continue;
                            }
                            else
                            {
                                updateGLCMDic(pbBig, c, r, nBand);
                                float outVl = System.Convert.ToSingle(getTransformedValue(countDic));
                                object newVl = rasterUtil.getSafeValue(outVl, pbt);
                                pixelValues.SetValue(newVl,c,r);
                            }
                        }

                    }
                    ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        //Creates lastValueProperties Property Set which stores fields and values for last value method
        //private void constructFieldArray()
        //{
        //    ITable tab = getTable(_defaultsTableName);
        //    IDataset dataset = tab as IDataset;
        //    IQueryFilter qFilter = new QueryFilterClass();
        //    qFilter.WhereClause = "ValueMethod = 'LAST_VALUE'";
        //    lastValueProperties = new PropertySetClass();
        //    if (tab != null)
        //    {
        //        ICursor tabCursor = tab.Search(qFilter, true);
        //        IRow row;
        //        while (((row = tabCursor.NextRow()) != null))
        //        {
        //            object objRow = row.get_Value(dynTargetField);
        //            object nullObject = null;
        //            lastValueProperties.SetProperty(objRow.ToString(), nullObject);
        //        }
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(tabCursor);
        //    }
        //}
        public static void TransformDataXYToPixelXY(IRaster pRaster, ref double dblX, ref double dblY)
        {
            // notes:
            // assume LOWER left origin for projected coordinate system
            // origin is UPPER left for raster pixels

            // Function usage: dblX and dblY are passed in in data coords and passed out in pixel coords

            IRasterProps pRasterProps;
            pRasterProps = (IRasterProps)pRaster;

            // get cell size dimensions
            IPnt pPnt;
            pPnt = new PntClass();
            pPnt = pRasterProps.MeanCellSize();

            double dblCellSizeX;
            double dblCellSizeY;
            dblCellSizeX = pPnt.X;
            dblCellSizeY = pPnt.Y;

            double dblPixelX;
            double dblPixelY;

            IEnvelope pEnv;
            pEnv = new EnvelopeClass();
            pEnv = pRasterProps.Extent;

            double dblDataX;
            dblDataX = dblX;

            double dblDataY;
            dblDataY = dblY;

            // X
            dblPixelX = (dblDataX - pEnv.XMin) / dblCellSizeX;

            // Y (taking care of different origins)
            double dblHeight;
            dblHeight = pRasterProps.Height;
            dblPixelY = dblHeight - ((dblDataY - pEnv.YMin) / dblCellSizeY);

            // make sure I don't get anything larger than height/width or less than 0
            double dblWidth;
            dblWidth = pRasterProps.Width;

            if (dblPixelX > dblWidth)
                dblPixelX = dblWidth;
            else if (dblPixelX < 0)
                dblPixelX = 0;

            if (dblPixelY > dblHeight)
                dblPixelY = dblHeight;
            else if (dblPixelY < 0)
                dblPixelY = 0;

            // return transformed coordinates
            dblX = dblPixelX;
            dblY = dblPixelY;
        }
        /// <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.

                System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
                float noDataVl = System.Convert.ToSingle(noDataValueArr.GetValue(0));
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                noDataValueArr = (System.Array)((IRasterProps)inrsBandsCoef).NoDataValue;//inrsBandsCoef
                int pBHeight = pPixelBlock.Height;
                int pBWidth = pPixelBlock.Width;
                IPnt pbSize = new PntClass();
                pbSize.SetCoords(pBWidth, pBHeight);
                IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);
                myFunctionHelper.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb);
                int pBRowIndex = 0;
                int pBColIndex = 0;
                IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                System.Array[] pArr = new System.Array[outPb.Planes];
                for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                {
                    System.Array pixelValues = (System.Array)(outPb.get_PixelData(coefnBand));
                    pArr[coefnBand] = pixelValues;
                }
                for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++)
                {
                    System.Array outArr = (System.Array)ipPixelBlock.get_PixelData(nBand);
                    rstPixelType pty = ipPixelBlock.get_PixelType(nBand);
                    for (int i = pBRowIndex; i < pBHeight; i++)
                    {
                        for (int k = pBColIndex; k < pBWidth; k++)
                        {

                            float[] IntSlpArr = slopes[nBand];
                            double sumVls = IntSlpArr[0];
                            for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                            {
                                double noDataValue = System.Convert.ToDouble(noDataValueArr.GetValue(coefnBand));
                                double pixelValue = Convert.ToDouble(pArr[coefnBand].GetValue(k, i));
                                if (rasterUtil.isNullData(pixelValue, noDataValue))
                                {
                                    sumVls = noDataVl;
                                    break;
                                }

                                double slp = System.Convert.ToDouble(IntSlpArr[coefnBand + 1]);
                                sumVls += pixelValue * slp;
                            }
                            if (sumVls < censored) sumVls = censored;
                            object newVl = rasterUtil.getSafeValue(sumVls, pty);
                            outArr.SetValue(newVl, k, i);
                        }
                    }
                    ipPixelBlock.set_PixelData(nBand, outArr);
                }
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of Tobit Function. " + exc.Message, exc);
                Console.WriteLine(exc.ToString());
            }
        }
 private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock)
 {
     System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes];
     List<System.Array[]> inPixelValuesArrLst = new List<System.Array[]>();
     List<System.Array> inPixelNoDataArrLst = new List<System.Array>();
     IPnt pntSize = new PntClass();
     pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height);
     ISpatialFilter spFlt = new SpatialFilterClass();
     spFlt.Geometry = (IGeometry)env;
     spFlt.GeometryField = ftrCls.ShapeFieldName;
     spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelOverlaps;
     IFeatureCursor fCur = ftrCls.Search(spFlt, false);
     int fIndex = ftrCls.FindField("catIndex");
     IFeature ftr = fCur.NextFeature();
     for (int i = 0; i < ipPixelBlock.Planes; i++)
     {
         outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i);
     }
     while (ftr != null)
     {
         int rsIndex = System.Convert.ToInt32(ftr.get_Value(fIndex));
         IRaster rs = inrs[rsIndex];
         IPixelBlock inputPb = rs.CreatePixelBlock(pntSize);
         IRaster2 rs2 = (IRaster2)rs;
         int pClm, pRw;
         rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw);
         IPnt tlc = new PntClass();
         tlc.SetCoords(pClm, pRw);
         rs.Read(tlc, inputPb);
         System.Array[] inPixelValuesArr = new System.Array[inputPb.Planes];
         for (int i = 0; i < inputPb.Planes; i++)
         {
             inPixelValuesArr[i] = (System.Array)inputPb.get_SafeArray(i);
         }
         inPixelNoDataArrLst.Add((System.Array)((IRasterProps)rs).NoDataValue);
         inPixelValuesArrLst.Add(inPixelValuesArr);
         ftr = fCur.NextFeature();
     }
     for (int i = 0; i < outPixelValuesArr.Length; i++)
     {
         for (int r = 0; r < ipPixelBlock.Height; r++)
         {
             for (int c = 0; c < ipPixelBlock.Width; c++)
             {
                 double vl = getValue(i, c, r, inPixelValuesArrLst, inPixelNoDataArrLst);
                 outPixelValuesArr[i].SetValue(vl,c,r);
             }
         }
     }
     for (int i = 0; i < outPixelValuesArr.Length; i++)
     {
         ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]);
     }
 }
        /// <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;
            }
        }
        public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock, int clms, int rws, IRasterFunctionHelper orig, rasterUtil.windowType wd)
        {
            try
            {
                if (wd == rasterUtil.windowType.RECTANGLE)
                {
                    try
                    {
                       int pBHeight = pPixelBlock.Height;
                        int pBWidth = pPixelBlock.Width;
                        IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                        IPnt pbBigSize = new PntClass();
                        IPnt pbBigLoc = new PntClass();
                        int l, t;
                        l = clms / 2;
                        t = rws / 2;
                        int pbBigWd = pBWidth + clms;// -1;
                        int pbBigHt = pBHeight + rws;// -1;
                        pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t));
                        pbBigSize.SetCoords(pbBigWd, pbBigHt);
                        IPixelBlock3 pbBig = (IPixelBlock3)orig.Raster.CreatePixelBlock(pbBigSize);
                        orig.Read(pbBigLoc, null,orig.Raster, (IPixelBlock)pbBig);

                        object[,] clmsValues = new object[pBWidth, pBHeight];
                        for (int nBand = 0; nBand < pbBig.Planes; nBand++)
                        {
                            float[,] pixelValues = (float[,])(ipPixelBlock.get_PixelData(nBand));
                            float[,] pixelValuesBig = (float[,])(pbBig.get_PixelData(nBand));
                            for (int r = 0; r < pBHeight; r++)//coordinates in terms of the small pixel block
                            {
                                int er = r + rws;
                                for (int c = 0; c < pBWidth; c++)
                                {
                                    int ec = c + clms;

                                    Dictionary<int, int[]> uDic = findUniqueRegions.getUniqueRegions(pixelValuesBig, ec,er,clms,rws,c,r,(float)rasterUtil.getNoDataValue(rstPixelType.PT_FLOAT)); //key(int) = cell value value(int[2] = number of cells and number of edges)
                                    float uniqueMax = findUniqueRegionsValue(uDic);

                                    pixelValues.SetValue(uniqueMax, c, r);
                                }
                            }
                            try
                            {
                                ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues);
                            }
                            catch
                            {
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        System.Exception myExc = new System.Exception("Exception caught in neighborhood landscape base rectangle helper Function. " + e.Message, e);
                        throw myExc;
                    }
                }
                else
                {

                    try
                    {
                        List<int[]> iter = new List<int[]>();
                        int[,] circleWindow = rasterUtil.createFocalWidow(clms, clms, wd,out iter);
                        System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
                        int pBHeight = pPixelBlock.Height;
                        int pBWidth = pPixelBlock.Width;
                        IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                        IPnt pbBigSize = new PntClass();
                        IPnt pbBigLoc = new PntClass();
                        int pbBigWd = pBWidth + clms;
                        int pbBigHt = pBHeight + rws;
                        int l, t;
                        l = clms / 2;
                        t = rws / 2;
                        pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t));
                        pbBigSize.SetCoords(pbBigWd, pbBigHt);
                        IPixelBlock3 pbBig = (IPixelBlock3)orig.Raster.CreatePixelBlock(pbBigSize);
                        orig.Read(pbBigLoc,null,orig.Raster, (IPixelBlock)pbBig);
                        object[,] clmsValues = new object[pBWidth, pBHeight];
                        for (int nBand = 0; nBand < pbBig.Planes; nBand++)
                        {
                            float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand));
                            float[,] pixelValues = (float[,])(ipPixelBlock.get_PixelData(nBand));
                            float[,] pixelValuesBig = (float[,])(pbBig.get_PixelData(nBand));
                            for (int r = 0; r < pBHeight; r++)
                            {
                                int er = r + rws;
                                for (int c = 0; c < pBWidth; c++)
                                {
                                    int ec = c + clms;

                                    Dictionary<int, int[]> uDic = findUniqueRegions.getUniqueRegions(pixelValuesBig, ec, er,clms,rws,c,r, noDataValue, circleWindow); //key(int) = cell value value(int[2] = number of cells and number of edges)
                                    float uniqueMax = findUniqueRegionsValue(uDic);

                                    try
                                    {
                                        pixelValues.SetValue(uniqueMax, c, r);
                                    }
                                    catch
                                    {
                                        pixelValues.SetValue(noDataValue, c, r);
                                    }
                                }
                            }
                            ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Exception myExc = new System.Exception("Exception caught in neighborhood landscape base circle helper Function. " + e.Message, e);
                        throw myExc;
                    }
                }
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in neighborhood landscape base helper Function. " + exc.Message, exc);
                throw myExc;
            }
        }
        private void buildModelrst()
        {
            n = 0;
            IRaster2 rsV2 = (IRaster2)InValueRaster;
            IRaster2 rsS2 = (IRaster2)InStrataRaster;
            IRasterBandCollection rsbc = (IRasterBandCollection)rsV2;
            IRasterCursor rsSCur = rsS2.CreateCursorEx(null);
            IPixelBlock pbS = null;
            double[] vlBandArr = new double[rsbc.Count];
            Dictionary<string,int> stratDic = new Dictionary<string,int>();
            double[] sumClms = null;
            double[,] sumCross = null;
            double[,] cov = null;
            double[,] scov = null;
            //System.Array[] pbVArrs = new System.Array[rsbc.Count];
            do
            {
                pbS = rsSCur.PixelBlock;
                IPnt pbSize = new PntClass();
                pbSize.SetCoords(pbS.Width,pbS.Height);
                IPixelBlock pb = InValueRaster.CreatePixelBlock(pbSize);
                IPnt ptLoc = new PntClass();
                double mx, my;
                rsS2.PixelToMap(System.Convert.ToInt32(rsSCur.TopLeft.X), System.Convert.ToInt32(rsSCur.TopLeft.Y), out mx, out my);
                int px,py;
                rsV2.MapToPixel(mx, my, out px, out py);
                ptLoc.SetCoords(px, py);
                InValueRaster.Read(ptLoc, pb);
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object sobj = pbS.GetVal(0, c, r);
                        if (sobj == null) continue;
                        bool chVl = true;
                        for (int p = 0; p < pb.Planes; p++)
                        {

                            object vlobj = pb.GetVal(p,c, r);
                            if (vlobj == null)
                            {
                                chVl = false;
                                break;
                            }
                            vlBandArr[p] = System.Convert.ToDouble(vlobj);
                        }
                        if (chVl)
                        {
                            string strataValue = sobj.ToString();
                            int sCnt;
                            if (stratDic.TryGetValue(strataValue, out sCnt))
                            {
                                stratDic[strataValue] = sCnt + 1;
                                int lIndex = lbl.IndexOf(strataValue);
                                sumClms = sumClmsLst[lIndex];
                                sumCross = sumCrossLst[lIndex];
                            }
                            else
                            {
                                stratDic.Add(strataValue, 1);
                                int lIndex = lbl.Count;
                                lbl.Add(strataValue);
                                sumClms = new double[VariableFieldNames.Length];
                                sumCross = new double[VariableFieldNames.Length, VariableFieldNames.Length];
                                cov = new double[VariableFieldNames.Length, VariableFieldNames.Length];
                                scov = new double[VariableFieldNames.Length, VariableFieldNames.Length];
                                sumClmsLst.Add(sumClms);
                                sumCrossLst.Add(sumCross);
                                covLst.Add(cov);
                                scovLst.Add(scov);
                            }
                            for (int v = 0; v < vlBandArr.Length; v++)
                            {
                                sumClms[v] += vlBandArr[v];
                                sumCross[v, v] += Math.Pow(vlBandArr[v], 2);
                                for (int j = 0 + v + 1; j < vlBandArr.Length; j++)
                                {
                                    double vl1 = vlBandArr[v];
                                    double vl2 = vlBandArr[j];
                                    double p12 = vl1 * vl2;
                                    sumCross[v, j] += p12;
                                    sumCross[j, v] += p12;
                                }
                            }
                            n++;
                        }
                    }
                }

            } while (rsSCur.Next() == true);
            for (int l = 0; l < lbl.Count; l++)
            {
                string lblVl = lbl[l];
                int sampN = stratDic[lblVl];
                double r = sampN / (sampN - 1);
                proportionsLst.Add(System.Convert.ToDouble(sampN) / n);
                sumClms = sumClmsLst[l];
                meansLst.Add((from double d in sumClms select d / sampN).ToArray());
                sumCross = sumCrossLst[l];
                cov = covLst[l];
                scov = scovLst[l];
                for (int i = 0; i < sumClms.Length; i++)
                {
                    double var = (sumCross[i, i] / sampN) - (Math.Pow(sumClms[i], 2) / Math.Pow((sampN), 2));
                    cov[i, i] = var;
                    scov[i, i] = var * r;
                    for (int j = 0 + i + 1; j < sumClms.Length; j++)
                    {
                        double vl1 = (sumCross[j, i] / sampN);
                        double vl2 = (sumClms[j] / sampN) * (sumClms[i] / (sampN));
                        double p12 = vl1 - vl2;
                        cov[i, j] = p12;
                        cov[j, i] = p12;
                        scov[i, j] = p12*r;
                        scov[j, i] = p12*r;
                    }
                }
            }

            k = lbl.Count;
            makeKMeans();
        }
 /// <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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         int pBHeight = pPixelBlock.Height;
         int pBWidth = pPixelBlock.Width;
         IPnt pbSize = new PntClass();
         pbSize.SetCoords(pBWidth, pBHeight);
         IPixelBlock3 inputPb = (IPixelBlock3)myFunctionHelperInput.Raster.CreatePixelBlock(pbSize);//independent variables
         myFunctionHelperInput.Read(pTlc, null, myFunctionHelperInput.Raster, (IPixelBlock)inputPb);
         IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
         System.Array pixelValues = (System.Array)ipPixelBlock.get_PixelData(0);
         createRegions(inputPb, pixelValues);
         ipPixelBlock.set_PixelData(0,pixelValues);
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method of the Region Group function Function. " + exc.Message, exc);
         Console.WriteLine(exc.ToString());
     }
 }
        private string getSummaryValue(string iteration,int fire,int Period, IFunctionRasterDataset final10, IFunctionRasterDataset final50, IFunctionRasterDataset arivZones)
        {
            IRasterFunctionHelper arFH = new RasterFunctionHelperClass();
            IRasterFunctionHelper f10FH = new RasterFunctionHelperClass();
            IRasterFunctionHelper f50FH = new RasterFunctionHelperClass();
            arFH.Bind(arivZones);
            f10FH.Bind(final10);
            f50FH.Bind(final50);
            Dictionary<int, double[]> vlDic = new Dictionary<int, double[]>();
            IRasterCursor rsCur = ((IRaster2)arFH.Raster).CreateCursorEx(null);
            IPnt pnt = new PntClass();
            pnt.X = rsCur.PixelBlock.Width;
            pnt.Y = rsCur.PixelBlock.Height;
            IRasterCursor rsCur2 = ((IRaster2)f10FH.Raster).CreateCursorEx(null);
            IRasterCursor rsCur3 = ((IRaster2)f50FH.Raster).CreateCursorEx(null);
            IPixelBlock3 pb = null;
            IPixelBlock3 pb2 = null;
            IPixelBlock3 pb3 = null;
            while (rsCur.Next() && rsCur2.Next() && rsCur3.Next())
            {
                pb = (IPixelBlock3)rsCur.PixelBlock;
                pb2 = (IPixelBlock3)rsCur2.PixelBlock;
                pb3 = (IPixelBlock3)rsCur3.PixelBlock;
                int ht = pb.Height;
                int wd = pb.Width;
                for (int h = 0; h < ht; h++)
                {
                    for (int w = 0; w < wd; w++)
                    {
                        object atobj = pb.GetVal(0, w, h);
                        object f10obj = pb2.GetVal(0, w, h);
                        object f50obj = pb3.GetVal(0, w, h);
                        if (atobj == null || f10obj == null || f50obj == null)
                        {
                            continue;
                        }
                        else
                        {
                            int at = System.Convert.ToInt32(atobj);
                            double f10 = System.Convert.ToDouble(f10obj);
                            double f50 = System.Convert.ToDouble(f50obj);
                            double[] fArr = { 0, 0, 0 };
                            if (vlDic.TryGetValue(at, out fArr))
                            {
                                fArr[0] = fArr[0] + f10;
                                fArr[1] = fArr[1] + f50;
                                fArr[2] = fArr[2] + 1;
                                vlDic[at] = fArr;
                            }
                            else
                            {
                                fArr = new double[] { f10, f50, 1 };
                                vlDic.Add(at, fArr);
                            }
                        }

                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            //need to sort to do accumulative;
            List<int> keySortLst = vlDic.Keys.ToList();
            keySortLst.Sort();
            double ac10 = 0;
            double ac50 = 0;
            foreach (int ky in keySortLst)
            {
                double[] vlArr = vlDic[ky];
                double s10 = vlArr[0];
                double s50 = vlArr[1];
                ac10 += s10;
                ac50 += s50;
                double cellCnt = vlArr[2];
                string newStr = iteration + "," + fire.ToString() + "," + Period.ToString() + "," + s10.ToString() + "," + s50.ToString() + "," + ac10.ToString() + "," + ac50.ToString() + "," + ky.ToString() + "," + cellCnt.ToString();
                //Console.WriteLine(newStr);
                sb.AppendLine(newStr);
            }
            return sb.ToString();
        }
 private void middleRowColumn(IPnt loc)
 {
     double x = loc.X;
     double y = loc.Y;
     string outLocStr = x.ToString() + ":" + y.ToString();
     IPnt readLoc = new PntClass();
     readLoc.SetCoords(x - 1, y - 1);
     IRasterEdit regRsE = (IRasterEdit)OutRaster;
     IPnt writePbSize = new PntClass();
     writePbSize.SetCoords(pbwidth, pbheight);
     IPnt readPbSize = new PntClass();
     readPbSize.SetCoords(pbwidth + 2, pbheight + 2);
     IPixelBlock pb = inRs.CreatePixelBlock(readPbSize);
     IPixelBlock pb2 = OutRaster.CreatePixelBlock(writePbSize);
     System.Array[] inOutArr;
     System.Array inArr = null;
     System.Array outArr = null;
     if (outArrDic.TryGetValue(outLocStr, out inOutArr))
     {
         inArr = inOutArr[0];
         outArr = inOutArr[1];
     }
     else
     {
         OutRaster.Read(loc, pb2);
         inRs.Read(readLoc, pb);
         OutRaster.Read(loc, pb2);
         inArr = (System.Array)pb.get_SafeArray(0);
         outArr = (System.Array)pb2.get_SafeArray(0);
         outArrDic[outLocStr] = new System.Array[] { inArr, outArr };
     }
     int height = pb2.Height;
     int width = pb2.Width;
     for (int c = 0; c < width; c++)
     {
         int ic = c + 1;
         for (int r = 0; r < height; r++)
         {
             List<string> cr = new List<string>();
             cr.Add(c.ToString()+":"+r.ToString());
             int ir = r + 1;
             int inVl = System.Convert.ToInt32(inArr.GetValue(ic, ir));
             //Console.WriteLine("Invalue = " + inVl.ToString());
             if ((inVl == noDataVl) || (inVl == (noDataVl - 1)))
             {
                 Console.WriteLine("Invalue = " + inVl.ToString());
                 continue;
             }
             else
             {
                 int outVl32 = System.Convert.ToInt32(outArr.GetValue(c, r));
                 if (outVl32 == noDataVl2)
                 {
                     rCnt = 0;
                     rPerm = 0;
                     outArr.SetValue(counter, c, r);
                     List<int>[] nextArray = { new List<int>(), new List<int>(), new List<int>(), new List<int>() };//determines if the next pixel block must be queried {left,top,right,bottom}
                     while (cr.Count > 0)
                     {
                         rCnt++;
                         rPerm += findRegion(inVl, counter, noDataVl2, inArr, outArr, cr, nextArray);
                     }
                     for (int i = 0; i < nextArray.Length; i++)
                     {
                         List<int> pbNextLst = nextArray[i];
                         if (pbNextLst.Count > 0)
                         {
                             int[] startClms = new int[pbNextLst.Count];
                             int[] startRws = new int[pbNextLst.Count];
                             IPnt newLoc = new PntClass();
                             double nClP = loc.X;
                             double nRwP = loc.Y;
                             switch (i)
                             {
                                 case 0:
                                     nClP = nClP - pbwidth;
                                     startRws = pbNextLst.ToArray();
                                     int stcl = pbwidth - 1;
                                     for (int k = 0; k < startRws.Length; k++)
                                     {
                                         startClms[k] = stcl;
                                     }
                                     break;
                                 case 1:
                                     nRwP = nRwP - pbheight;
                                     startClms = pbNextLst.ToArray();//rws=pbHeight-1
                                     int strw = pbheight - 1;
                                     for (int k = 0; k < startClms.Length; k++)
                                     {
                                         startRws[k] = strw;
                                     }
                                     break;
                                 case 2:
                                     nClP = nClP + pbwidth;
                                     startRws = pbNextLst.ToArray();//clms=0;
                                     break;
                                 default:
                                     nRwP = nRwP + pbheight;
                                     startClms = pbNextLst.ToArray();//rws = 0;
                                     break;
                             }
                             if ((nClP >= 0 && nRwP >= 0 & nClP <= rsProps2.Width && nRwP <= rsProps2.Height))
                             {
                                 newLoc.SetCoords(nClP, nRwP);
                                 middleRowColumn(newLoc, startRws, startClms);
                             }
                         }
                     }
                     IRow row = vatTable.CreateRow();
                     row.set_Value(valueIndex, counter);
                     row.set_Value(countIndex, rCnt);
                     row.set_Value(permIndex, rPerm);
                     row.Store();
                     counter++;
                 }
                 else
                 {
                 }
             }
         }
     }
     pb2.set_SafeArray(0, (System.Array)outArr);
     regRsE.Write(loc, pb2);
     outArrDic.Remove(outLocStr);
 }
 /// <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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         IPnt pntSize = new PntClass();
         pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height);
         IPixelBlock vPb = myFunctionHelper2.Raster.CreatePixelBlock(pntSize);
         myFunctionHelper2.Read(pTlc, null, myFunctionHelper2.Raster, vPb);
         IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock;
         object intArr = calcMeanShift((IPixelBlock3)vPb,pb3);
         pb3.set_PixelData(0, intArr);
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method Mean-shift Function. " + exc.Message, exc);
         throw myExc;
     }
 }
 /// <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
     {
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         IPnt pntSize = new PntClass();
         pntSize.SetCoords(pPixelBlock.Width,pPixelBlock.Height);
         IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock;
         for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++)
         {
             //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand));
             object dArr = vPb.get_PixelData(nBand);
             pb3.set_PixelData(nBand,dArr);
         }
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method PixelBlock To Raster Function. " + exc.Message, exc);
         throw myExc;
     }
 }
        /// <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
            {
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                IPixelBlock3 pPixelBlock3 = (IPixelBlock3)pPixelBlock;
                IRaster2 inRs2 = (IRaster2)inrs;
                IRaster2 outRs2 = (IRaster2)outrs;
                double mx,my;
                outRs2.PixelToMap((int)pTlc.X,(int)pTlc.Y,out mx, out my);
                int clm,rw;
                inRs2.MapToPixel(mx,my,out clm,out rw);
                IPnt pntLoc = new PntClass();
                pntLoc.SetCoords(clm,rw);
                IPnt pntSize = new PntClass();
                pntSize.SetCoords(pPixelBlock.Width,pPixelBlock.Height);
                IPixelBlock3 pBIn = (IPixelBlock3)inrs.CreatePixelBlock(pntSize);
                inrs.Read(pntLoc,(IPixelBlock)pBIn);
                for (int p = 0; p < pPixelBlock.Planes; p++)
                {
                    pPixelBlock3.set_PixelData(p, pBIn.get_PixelData(p));
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        /// <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.

                //System.Array noDataValueArr = (System.Array)((IRasterProps)inrsBandsCoef).NoDataValue;
                //Console.WriteLine("Before Read");
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                //Console.WriteLine("After Read");
                int pBHeight = pPixelBlock.Height;
                int pBWidth = pPixelBlock.Width;
                IPnt pbSize = new PntClass();
                pbSize.SetCoords(pBWidth, pBHeight);
                IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize);//independent variables
                myFunctionHelperCoef.Read(pTlc, null,myFunctionHelperCoef.Raster,(IPixelBlock)outPb);
                int pBRowIndex = 0;
                int pBColIndex = 0;
                IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock;
                //System.Array[] pArr = new System.Array[outPb.Planes];
                //for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                //{
                //    System.Array pixelValues = (System.Array)(outPb.get_PixelData(coefnBand));
                //    pArr[coefnBand] = pixelValues;
                //}
                System.Array pValues = (System.Array)ipPixelBlock.get_PixelData(0);//(System.Array)(td);
                rstPixelType pTyp = ipPixelBlock.get_PixelType(0);
                for (int i = pBRowIndex; i < pBHeight; i++)
                {
                    for (int k = pBColIndex; k < pBWidth; k++)
                    {
                        double[] xVls = new double[outPb.Planes];
                        bool ndT = true;
                        for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++)
                        {
                            //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(coefnBand));
                            object pObj = outPb.GetVal(coefnBand, k, i);

                            if (pObj == null)
                            {
                                ndT = false;
                                break;
                            }
                            double pixelValue = Convert.ToDouble(pObj);
                            xVls[coefnBand] = pixelValue;
                        }
                        if (ndT)
                        {
                            int c = cluster.computNew(xVls);
                            object newVl = rasterUtil.getSafeValue(c, pTyp);
                            pValues.SetValue(newVl, k, i);
                        }

                    }
                }
                ipPixelBlock.set_PixelData(0, pValues);

            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of Cluster Function. " + exc.Message, exc);
                Console.WriteLine(exc.ToString());
            }
        }
 private void buildModelRst()
 {
     sumX.Clear();
     sumX2.Clear();
     cateDic.Clear();
     lbl.Clear();
     int v1 = VariableFieldNames.Length;
     double[] s = new double[(v1 * v1 - v1) / 2];
     double[] s2 = new double[s.Length];
     n=0;
     IRasterCursor rsCur = ((IRaster2)InStrataRaster).CreateCursorEx(null);
     IPnt vCurSize = new PntClass();
     do
     {
         IPixelBlock pbS = rsCur.PixelBlock;
         vCurSize.SetCoords(pbS.Width, pbS.Height);
         IPixelBlock pbV = InValueRaster.CreatePixelBlock(vCurSize);
         InValueRaster.Read(rsCur.TopLeft, pbV);
         for (int r = 0; r < pbS.Height; r++)
         {
             for (int c = 0; c < pbS.Width; c++)
             {
                 object strata = pbS.GetVal(0, c, r);
                 if (strata == null)
                 {
                     continue;
                 }
                 else
                 {
                     string strataStr = strata.ToString();
                     int strataIndex;
                     int cnt;
                     if (cateDic.TryGetValue(strataStr, out cnt))
                     {
                         cnt=cnt+1;
                         strataIndex = lbl.IndexOf(strataStr);
                         s = sumX[strataIndex];
                         s2 = sumX2[strataIndex];
                     }
                     else
                     {
                         cnt=1;
                         cateDic.Add(strataStr, 0);
                         lbl.Add(strataStr);
                         strataIndex = lbl.Count - 1;
                         s = new double[s.Length];
                         s2 = new double[s.Length];
                         sumX.Add(s);
                         sumX2.Add(s2);
                     }
                     bool checkVl = true;
                     double[] vlArr = new double[pbV.Planes];
                     for (int p = 0; p < pbV.Planes; p++)
                     {
                         object vl = pbV.GetVal(p,c,r);
                         if (vl == null)
                         {
                             checkVl = false;
                             break;
                         }
                         else
                         {
                             vlArr[p] = System.Convert.ToDouble(vl);
                         }
                     }
                     if (checkVl)
                     {
                         int vlCnter = 1;
                         int sCnter = 0;
                         for (int i = 0; i < vlArr.Length-1; i++)
                         {
                             for (int k = vlCnter; k < vlArr.Length; k++)
                             {
                                 double m = vlArr[i] - vlArr[k];
                                 double m2 = m * m;
                                 s[sCnter] = s[sCnter] + m;
                                 s2[sCnter] = s2[sCnter]+ m2;
                                 sCnter += 1;
                             }
                             vlCnter += 1;
                         }
                         cateDic[strataStr] = cnt;
                         n+=1;
                     }
                 }
             }
         }
     } while (rsCur.Next() == true);
 }
예제 #55
0
파일: Raster.cs 프로젝트: nazzal88/ares
        /// <summary>
        /// Write edits to the input raster.
        /// </summary>
        /// <param name="raster">Raster of raster layer to be edited.</param>
        /// <param name="edits">Pixel collection that contains edited pixels.</param>
        private static void WriteEdits(IRaster raster, PixelCollection edits)
        {
            IRasterProps rasterProps = (IRasterProps)raster;

            int minRow = rasterProps.Height - 1;
            int maxRow = 0;
            int minCol = rasterProps.Width - 1;
            int maxCol = 0;

            for (int i = 0; i < edits.Count; i++)
            {
                #region Get the extent of the edition region

                Position cellPos = edits[i].Position;

                if (cellPos.Row > maxRow)
                {
                    maxRow = cellPos.Row;
                }

                if (cellPos.Row < minRow)
                {
                    minRow = cellPos.Row;
                }

                if (cellPos.Column > maxCol)
                {
                    maxCol = cellPos.Column;
                }

                if (cellPos.Column < minCol)
                {
                    minCol = cellPos.Column;
                }

                #endregion
            }

            IPnt pos = new PntClass();
            pos.SetCoords(maxCol - minCol + 1, maxRow - minRow + 1);
            IPixelBlock pixelBlock = raster.CreatePixelBlock(pos);
            pos.SetCoords(minCol, minRow);
            raster.Read(pos, pixelBlock);

            // Set new values
            IPixelBlock3 pixelBlock3 = (IPixelBlock3)pixelBlock;
            Array pixels = (Array)pixelBlock3.get_PixelData(0);

            for (int i = 0; i < edits.Count; i++)
            {
                object value = null;
                Raster.CSharpValue2PixelValue(edits[i].NewValue, rasterProps.PixelType, out value);

                pixels.SetValue(value,
                                edits[i].Position.Column - minCol,
                                edits[i].Position.Row - minRow);
            }

            pixelBlock3.set_PixelData(0, (System.Object)pixels);
            IRasterEdit rasterEdit = (IRasterEdit)raster;
            rasterEdit.Write(pos, (IPixelBlock)pixelBlock3);
        }
예제 #56
0
 /// <summary>
 /// 读取数据
 /// </summary>
 /// <param name="xIndex">水平方向的序号</param>
 /// <param name="yIndex">垂直方向的序号</param>
 /// <returns>获取的值,如果为null,表明该位置没有数据</returns>
 public object Read(int xIndex, int yIndex)
 {
     IRaster pRaster = GetRaster();
     IPnt pnt = new PntClass();
     pnt.SetCoords(xIndex, yIndex);
     IPnt pntSize=new PntClass();
     pntSize.SetCoords(1,1);
     IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
     pRaster.Read(pnt,pixelBlock);          
     object obj = pixelBlock.GetVal(0, 0, 0);
     return obj;
 }
        private void calcZoneValues()
        {
            bool makeDic = (ZoneClassCount||ZoneTypes.Contains(rasterUtil.zoneType.VARIETY)||ZoneTypes.Contains(rasterUtil.zoneType.ENTROPY)||ZoneTypes.Contains(rasterUtil.zoneType.ASM)||ZoneTypes.Contains(rasterUtil.zoneType.MINORITY)||ZoneTypes.Contains(rasterUtil.zoneType.MODE)||ZoneTypes.Contains(rasterUtil.zoneType.MEDIAN));
            IPnt zMeanCellSize = zRs.RasterInfo.CellSize;
            IPnt vMeanCellSize = vRs.RasterInfo.CellSize;
            int intersectWidthCells = System.Convert.ToInt32(intEnv.Width / zMeanCellSize.X);
            int intersectHeightCells = System.Convert.ToInt32(intEnv.Height / zMeanCellSize.Y);
            IPoint tl = intEnv.UpperLeft;
            int bH = 512;
            int bW = 512;
            int wCellsMax = intersectWidthCells;
            int hCellsMax = intersectHeightCells;
            IPnt zPntLoc = new PntClass();
            IPnt vPntLoc = new PntClass();
            IPnt zPntSize = new PntClass();
            IRasterFunctionHelper inZoneHelper = new RasterFunctionHelperClass();
            inZoneHelper.Bind(InZoneRaster);
            IRasterFunctionHelper inValueHelper = new RasterFunctionHelperClass();
            inValueHelper.Bind(InValueRaster);
            IRaster2 zr = (IRaster2)inZoneHelper.Raster;
            IRaster2 vr = (IRaster2)inValueHelper.Raster;
            int zclm, zrw, vclm, vrw;
            zr.MapToPixel(tl.X+(zMeanCellSize.X/2), tl.Y-(zMeanCellSize.Y/2), out zclm, out zrw);
            vr.MapToPixel(tl.X + (zMeanCellSize.X / 2), tl.Y - (zMeanCellSize.Y / 2), out vclm, out vrw);
            int ozclm = zclm;
            int ozrw = zrw;
            int ovclm = vclm;
            int ovrw = vrw;
            zPntLoc.SetCoords(zclm, zrw);
            vPntLoc.SetCoords(vclm, vrw);
            for (int brw = 0; brw < hCellsMax; brw += bH)
            {
                int rH = hCellsMax-brw;//Height of block
                if (rH > bH) rH = bH;
                for (int bclm = 0; bclm < wCellsMax; bclm += bW)
                {
                    int cW = wCellsMax - bclm;//Width of block
                    if (cW > bW) cW = bW;
                    zPntSize.SetCoords(cW, rH);
                    IPixelBlock zPb = ((IRaster)zr).CreatePixelBlock(zPntSize);
                    IPixelBlock vPb = ((IRaster)vr).CreatePixelBlock(zPntSize);
                    inZoneHelper.Read(zPntLoc, null, (IRaster)zr, zPb);
                    inValueHelper.Read(vPntLoc, null, (IRaster)vr, vPb);
                    for (int i = 0; i < vPb.Planes; i++)
                    {
                        zoneValueDic = zoneValueDicArr[i];
                        //double vNoDataVl = System.Convert.ToDouble(((System.Array)vProps.NoDataValue).GetValue(i));
                        //System.Array vPix = (System.Array)vPb.get_SafeArray(i);
                        for (int r = 0; r < rH; r++)
                        {
                            for (int c = 0; c < cW; c++)
                            {
                                object zObj = zPb.GetVal(0, c, r);
                                if (zObj==null)
                                {
                                    continue;
                                }
                                else
                                {
                                    double z = System.Convert.ToDouble(zObj);
                                    object vObj = vPb.GetVal(i, c, r);

                                    if (vObj==null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        double v = System.Convert.ToDouble(vObj);
                                        object[] zoneValue;
                                        if (zoneValueDic.TryGetValue(z, out zoneValue))
                                        {
                                            double cnt = System.Convert.ToDouble(zoneValue[0]);
                                            zoneValue[0] = cnt += 1;
                                            double maxVl = System.Convert.ToDouble(zoneValue[1]);
                                            if (v > maxVl)
                                            {
                                                maxVl = v;
                                                zoneValue[1] = maxVl;
                                            }
                                            double minVl = System.Convert.ToDouble(zoneValue[2]);
                                            if (v < minVl)
                                            {
                                                minVl = v;
                                                zoneValue[2] = minVl;
                                            }
                                            double s = System.Convert.ToDouble(zoneValue[3]);
                                            zoneValue[3] = s + v;
                                            double s2 = System.Convert.ToDouble(zoneValue[4]);
                                            zoneValue[4] = s2 + v * v;
                                            if (makeDic)
                                            {
                                                Dictionary<double, int> uDic = (Dictionary<double, int>)zoneValue[5];
                                                int cntVl = 0;
                                                if (uDic.TryGetValue(v, out cntVl))
                                                {
                                                    uDic[v] = cntVl += 1;
                                                }
                                                else
                                                {
                                                    uDic.Add(v, 1);
                                                }
                                                zoneValue[5] = uDic;
                                            }
                                            zoneValueDic[z] = zoneValue;
                                        }
                                        else
                                        {
                                            zoneValue = new object[6];
                                            zoneValue[0] = 1d;
                                            zoneValue[1] = v;
                                            zoneValue[2] = v;
                                            zoneValue[3] = v;
                                            zoneValue[4] = v * v;
                                            if (makeDic)
                                            {
                                                Dictionary<double, int> uDic = new Dictionary<double, int>();
                                                uDic.Add(v, 1);
                                                zoneValue[5] = uDic;
                                            }
                                            zoneValueDic.Add(z, zoneValue);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //do this at the end
                    zclm = zclm + cW;
                    vclm = vclm + cW;
                    zPntLoc.SetCoords(zclm, zrw);
                    vPntLoc.SetCoords(vclm, vrw);
                }
                zrw = zrw + rH;
                vrw = vrw + rH;
                //reset PixelBlock columns
                zclm = ozclm;
                vclm = ovclm;
                zPntLoc.SetCoords(zclm, zrw);
                vPntLoc.SetCoords(vclm, vrw);
            }
        }
예제 #58
0
파일: Raster.cs 프로젝트: nazzal88/ares
        /// <summary>
        /// Get the pixel values in a region of the input raster.
        /// </summary>
        /// <param name="tlCorner"></param>
        /// <param name="brCorner"></param>
        /// <param name="raster"></param>
        /// <returns></returns>
        public static double[,] GetValues(Position tlCorner, Position brCorner, IRaster raster)
        {
            int colCount = brCorner.Column - tlCorner.Column + 1;
            int rowCount = brCorner.Row - tlCorner.Row + 1;

            IPnt regionSize = new PntClass();
            regionSize.SetCoords(colCount, rowCount);
            IPixelBlock pixelBlock = raster.CreatePixelBlock(regionSize);
            IPnt tl = new PntClass();
            tl.SetCoords(tlCorner.Column, tlCorner.Row);
            raster.Read(tl, pixelBlock);

            double[,] values = new double[colCount, rowCount];
            for (int x = 0; x < colCount; x++)
            {
                for (int y = 0; y < rowCount; y++)
                {
                    values[x, y] = Convert.ToDouble(pixelBlock.GetVal(0, x, y));
                }
            }
            return values;
        }
        public static void OpenFileRasterDataset(string inFolderName, string inRasterDatasetName, string inFeatureName, string inFieldName, double outCellSize, string outSummaryFile)
        {
            EnableEsriLiscences();

            //Get feature raster from feature shp
            string outTempRasterName = "tempZoneRasterFromESRI.tif";
            string outZoneRater = inFolderName + "\\" + outTempRasterName;
            int rasterBlockSize = 1024;
            RasterizeEsri.Rasterize(inFeatureName, outZoneRater, inFieldName, outCellSize);

            //Open raster file workspace
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(inFolderName, 0);

            //Align raster
            string inValueRaster = inFolderName + "\\" + inRasterDatasetName;
            string inClipFeature = inFeatureName;
            string outClippedRasterName = "tempValueRasterFromESRI.tif";
            string outClippedValueRaster = inFolderName + "\\" + outClippedRasterName;

            ClipRasterBoundaryEsri.ClipRaster(inValueRaster, inClipFeature, outClippedValueRaster);

            //Open zone raster dataset
            IRasterDataset zoneRasterDataset = rasterWorkspace.OpenRasterDataset(outTempRasterName);
            IRasterDataset2 zoneRasterDataset2 = zoneRasterDataset as IRasterDataset2;
            IRaster2 zoneRs2 = zoneRasterDataset2.CreateFullRaster() as IRaster2;

            //Open value raster dataset
            IRasterDataset valueRasterDataset = rasterWorkspace.OpenRasterDataset(outClippedRasterName);
            IRasterDataset2 valueRasterDataset2 = valueRasterDataset as IRasterDataset2;
            IRaster2 valueRs2 = valueRasterDataset2.CreateFullRaster() as IRaster2;

            //Extract bands from the raster
            IRasterBandCollection valueRasterPlanes = valueRasterDataset as IRasterBandCollection;

            //create raster cursor to read block by block
            IPnt blockSize = new PntClass();
            blockSize.SetCoords(rasterBlockSize, rasterBlockSize);

            IRasterCursor valueRasterCursor = valueRs2.CreateCursorEx(blockSize);
            IRasterCursor zoneRasterCursor = zoneRs2.CreateCursorEx(blockSize);

            if (valueRasterPlanes != null)
            {
                Dictionary<int, StatisticsInfo>[] rasInfoDict = new Dictionary<int, StatisticsInfo>[valueRasterPlanes.Count];
                int zoneRasterBandId = 0;

                for (int b = 0; b < valueRasterPlanes.Count; b++)
                {
                    rasInfoDict[b] = new Dictionary<int, StatisticsInfo>();
                }

                do
                {
                    IPixelBlock3 valueRasterPixelBlock3 = valueRasterCursor.PixelBlock as IPixelBlock3;
                    IPixelBlock3 zoneRasterPixelBlock3 = zoneRasterCursor.PixelBlock as IPixelBlock3;

                    //No Idea how esri cursor fills the raster gap if zone is greater than value, so quick and fix using smallest extent

                    int blockWidth = valueRasterPixelBlock3.Width < zoneRasterPixelBlock3.Width ? valueRasterPixelBlock3.Width : zoneRasterPixelBlock3.Width;
                    int blockHeight = valueRasterPixelBlock3.Height < zoneRasterPixelBlock3.Height ? valueRasterPixelBlock3.Height : zoneRasterPixelBlock3.Height;

                    //Console.WriteLine(zoneRasterPixelBlock3.Width);
                    //Console.WriteLine(blockWidth);

                    try
                    {
                        System.Array zoneRasterPixels = (System.Array)zoneRasterPixelBlock3.get_PixelData(zoneRasterBandId);
                        for (int b = 0; b < valueRasterPlanes.Count; b++)
                        {
                            //Console.WriteLine(b);
                            //Get pixel array
                            System.Array valueRasterPixels = (System.Array)valueRasterPixelBlock3.get_PixelData(b);

                            for (int i = 0; i < blockWidth; i++)
                            {
                                for (int j = 0; j < blockHeight; j++)
                                {
                                    //Get pixel value
                                    object pixelValueFromValue = null;
                                    object pixelValueFromZone = null;
                                    try
                                    {
                                        pixelValueFromValue = valueRasterPixels.GetValue(i, j);
                                        pixelValueFromZone = zoneRasterPixels.GetValue(i, j);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex.Message);
                                    }

                                    //process each pixel value
                                    try
                                    {
                                        if (rasInfoDict[b].ContainsKey(Convert.ToInt32(pixelValueFromZone)))
                                        {
                                            StatisticsInfo rastStatistics = rasInfoDict[b][Convert.ToInt32(pixelValueFromZone)];
                                            rastStatistics.Count++;
                                            rastStatistics.Sum = rastStatistics.Sum + Convert.ToDouble(pixelValueFromValue);

                                            rasInfoDict[b][Convert.ToInt32(pixelValueFromZone)] = rastStatistics;
                                        }
                                        else
                                        {
                                            rasInfoDict[b][Convert.ToInt32(pixelValueFromZone)] = new StatisticsInfo() { Count = 1, Sum = Convert.ToDouble(pixelValueFromValue) };
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex.Message);
                                    }

                                    //Console.WriteLine(i +"-"+j);
                                    //Console.WriteLine(pixelValueFromValue + "-" + pixelValueFromZone);

                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                } while (zoneRasterCursor.Next() == true);

                //Export results
                StatisticsExport writer = new StatisticsExport(outSummaryFile);
                writer.ExportZonalStatistics(ref rasInfoDict, outCellSize);

            }
            else
            {
                Console.WriteLine("No band available in the Value Raster");
            }
        }
        // From ArcObjects Help: How to Create a Raster dataset
        // http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#/How_to_create_a_raster_dataset/000100000464000000/
        public static IRasterDataset CreateRasterDataset(string Path, string FileName)
        {
            try
            {
                IRasterWorkspace2 rasterWs = OpenRasterWorkspace(Path); // This is a custom method that's at the bottom of this code.
                //Define the spatial reference of the raster dataset.
                ISpatialReference sr = new UnknownCoordinateSystemClass();
                //Define the origin for the raster dataset, which is the lower left corner of the raster.
                IPoint origin = new PointClass();
                origin.PutCoords(15.0, 15.0);
                //Define the dimensions of the raster dataset.
                int width = 100; //This is the width of the raster dataset.
                int height = 100; //This is the height of the raster dataset.
                double xCell = 30; //This is the cell size in x direction.
                double yCell = 30; //This is the cell size in y direction.
                int NumBand = 1; // This is the number of bands the raster dataset contains.
                //Create a raster dataset in TIFF format.
                IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(FileName, "TIFF",
                    origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
                    true);

                //If you need to set NoData for some of the pixels, you need to set it on band 
                //to get the raster band.
                IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
                IRasterBand rasterBand;
                IRasterProps rasterProps;
                rasterBand = rasterBands.Item(0);
                rasterProps = (IRasterProps)rasterBand;
                //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
                rasterProps.NoDataValue = 255;
                //Create a raster from the dataset.
                IRaster raster = ((IRasterDataset2)rasterDataset).CreateFullRaster();

                //Create a pixel block using the weight and height of the raster dataset. 
                //If the raster dataset is large, a smaller pixel block should be used. 
                //Refer to the topic "How to access pixel data using a raster cursor".
                IPnt blocksize = new PntClass();
                blocksize.SetCoords(width, height);
                IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3;

                //Populate some pixel values to the pixel block.
                System.Array pixels;
                pixels = (System.Array)pixelblock.get_PixelData(0);
                for (int i = 0; i < width; i++)
                    for (int j = 0; j < height; j++)
                        if (i == j)
                            pixels.SetValue(Convert.ToByte(255), i, j);
                        else
                            pixels.SetValue(Convert.ToByte((i * j) / 255), i, j);

                pixelblock.set_PixelData(0, (System.Array)pixels);

                //Define the location that the upper left corner of the pixel block is to write.
                IPnt upperLeft = new PntClass();
                upperLeft.SetCoords(0, 0);

                //Write the pixel block.
                IRasterEdit rasterEdit = (IRasterEdit)raster;
                rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);

                //Release rasterEdit explicitly.
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);

                return rasterDataset;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return null;
            }
        }