Esempio n. 1
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);
        }
Esempio n. 2
0
 /// <summary>
 /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels.
 /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling.
 /// The log raster is the natural log of the raster.
 /// </summary>
 /// <param name="pTlc">Point to start the reading from in the Raster</param>
 /// <param name="pRaster">Reference Raster for the PixelBlock</param>
 /// <param name="pPixelBlock">PixelBlock to be filled in</param>
 public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock)
 {
     try
     {
         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());
     }
 }
Esempio n. 3
0
        /// <summary>
        ///  this method writes values to the current block of pixels of the raster
        /// </summary>
        /// <param name="pixelblock3">current block of pixels</param>
        protected override void DoOperationsWithPixels(IPixelBlock3 pixelblock3)
        {
            //Get the pixel array.
            pixels      = (System.Array)pixelblock3.get_PixelData(0);
            blockwidth  = pixelblock3.Width;
            blockheight = pixelblock3.Height;
            pixelblock3.Mask(255);
            object k = null;

            //iterate through each line in an image
            for (int i = 0; i < blockwidth; i++)
            {
                //iterate through each pixel in the line
                for (int j = 0; j < blockheight; j++)
                {
                    k = Convert.ToSingle(blocksWithValues[numOfBlock, i + j * 64]);
                    pixels.SetValue(k, i, j);
                }
            }

            //Set the pixel array to the pixel block.
            pixelblock3.set_PixelData(0, pixels);

            //Write back to the raster.
            tlc = rasterCursor.TopLeft;
            rasterEdit.Write(tlc, (IPixelBlock)pixelblock3);
        }
Esempio n. 4
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());
     }
 }
Esempio n. 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.

                //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());
            }
        }
Esempio n. 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)
        {
            double vl         = 0;
            float  pixelValue = 0f;

            try
            {
                System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
                // Call Read method of the Raster Function Helper object.
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock;
                #region Load log object
                for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++)
                {
                    float        noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand));
                    System.Array dArr        = (System.Array)pb3.get_PixelData(nBand);
                    for (int r = 0; r < pPixelBlock.Height; r++)
                    {
                        for (int c = 0; c < pPixelBlock.Width; c++)
                        {
                            vl = System.Convert.ToDouble(dArr.GetValue(c, r));
                            if (rasterUtil.isNullData(System.Convert.ToSingle(vl), noDataValue))
                            {
                                continue;
                            }
                            pixelValue = System.Convert.ToSingle(getFunctionValue(vl));
                            dArr.SetValue(pixelValue, c, r);
                        }
                    }
                    pb3.set_PixelData(nBand, dArr);
                    //unsafe
                    //{
                    //    System.Array dArr = (System.Array)pb3.get_PixelData(nBand);
                    //    int lng = dArr.Length;
                    //    fixed (float* dValue = (float[,])dArr)
                    //    {
                    //        for (int i = 0; i < lng; i++)
                    //        {
                    //            pixelValue = *(dValue + i);
                    //            if (rasterUtil.isNullData(pixelValue, noDataValue))
                    //            {
                    //                continue;
                    //            }
                    //            pixelValue = System.Convert.ToSingle(getFunctionValue(pixelValue));
                    //            *(dValue + i) = pixelValue;

                    //        }
                    //        pb3.set_PixelData(nBand, dArr);
                    //    }
                    //}
                }
                #endregion
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method math Function. " + exc.Message, exc);
                throw myExc;
            }
        }
        public static void exportRasterData(string parth, IRasterLayer rasterLayer, float[,] rasterMat)   //输出栅格数据
        {
            string            directory        = parth.Substring(0, parth.LastIndexOf("\\"));
            string            name             = parth.Substring(parth.LastIndexOf("\\") + 1);
            IWorkspaceFactory workspaceFac     = new RasterWorkspaceFactoryClass();
            IRasterWorkspace2 rasterWorkspace2 = workspaceFac.OpenFromFile(directory, 0) as IRasterWorkspace2;

            IRasterInfo rasterInfo  = (rasterLayer.Raster as IRawBlocks).RasterInfo;
            IPoint      originPoint = new Point();

            originPoint.PutCoords(rasterInfo.Origin.X, rasterInfo.Origin.Y - (rasterLayer.Raster as IRasterProps).Height * (rasterLayer.Raster as IRasterProps).MeanCellSize().Y);
            IRasterProps   rasterProps   = rasterLayer.Raster as IRasterProps;
            IRasterDataset rasterDataSet = rasterWorkspace2.CreateRasterDataset(name, "IMAGINE Image", originPoint, rasterProps.Width, rasterProps.Height,
                                                                                rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, 1, rstPixelType.PT_FLOAT, rasterProps.SpatialReference, true) as IRasterDataset2;

            IRaster2 raster2 = rasterDataSet.CreateDefaultRaster() as IRaster2;

            IPnt pntClass = new Pnt();

            pntClass.X = rasterProps.Width;
            pntClass.Y = rasterProps.Height;
            IRasterCursor rasterCursor   = raster2.CreateCursorEx(pntClass);
            IRasterCursor inRasterCursor = (rasterLayer.Raster as IRaster2).CreateCursorEx(pntClass);

            IRasterEdit rasterEdit = raster2 as IRasterEdit;

            if (rasterEdit.CanEdit())
            {
                IPixelBlock3 pixelBlock3   = rasterCursor.PixelBlock as IPixelBlock3;
                IPixelBlock3 inPixelBlock3 = inRasterCursor.PixelBlock as IPixelBlock3;
                System.Array pixels        = (System.Array)rasterMat;
                pixelBlock3.set_PixelData(0, (System.Array)pixels);
                rasterEdit.Write(rasterCursor.TopLeft, (IPixelBlock)pixelBlock3);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pixelBlock3);
            }
            rasterEdit.Refresh();
            IGeoDataset   inDataset   = rasterLayer.Raster as IGeoDataset;
            IGeoDataset   outDataset  = rasterDataSet as IGeoDataset;
            IExtractionOp op          = new RasterExtractionOpClass();
            var           outDataset1 = op.Raster(outDataset, inDataset);
            var           clipRaster  = (IRaster)outDataset1;
            ISaveAs       pSaveAs     = clipRaster as ISaveAs;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterCursor);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(raster2);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterDataSet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterWorkspace2);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFac);
            if (File.Exists(parth))
            {
                File.Delete(parth);
            }
            workspaceFac = new RasterWorkspaceFactoryClass();
            IDataset outdataset = pSaveAs.SaveAs(name, workspaceFac.OpenFromFile(directory, 0), "IMAGINE Image");

            System.Runtime.InteropServices.Marshal.ReleaseComObject(outdataset);
            return;
        }
Esempio n. 8
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]);
            }
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
0
 //计算栅格数据块的高程值
 public void CalPixelArray(ITinSurface pTinSurf, double UpLeftX, double UpLeftY, double XCellsize, double YCellsize, ref IPixelBlock3 pixelblock)
 {
     System.Array pArray = pixelblock.get_PixelData(0) as System.Array;
     for (int i = 0; i < pixelblock.Width; i++)
     {
         for (int j = 0; j < pixelblock.Height; j++)
         {
             double dheight = (double)pTinSurf.get_Z(UpLeftX + i * XCellsize, UpLeftY - j * YCellsize);
             pArray.SetValue(dheight, i, j);
         }
     }
     pixelblock.set_PixelData(0, pArray);
 }
Esempio n. 12
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());
     }
 }
Esempio n. 13
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());
     }
 }
Esempio n. 14
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;
     }
 }
Esempio n. 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
     {
         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;
     }
 }
Esempio n. 16
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)
 {
     //double vl = 0;
     //float pixelValue = 0f;
     try
     {
         //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue;
         // Call Read method of the Raster Function Helper object.
         myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
         IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock;
         #region Load log object
         for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++)
         {
             //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand));
             System.Array dArr = (System.Array)pb3.get_PixelData(nBand);
             rstPixelType pbT  = pb3.get_PixelType(nBand);
             for (int r = 0; r < pPixelBlock.Height; r++)
             {
                 for (int c = 0; c < pPixelBlock.Width; c++)
                 {
                     object objVl = pb3.GetVal(nBand, c, r);
                     if (objVl == null)
                     {
                         continue;
                     }
                     else
                     {
                         //vl = System.Convert.ToSingle(objVl);
                         //pixelValue = (float)getFunctionValue(vl);
                         double objVl2 = getFunctionValue(System.Convert.ToDouble(objVl));
                         object newVl  = rasterUtil.getSafeValue(objVl2, pbT);
                         dArr.SetValue(newVl, c, r);
                     }
                 }
             }
             pb3.set_PixelData(nBand, dArr);
         }
         #endregion
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception("Exception caught in Read method math Function. " + exc.Message, exc);
         throw myExc;
     }
 }
Esempio n. 17
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 IRasterDataset exportRasterData(string parth, IRasterLayer rasterLayer, float[,] rasterMat)
        {
            string directory = parth.Substring(0, parth.LastIndexOf("\\"));
            string name      = parth.Substring(parth.LastIndexOf("\\") + 1);

            IWorkspaceFactory workspaceFac     = new RasterWorkspaceFactoryClass();
            IRasterWorkspace2 rasterWorkspace2 = workspaceFac.OpenFromFile(directory, 0) as IRasterWorkspace2;

            IRasterInfo rasterInfo  = (rasterLayer.Raster as IRawBlocks).RasterInfo;
            IPoint      originPoint = new Point();

            originPoint.PutCoords(rasterInfo.Origin.X, rasterInfo.Origin.Y - (rasterLayer.Raster as IRasterProps).Height * (rasterLayer.Raster as IRasterProps).MeanCellSize().Y);
            IRasterProps   rasterProps   = rasterLayer.Raster as IRasterProps;
            IRasterDataset rasterDataSet = rasterWorkspace2.CreateRasterDataset(name, "IMAGINE Image", originPoint, rasterProps.Width, rasterProps.Height, rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, 1, rstPixelType.PT_FLOAT, rasterProps.SpatialReference, true);
            IRaster2       raster2       = rasterDataSet.CreateDefaultRaster() as IRaster2;
            IPnt           pntClass      = new Pnt();

            pntClass.X = rasterProps.Width;
            pntClass.Y = rasterProps.Height;

            IRasterCursor rasterCursor = raster2.CreateCursorEx(pntClass);
            IRasterEdit   rasterEdit   = raster2 as IRasterEdit;

            if (rasterEdit.CanEdit())
            {
                IRasterBandCollection bands       = rasterDataSet as IRasterBandCollection;
                IPixelBlock3          pixelBlock3 = rasterCursor.PixelBlock as IPixelBlock3;
                System.Array          pixels      = (System.Array)pixelBlock3.get_PixelData(0);
                for (int i = 0; i < rasterProps.Width; i++)
                {
                    for (int j = 0; j < rasterProps.Height; j++)
                    {
                        pixels.SetValue(Convert.ToSingle(rasterMat[j, i]), i, j);
                    }
                }
                pixelBlock3.set_PixelData(0, (System.Array)pixels);
                rasterEdit.Write(rasterCursor.TopLeft, (IPixelBlock)pixelBlock3);
            }
            (raster2 as IRasterProps).NoDataValue = 0f;
            rasterEdit.Refresh();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
            return(rasterDataSet);
        }
Esempio n. 19
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);
         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;
     }
 }
Esempio n. 20
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);
         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());
     }
 }
Esempio n. 21
0
        private bool WriteToRaster(IRaster pRaster, byte[,] dbData, Point2D ptLeftTop)
        {
            //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".
            IRasterProps pProps = pRaster as IRasterProps;
            int          width  = pProps.Width;
            int          height = pProps.Height;

            IPnt blocksize = new PntClass();

            blocksize.SetCoords(width, height);
            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 < width; i++)
            {
                for (int j = 0; j < height; 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);

            return(true);
        }
Esempio n. 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
            {
                // Call Read method of the Raster Function Helper object.
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                IPixelBlock3   ipPixelBlock = (IPixelBlock3)pPixelBlock;
                System.Array[] outArr       = new System.Array[bndCnt];
                int            pbHeight     = ipPixelBlock.Height;
                int            pbWidth      = ipPixelBlock.Width;
                for (int p = 0; p < ipPixelBlock.Planes; p++)
                {
                    outArr[p] = (System.Array)ipPixelBlock.get_PixelData(p);
                }

                for (int r = 0; r < pbHeight; r++)
                {
                    for (int c = 0; c < pbWidth; c++)
                    {
                        object vlObj = ipPixelBlock.GetVal(0, c, r);
                        if (vlObj != null)
                        {
                            float vl = System.Convert.ToSingle(vlObj);
                            getOutPutVl(ipPixelBlock, outArr, vl, c, r);
                        }
                    }
                }
                for (int p = 0; p < ipPixelBlock.Planes; p++)
                {
                    ipPixelBlock.set_PixelData(p, outArr[p]);
                }
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of localMean Function. " + exc.Message, exc);
                throw myExc;
            }
        }
Esempio n. 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.

                //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++)
                                {
                                    double vl    = c[v];
                                    object newVl = rasterUtil.getSafeValue(vl, ipPixelBlock.get_PixelType(v));
                                    pArr[v].SetValue(newVl, k, i);
                                }
                            }
                            else
                            {
                                for (int v = 0; v < pArr.Length; v++)
                                {
                                    pArr.SetValue(ndVl, 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());
            }
        }
Esempio n. 24
0
        /// <summary>
        /// TIN数据转栅格数据并进行坡度分析      张琪    20110614
        /// </summary>
        /// <param name="pTinAdvanced"></param>
        /// <param name="pRastConvType"></param>
        /// <param name="sDir"></param>
        /// <param name="sName"></param>
        /// <param name="ePixelType"></param>
        /// <param name="cellsize"></param>
        /// <param name="pExtent"></param>
        /// <param name="bPerm"></param>
        /// <param name="strType"></param>
        /// <returns></returns>
        public IRasterDataset TinToRaster(ITinAdvanced pTinAdvanced, esriRasterizationType pRastConvType, String sDir, String sName, rstPixelType ePixelType, Double cellsize, IEnvelope pExtent, bool bPerm, String strType)
        {
            try
            {
                ESRI.ArcGIS.Geometry.IPoint pOrigin = pExtent.LowerLeft;
                pOrigin.X = pOrigin.X - (cellsize * 0.5);
                pOrigin.Y = pOrigin.Y - (cellsize * 0.5);
                int nCol, nRow;
                nCol = Convert.ToInt32(Math.Round(pExtent.Width / cellsize)) + 1;
                nRow = Convert.ToInt32(Math.Round(pExtent.Height / cellsize)) + 1;
                IGeoDataset         pGeoDataset        = pTinAdvanced as IGeoDataset;
                ISpatialReference2  pSpatialReference2 = pGeoDataset.SpatialReference as ISpatialReference2;
                IRasterDataset      pRasterDataset     = CreateRasterSurf(sDir, sName, strType, pOrigin, nCol, nRow, cellsize, cellsize, ePixelType, pSpatialReference2, bPerm);
                IRawPixels          pRawPixels         = GetRawPixels(pRasterDataset, 0);
                object              pCache             = pRawPixels.AcquireCache();
                ITinSurface         pTinSurface        = pTinAdvanced as ITinSurface;
                IGeoDatabaseBridge2 pbridge2           = (IGeoDatabaseBridge2) new GeoDatabaseHelperClass();

                IRasterProps pRasterProps = pRawPixels as IRasterProps;
                //float nodataFloat;
                //int nodataInt;
                double dZMin = pTinAdvanced.Extent.ZMin;
                object vNoData;
                if (ePixelType.ToString() == "PT_FLOAT")
                {
                    vNoData = (dZMin - 1).ToString();
                }
                else
                {
                    vNoData = Convert.ToInt32((dZMin - 1));
                }
                pRasterProps.NoDataValue = vNoData;
                IPnt pOffset    = new DblPntClass();
                int  lMaxBlockX = 2048;
                if (nCol < lMaxBlockX)
                {
                    lMaxBlockX = nCol;
                }
                int lMaxBlockY = 2048;
                if (nRow < lMaxBlockY)
                {
                    lMaxBlockY = nRow;
                }
                IPnt pBlockSize = new DblPntClass();
                pBlockSize.X = lMaxBlockX;
                pBlockSize.Y = lMaxBlockY;
                IPixelBlock3 pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize) as IPixelBlock3;
                object       blockArray  = pPixelBlock.get_PixelDataByRef(0);
                ITrackCancel pCancel     = new CancelTrackerClass();
                pCancel.CancelOnClick    = false;
                pCancel.CancelOnKeyPress = true;
                int lBlockCount = Convert.ToInt32(Math.Round((nCol / lMaxBlockX) + 0.49) * Math.Round((nRow / lMaxBlockY) + 0.49));
                ESRI.ArcGIS.Geometry.IPoint pBlockOrigin = new ESRI.ArcGIS.Geometry.PointClass();
                int lColOffset, lRowOffset;

                for (lRowOffset = 0; lRowOffset < (nRow - 1);)
                {
                    for (lColOffset = 0; lColOffset < (nCol - 1);)
                    {
                        if ((nCol - lColOffset) < lMaxBlockX)
                        {
                            pBlockSize.X = (nCol - lColOffset);
                            pPixelBlock  = pRawPixels.CreatePixelBlock(pBlockSize) as IPixelBlock3;
                            blockArray   = pPixelBlock.get_PixelDataByRef(0);
                        }
                        pBlockOrigin.X = pOrigin.X + (lColOffset * cellsize) + (cellsize * 0.5);
                        pBlockOrigin.Y = pOrigin.Y + ((nRow - lRowOffset) * cellsize) - (cellsize * 0.5);
                        pbridge2.QueryPixelBlock(pTinSurface, pBlockOrigin.X, pBlockOrigin.Y, cellsize, cellsize, pRastConvType, vNoData, ref blockArray);
                        //pTinSurface.QueryPixelBlock(pBlockOrigin.X, pBlockOrigin.Y, cellsize, cellsize, pRastConvType, vNoData, blockArray);
                        pOffset.X = lColOffset;
                        pOffset.Y = lRowOffset;
                        pPixelBlock.set_PixelData(0, (System.Object)blockArray);
                        pRawPixels.Write(pOffset, pPixelBlock as IPixelBlock);
                        if (lBlockCount > 1)
                        {
                            if (!pCancel.Continue())
                            {
                                break;
                            }
                            else if (pTinAdvanced.ProcessCancelled)
                            {
                                break;
                            }
                        }
                        lColOffset = lColOffset + lMaxBlockX;
                    }
                    bool bReset = false;
                    if (pBlockSize.X != lMaxBlockX)
                    {
                        pBlockSize.X = lMaxBlockX;
                        bReset       = true;
                    }
                    if ((nRow - lRowOffset) < lMaxBlockY)
                    {
                        pBlockSize.Y = (nRow - lRowOffset);
                        bReset       = true;
                    }
                    if (bReset)
                    {
                        pPixelBlock.set_PixelData(0, blockArray);
                        pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize) as IPixelBlock3;
                        blockArray  = pPixelBlock.get_PixelDataByRef(0);
                    }
                    lRowOffset = lRowOffset + lMaxBlockY;
                }

                pRawPixels.ReturnCache(pCache);
                pCache         = null;
                pRawPixels     = null;
                pPixelBlock    = null;
                pRasterProps   = null;
                blockArray     = 0;
                pRasterDataset = OpenRasterDataset(sDir, sName);
                if (lBlockCount == 1)
                {
                    pTinAdvanced.TrackCancel = null;
                }
                return(pRasterDataset);
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 25
0
        //IRasterStatistics rasterStatic;
        private void SetRasterSymbol(IRasterLayer rasterLayer)
        {
            //获取选择的序号
            int index = sBC.SelectedIndex;
            ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
            IStyleGalleryItem    mStyleGalleryItem   = symbologyStyleClass.GetItem(index);
            //获取选择的符号
            IColorRamp select = (IColorRamp)mStyleGalleryItem.Item;
            IRasterStretchColorRampRenderer rasterStretchColorRampRenderer = new RasterStretchColorRampRendererClass();
            IRasterRenderer rasterRenderer = rasterStretchColorRampRenderer as IRasterRenderer;

            rasterRenderer.Raster = (rasterLayer).Raster;

            if (checkBox1.Checked == true)
            {
                //修改像元值 线性拉伸
                IRaster2 pRaster2  = rasterLayer.Raster 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 = rasterLayer as IRasterBandCollection;
                    IPixelBlock3 pPixelblock3 = null;
                    int          pBlockwidth  = 0;
                    int          pBlockheight = 0;
                    System.Array pixels;
                    IPnt         pPnt = null;
                    object       pValue;
                    // long pBandCount = pBands.Count;

                    //获取Nodata
                    //IRasterProps pRasterPro = pRaster2 as IRasterProps;

                    //object pNodata = pRasterPro.NoDataValue;
                    do
                    {
                        pPixelblock3 = pRasterCursor.PixelBlock as IPixelBlock3;
                        pBlockwidth  = pPixelblock3.Width;
                        pBlockheight = pPixelblock3.Height;

                        //   for (int k = 0; k < pBandCount; k++)

                        //IRasterBandCollection bandCollection;
                        //IRasterLayer irasterLayer = rasterLayer;
                        //IRaster2 raster = rasterLayer.Raster as IRaster2;
                        //IRasterDataset rd = raster.RasterDataset;
                        //bandCollection = rd as IRasterBandCollection;
                        //IEnumRasterBand enumband = bandCollection.Bands;
                        //IRasterBand rasterBand = enumband.Next();
                        //rasterStatic = null;
                        //if (rasterBand != null && rasterBand.Statistics != null)
                        //{
                        //    rasterStatic = rasterBand.Statistics;
                        //}


                        int pixelmax, pixelmin;


                        {
                            pixels   = (System.Array)pPixelblock3.get_PixelData(0);
                            pixelmax = Convert.ToInt32(pixels.GetValue(0, 0));
                            pixelmin = pixelmax;

                            for (int i = 0; i < pBlockwidth; i++)
                            {
                                for (int j = 0; j < pBlockheight; j++)
                                {
                                    pValue = pixels.GetValue(i, j);


                                    {
                                        // pixels.SetValue(Convert.ToByte(Convert.ToInt32(pValue) * strscale), i, j);
                                        if (Convert.ToInt32(pValue) > pixelmax)
                                        {
                                            pixelmax = Convert.ToInt32(pValue);
                                        }
                                        if (Convert.ToInt32(pValue) < pixelmin)
                                        {
                                            pixelmin = Convert.ToInt32(pValue);
                                        }
                                    }
                                }
                            }
                            double strscale = ((double)pixelmax - (double)pixelmin) / 255.0;

                            for (int i = 0; i < pBlockwidth; i++)
                            {
                                for (int j = 0; j < pBlockheight; j++)
                                {
                                    pValue = pixels.GetValue(i, j);


                                    {
                                        pixels.SetValue(Convert.ToByte((Convert.ToInt32(pValue) - pixelmin) / (strscale)), i, j);
                                    }
                                }
                            }


                            pPixelblock3.set_PixelData(0, pixels);
                        }
                        pPnt = pRasterCursor.TopLeft;
                        pRasterEdit.Write(pPnt, (IPixelBlock)pPixelblock3);
                    }while (pRasterCursor.Next());
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);
                    // MessageBox.Show("done");
                }
            }



            rasterRenderer.Update();
            rasterStretchColorRampRenderer.ColorRamp = select;
            rasterRenderer.Update();
            ((IRasterLayer)rasterLayer).Renderer = rasterRenderer;
        }
        // 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);
            }
        }
Esempio n. 27
0
        private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock)
        {
            System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes];
            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.esriSpatialRelIndexIntersects;
            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);
            }
            //IQueryFilter qf = new QueryFilterClass();
            //string s1 = "(XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")";
            //string s2 = "(XMIN > " + env.XMin.ToString() + " AND XMIN < " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")";
            //string s3 = "(XMAX < " + env.XMax.ToString() + " AND XMIN > " + env.XMin.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")";
            //string s4 = "(YMIN > " + env.YMin.ToString() + " AND YMIN < " + env.YMax.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")";
            //string s5 = "(YMAX < " + env.YMax.ToString() + " AND YMIN > " + env.YMin.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")";
            //qf.WhereClause = s1 + " OR " + s2 + " OR " + s3 + " OR " + s4 + " OR " + s5;
            //ICursor cur = tbl.Search(qf, false);
            //IRow rw = cur.NextRow();
            List <IPixelBlock> inPbValue = new List <IPixelBlock>();

            //while (rw != null)
            //{
            //    int rsIndex = System.Convert.ToInt32(rw.get_Value(catIndex));
            //    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);
            //    inPbValue.Add(inputPb);
            //    rw = cur.NextRow();
            //}

            while (ftr != null)
            {
                int         rsIndex = System.Convert.ToInt32(ftr.get_Value(catIndex));
                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);
                inPbValue.Add(inputPb);
                ftr = fCur.NextFeature();
            }
            for (int i = 0; i < ipPixelBlock.Planes; i++)
            {
                for (int r = 0; r < ipPixelBlock.Height; r++)
                {
                    for (int c = 0; c < ipPixelBlock.Width; c++)
                    {
                        object vl = getValue(i, c, r, inPbValue);
                        outPixelValuesArr[i].SetValue(vl, c, r);
                    }
                }
            }
            for (int i = 0; i < outPixelValuesArr.Length; i++)
            {
                ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]);
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Write edits to the input raster.
        /// </summary>
        /// <param name="raster"></param>
        private static void WriteEdits(IRaster raster)
        {
            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 < Editor.Edits.Count; i++)
            {
                #region Get the extent of the edition region

                Position cellPos = Editor.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 < Editor.Edits.Count; i++)
            {
                object value = null;
                Editor.CSharpValue2PixelValue(Editor.Edits[i].NewValue, rasterProps.PixelType, out value);

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

            pixelBlock3.set_PixelData(0, (System.Object)pixels);
            IRasterEdit rasterEdit = (IRasterEdit)raster;
            rasterEdit.Write(pos, (IPixelBlock)pixelBlock3);
        }
Esempio n. 29
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)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];
            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.esriSpatialRelIndexIntersects;
            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);
            }
            //IQueryFilter qf = new QueryFilterClass();
            //string s1 = "(XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")";
            //string s2 = "(XMIN > " + env.XMin.ToString() + " AND XMIN < " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")";
            //string s3 = "(XMAX < " + env.XMax.ToString() + " AND XMIN > " + env.XMin.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")";
            //string s4 = "(YMIN > " + env.YMin.ToString() + " AND YMIN < " + env.YMax.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")";
            //string s5 = "(YMAX < " + env.YMax.ToString() + " AND YMIN > " + env.YMin.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")";
            //qf.WhereClause = s1 + " OR " + s2 + " OR " + s3 + " OR " + s4 + " OR " + s5;
            //ICursor cur = tbl.Search(qf, false);
            //IRow rw = cur.NextRow();
            List<IPixelBlock> inPbValue = new List<IPixelBlock>();
            //while (rw != null)
            //{
            //    int rsIndex = System.Convert.ToInt32(rw.get_Value(catIndex));
            //    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);
            //    inPbValue.Add(inputPb);
            //    rw = cur.NextRow();
            //}

            while (ftr != null)
            {
                int rsIndex = System.Convert.ToInt32(ftr.get_Value(catIndex));
                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);
                inPbValue.Add(inputPb);
                ftr = fCur.NextFeature();
            }
            for (int i = 0; i < ipPixelBlock.Planes; i++)
            {
                for (int r = 0; r < ipPixelBlock.Height; r++)
                {
                    for (int c = 0; c < ipPixelBlock.Width; c++)
                    {
                        object vl = getValue(i, c, r, inPbValue);
                        outPixelValuesArr[i].SetValue(vl,c,r);
                    }
                }
            }
            for (int i = 0; i < outPixelValuesArr.Length; i++)
            {
                ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]);
            }
        }
 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]);
     }
 }
Esempio n. 32
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 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());
     }
 }