rohitp

Cannot update raster using safearray .... please help

Discussion created by rohitp on May 5, 2010
Latest reply on May 5, 2010 by rohitp
Hello,

I have been trying to create a raster and update its values but its not working. The output raster is created successfully, safearray has correct values written to it but the raster is not updated. I am only reading 1/4 of the raster at a time in pixelblock because if it try to read it all it gives me an error. I will be running a loop to read the complete raster block by block. Can anyone please help me resolve the problem. Here is my code.

Thanks in advance.

public void Calc()
        {
            

            // CREATE EUCLIDIAN RASTER PIXEL BLOCK
            RasterWorkspaceFactory factory = new RasterWorkspaceFactory();
            IRasterWorkspace workspace = (IRasterWorkspace)factory.OpenFromFile(OutputPath, 0);
            IRasterDataset pEUCRasterDS = workspace.OpenRasterDataset("EUC1");
            IRasterBandCollection pEUCBandColl = (IRasterBandCollection)pEUCRasterDS;
            IRasterBand pEUCBand = pEUCBandColl.Item(0);
            IRasterProps pEUCProps = (IRasterProps)pEUCBand;
            IRawPixels pEUCRawPixels = (IRawPixels) pEUCBand;
            //IRaster pEUCDefaultRaster = EUCRasterDS.CreateDefaultRaster();
           
            
            //CREATE OUTPUT RASTER PIXEL BLOCK
            IRasterDataset pOutRasterDS = CreateOutputRaster(OutputPath, "calc1", pEUCProps);
            IRaster pOutDefaultRaster = pOutRasterDS.CreateDefaultRaster();
            IRasterBandCollection pOutBandColl = (IRasterBandCollection) pOutRasterDS;
            IRasterBand pOutBand = pOutBandColl.Item(0);
            IRasterProps pOutProps = (IRasterProps)pOutBand;
            IRawPixels pOutRawPixels = (IRawPixels) pOutBand;
            
            
            DblPnt blockStart = new DblPnt();
            DblPnt blockExtent = new DblPnt();
            blockStart.SetCoords(0, 0);
            blockExtent.SetCoords(pEUCProps.Width/2, pEUCProps.Height/2); 
// if i use pEUCProps.Width then i get error "the input string is not a geographic or projected coordinate system". This i believe is due to the huge size of array
            
            IPixelBlock pEUCBlock = pEUCRawPixels.CreatePixelBlock(blockExtent);
            IPixelBlock pOutBlock = pOutRawPixels.CreatePixelBlock(blockExtent);

            pEUCRawPixels.Read(blockStart, pEUCBlock);

            int[,] safearray = (int[,])pOutBlock.get_SafeArray(0);

            for (int i = 0; i < blockExtent.X; i++)
            {
                for (int j = 0; j < blockExtent.Y; j++)
                {
                    safearray[i, j] = Convert.ToInt32(pEUCBlock.GetVal(0, i, j));
                   
                }
            }

            pOutRawPixels.Write(blockStart, pOutBlock);
        }
   
    

        private IRasterDataset CreateOutputRaster(string sPath, string OutputName, IRasterProps OutputRasterProps)
        {
            RasterWorkspaceFactory factory = new RasterWorkspaceFactory();


            if (factory.IsWorkspace(sPath))
            {
                IRasterWorkspace workspace = (IRasterWorkspace)factory.OpenFromFile(sPath, 0);
                
                IRasterWorkspace2 rasterWorskapce = (IRasterWorkspace2)workspace;
                IPnt outMeanPixel = OutputRasterProps.MeanCellSize();
                
                Point originPoint = new Point();
                originPoint.X = OutputRasterProps.Extent.XMin;
                originPoint.Y = OutputRasterProps.Extent.YMin;

                IRasterDataset outputRaster = rasterWorskapce.CreateRasterDataset(OutputName,
                    "GRID", originPoint, OutputRasterProps.Width, OutputRasterProps.Height,
                    outMeanPixel.X, outMeanPixel.Y, 1, rstPixelType.PT_LONG, OutputRasterProps.SpatialReference, true);

                return outputRaster;


            }
              return null;
        }

Outcomes