ArcObjects Library Reference  

NodataFilter

About the Create a custom NoData pixel filter Sample

[C#]

NodataFilter.cs

using System;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;

namespace CustomNodataFilter
{
    //This sample shows the steps to create a customized pixelfilter
    //INodataFilter filters out a range of values in a raster to be nodata
    public interface INodataFilter : IPixelFilter
    {
        //IPixelFilter methods
        new void Filter(IPixelBlock pPixelBlock);
        new void GetCenterPosition(ref int x, ref int y);
        new void GetSize(ref int nCols, ref int nRows);

        //INodataFilter members
        int MinNodataValue { get; set;}
        int MaxNodataValue { get; set;}
    }

    public class NodataFilter : INodataFilter
    {
        public void Filter(IPixelBlock pPixelBlock)
        {
            try
            {
                IPixelBlock3 pPixelBlock3 = (IPixelBlock3)pPixelBlock;

                byte[] lookup = new byte[8] { 128, 64, 32, 16, 8, 4, 2, 1 };

                //get number of bands
                int plane = pPixelBlock.Planes;

                //loop through each band
                for (int i = 0; i < plane; i++)
                {
                    //get nodata mask array
                    byte[] outputArray = (byte[])pPixelBlock3.get_NoDataMaskByRef(i);

                    //loop through each pixel in the pixelblock and do calculation
                    for (int x = 0; x < pPixelBlock.Width; x++)
                    {
                        for (int y = 0; y < pPixelBlock.Height; y++)
                        {
                            //get index in the nodata mask byte array
                            int ind = x + y * (pPixelBlock.Width);

                            //get nodata mask byte 
                            byte nd = outputArray[ind / 8];

                            //get pixel value and check if it is nodata
                            object tempVal = pPixelBlock3.GetVal(i, x, y);

                            if (tempVal != null) //not nodata pixel
                            {
                                //convert pixel value to int and compare with nodata range

                                int curVal = Convert.ToInt32(tempVal);
                                if (curVal >= minNodataValue && curVal <= maxNodataValue)
                                {
                                    outputArray[ind / 8] = (byte)(nd & ~lookup[ind % 8]);
                                }
                            }
                        }
                    }
                    //set nodata mask array
                    pPixelBlock3.set_NoDataMask(i, outputArray);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //implements IPixelFilter:GetCenterPosition
        public void GetCenterPosition(ref int x, ref int y)
        {
            x = 0;
            y = 0;
        }

        //implements IPixelFilter:GetSize
        public void GetSize(ref int nCols, ref int nRows)
        {
            nCols = 0;
            nRows = 0;
        }

        //get/set max range of nodata 
        public int MaxNodataValue
        {
            get
            {
                return maxNodataValue;
            }
            set
            {
                maxNodataValue = value;
            }
        }

        //get/set min range of nodata
        public int MinNodataValue
        {
            get
            {
                return minNodataValue;
            }
            set
            {
                minNodataValue = value;
            }
        }

        private int minNodataValue;
        private int maxNodataValue;

    }
}
[Visual Basic .NET]

NodataFilter.vb

Imports System
Imports ESRI.ArcGIS.DataSourcesRaster
Imports ESRI.ArcGIS.Geodatabase

'This sample shows the steps to create a customized pixelfilter
'INodataFilter filters out a range of values in a raster to be nodata
Public Interface INodataFilter
  Inherits IPixelFilter

  'INodataFilter members
  Property MinNodataValue() As Integer
  Property MaxNodataValue() As Integer

End Interface

Public Class NodataFilter
  Implements INodataFilter
  Sub Filter(ByVal pPixelBlock As IPixelBlock) Implements INodataFilter.Filter
    Dim x As Integer
    Dim y As Integer
    Try
      Dim pPixelBlock3 As IPixelBlock3 = CType(pPixelBlock, IPixelBlock3)

      Dim lookup() As Byte = New Byte(7) {128, 64, 32, 16, 8, 4, 2, 1}

      'get number of bands
      Dim plane As Integer = pPixelBlock.Planes

      'loop through each band
      Dim i As Integer
      For i = 0 To plane - 1
        'get nodata mask array
        Dim outputArray() As Byte = CType(pPixelBlock3.NoDataMask(i), Byte())

        'loop through each pixel in the pixelblock and do calculation

        For y = 0 To pPixelBlock.Height - 1
          For x = 0 To pPixelBlock.Width - 1
            'get index in the nodata mask byte array
            Dim ind As Integer = x + y * (pPixelBlock.Width)

            'get nodata mask byte 
            Dim nd As Byte = outputArray(ind \ 8)

            'get pixel value and check if it is nodata
            Dim tempVal As Object = pPixelBlock3.GetVal(i, x, y)

            If Not tempVal Is Nothing Then
              'convert pixel value to int and compare with nodata range

              Dim curVal As Integer = Convert.ToInt32(tempVal)
              If curVal >= MinNodataValue And curVal <= MaxNodataValue Then
                outputArray(ind \ 8) = CType((nd - lookup(ind Mod 8)), Byte)
              End If
            End If
          Next
        Next
        'set nodata mask array
        pPixelBlock3.NoDataMask(i) = outputArray
      Next
    Catch e As Exception
      Console.WriteLine(e.Message)
    End Try
  End Sub

  'implements IPixelFilter:GetCenterPosition
  Public Sub GetCenterPosition(ByRef x As Integer, ByRef y As Integer) Implements INodataFilter.GetCenterPosition
    x = 0
    y = 0
  End Sub

  'implements IPixelFilter:GetSize
  Public Sub GetSize(ByRef nCols As Integer, ByRef nRows As Integer) Implements INodataFilter.GetSize
    nCols = 0
    nRows = 0
  End Sub

  'get/set max range of nodata 
  Public Property MaxNodataValue() As Integer Implements INodataFilter.MaxNodataValue
    Get
      Return maxNDValue
    End Get
    Set(ByVal Value As Integer)
      maxNDValue = Value
    End Set
  End Property

  'get/set min range of nodata
  Public Property MinNodataValue() As Integer Implements INodataFilter.MinNodataValue
    Get
      Return minNDValue
    End Get
    Set(ByVal Value As Integer)
      minNDValue = Value
    End Set
  End Property

  Private minNDValue As Integer
  Private maxNDValue As Integer

End Class