ericmweber

How can I reliably set the NoData value of a raster?

Discussion created by ericmweber on Aug 20, 2012
Latest reply on Apr 8, 2015 by limgeomatics_u2
I want to clip a raster and retain the NoData value from the source raster in the clipped raster. In the method below, I follow these steps:

(1) use map algebra to clip the source raster to the new extent
(2) use IRasterProps to set the pixel type and NoData value to be the same as the source raster
(3) use ISaveAs to save the new raster dataset

The source raster I've been testing with is a 16-bit signed integer raster with a NoData value of -32768. Setting the pixel type works correctly, but the NoData value always ends up being 32767.

I have also tried using the geoprocessing clip method (http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/How_to_clip_a_raster_dataset_with_an_envelope_or_a_dataset/0001000000n5000000/), but I still end up with 32767.

(Other specifics in my testing that may or may not be relevant: input raster is GRID, output is file geodatabase)

        public static IRaster ClipRaster(IRaster2 sourceRaster, IEnvelope extent, IWorkspace workspace, string rasterName, string rasterFormat)
        {
            //map algebra method for clipping
            //NoData value is not set properly (always ends up as largest positive integer supported by pixel type)

            //clip
            SetRasterAnalysisEnvCellSize(sourceRaster, true);
            SetRasterAnalysisEnvExtent(extent, sourceRaster, true);
            IMapAlgebraOp mapAlgebraOp = new RasterMapAlgebraOpClass();
            string rasterSymbol = "source";
            mapAlgebraOp.BindRaster((IGeoDataset)sourceRaster, rasterSymbol);
            IGeoDataset tempClipRaster = mapAlgebraOp.Execute("[" + rasterSymbol + "]");
         
            //set pixel type and nodata value to match input raster and save
            IRasterProps propsClip = tempClipRaster as IRasterProps;
            IRasterProps propsSource = sourceRaster as IRasterProps;
            propsClip.PixelType = propsSource.PixelType;
            propsClip.NoDataValue = propsSource.NoDataValue; // this doesn't stick
            IRasterDataset3 clipOutput = ((ISaveAs2)tempClipRaster).SaveAs(rasterName, workspace, rasterFormat) as IRasterDataset3;
            
            //delete the temporary dataset created by the map algebra op
            if (((IDataset)((IRaster2)tempClipRaster).RasterDataset).CanDelete())
                ((IDataset)((IRaster2)tempClipRaster).RasterDataset).Delete();

            IRaster outRaster = clipOutput.CreateDefaultRaster();
            //IRaster outRaster = clipOutput.CreateFullRaster(); //this method has same result
            return outRaster;
        }

Outcomes