Codes to set "Nodata" to 0 value

344
1
02-18-2012 06:49 AM
MintuMedhi
New Contributor
Respected sir/madam
I am looking for the codes to set the value of NoData pixeles to 0 value through C# programing.I need to calculate cube root of (B4-B3).Here B4=Band 3(NIR band),B3=Band3(Red band).So if B4>B3 then no problem but if B3>B4 then it comes as NoData because cube root of a negative value is a complex number. So I want to set this NoData values to 0.Please give some code in C#.
I have applied the following code
public void UsingRasterMapAlgebra(string filePath1, string fileName1, string filePath2, string fileName2)
        {
            //MessageBox.Show(System.Windows.Forms.ProgressBar.DefaultFont.ToString());
            //Get rasters.
            IRasterDataset inRas01 = new RasterDatasetClass();
            inRas01.OpenFromFile(filePath1 + fileName1);

            IRasterDataset inRas02 = new RasterDatasetClass();
            inRas02.OpenFromFile(filePath2 + fileName2);
           // create RasterMapAlgebraOp.
            IMapAlgebraOp mapAlgebraOp;
            mapAlgebraOp = new RasterMapAlgebraOpClass();
            //Set environment.
            IRasterAnalysisEnvironment env = default(IRasterAnalysisEnvironment);
            env = (IRasterAnalysisEnvironment)mapAlgebraOp;

            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            IWorkspace workspace = workspaceFactory.OpenFromFile(@"G:\data\image\1999etmdec\LE71370421999344SGS01\Arc object", 0);
            env.OutWorkspace = workspace;
            mapAlgebraOp.BindRaster((IGeoDataset)inRas01, "B1");
            mapAlgebraOp.BindRaster((IGeoDataset)inRas02, "B2");
            IRaster rasOut; IGeoDataset condition; IGeoDataset condition2;
            IGeoDataset rasOutAVI;
            IMxDocument pMxDocument = (IMxDocument)m_application.Document;
            try
            {
                rasOut = (IRaster)mapAlgebraOp.Execute("(([B2] + 1) * (256 - [B1]) * ([B2] - [B1]))");
                IMathOp pMathOp;
                pMathOp = new RasterMathOps() as IMathOp;
                rasOutAVI = pMathOp.Power((IGeoDataset)rasOut, .33);
//set NoData
IRasterProps rasterProps = (IRasterProps)rasOutAVI;
                rasterProps.NoDataValue = 0;


                // Display the AVI
                IMap pMap = pMxDocument.FocusMap;
                IRasterLayer pRasterLayer3 = new RasterLayerClass();
                pRasterLayer3.CreateFromRaster(rasterProps as IRaster);
                pRasterLayer3.Name = "AVI2";
                pMap.AddLayer(pRasterLayer3);
                ISaveAs2 saveAsAVI;
                saveAsAVI = (ISaveAs2)rasterProps;
                IRasterStorageDef rasterStorageDef = new RasterStorageDefClass();
                IRasterStorageDef2 rasterStorageDef2 = (IRasterStorageDef2)rasterStorageDef;

                rasterStorageDef2.PyramidLevel = 95;
                saveAsAVI.SaveAsRasterDataset("AVI2.img", workspace, "IMAGINE Image", rasterStorageDef2);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
but this code is not replacing Nodata valued pixels with 0 value. Where I have done the mistake? is there any other code to replace Nodata value?
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor
You can do this in one of 2 ways, create a geoprocessor object and call the spatial analyst CON tool. If you want to stay within the arcobject environment then you need to be using the IConditionalOp Interface and do a conditional operation (if nodata set to zero else use existing cell value).

Duncan
0 Kudos