Create an NDVI custom raster function


Purpose
This sample shows how to create a custom raster function, its corresponding arguments object and the user interface (UI) for the function by implementing the IRasterFunction and IRasterFunctionArguments interfaces along with additional interfaces. The implemented function is a function that calculates the Normalized Differential Vegetation index (NDVI) for an image given the indexes of the Near Infrared and Red bands.
The Function UI can be used to view or modify the parameters of the function. The function editor can be used to add or remove it from a function raster dataset or mosaic dataset. The optional test application provided can be used to apply the custom function to a raster dataset or a mosaic dataset, create a Raster Function template from the custom function, serialize the function template to Extensible Markup Language (XML), and deserialize it back from an XML file.
Note: To be able to serialize a function to and from an XML file, you need to update the XmlSupport.dat file, which is described in this sample topic.

How to use

See How to use ArcGIS samples for help on compiling, setting up the debugger, and running the sample. If the sample has associated data, you will find that the sample's zip file includes a "data" folder alongside the language folders. However, you will need to update the sample to point to the location of the data once you have extracted all the files.

Setting up the custom function
  1. Start Visual Studio and open the solution.
  2. Build the NDVICustomFunction and NDVICustomFunctionUI projects.
  3. The function can now be used like any of the built-in functions in the system.

Updating XmlSupport.dat to serialize functions to and from XML
  1. Open the XmlSupportSubsetNC.dat file in the TestNDVICustomFunction folder. Copy its contents.
  2. Open <Program Files>\ArcGIS\<Desktop Directory>\bin\XmlSupport.dat. This is an XML file containing types that can be serialized to and from an XML file. Create a backup of the file.
  3. Paste the contents copied in Step 1 inside the main node of the XML along with the other type entries. Make sure it's between type nodes and not inside one.
  4. Save the XMLSupport.dat file.

Using the custom function in desktop
  1. Start ArcMap.
  2. Open the raster dataset on which the function is to be applied.
  3. Open the Image Analysis window.
  4. Select the raster dataset on which the function is to be applied in the topmost section.
  5. In ArcGIS 10.0, under the Processing section, click the Clip button, open the properties of the newly created layer, then navigate to the functions tab. The function editor opens. Right-click the Clip function and click Remove.
  6. In ArcGIS 10.1 and after, under the Processing section, click the fx button. The function editor opens.
  7. Right-click the dataset and open the Insert menu, then click the NDVI Custom function. This opens the NDVI Function UI page, which allows you to set the parameter.
  8. Input the band indexes for the NIR and Red bands separated by a space.
  9. Click OK.
  10. In ArcGIS 10.0, the layer now represents the output of the custom function applied to the raster dataset.
  11. In ArcGIS 10.1 and after, a new layer is created that represents the output of the custom function applied to the raster dataset.

Using the custom function test application
  1. Start Visual Studio and open the solution file.
  2. Open the TestNDVICustomFunction project.
  3. Set the flags to add the NDVI Custom function on top of a raster dataset and a mosaic dataset, to serialize a Raster Function template containing the custom function to an XML file, or to deserialize an XML file back to a function template containing the custom function.
  4. Edit the strings in the Specify inputs region to specify the directory and name of the input dataset, the Geodatabase and name of the mosaic dataset, the output directory, the name of the output dataset (including the .afr extension), and the full path to the XML file to write out or read from if needed.
  5. Set the parameters for the NDVI Custom function (that is, indexes of the NIR and Red band delimited by spaces).
  6. Compile and run the program. If the program succeeds, the console shows a Success message. If an error occurs, it shows on the console window.
  7. If adding on top of a raster dataset, load the created output .afr file into an ArcMap or ArcCatalog dataset. An image that represents the output of the custom function applied to the raster dataset appears.
  8. If adding on top of a mosaic dataset, load the mosaic dataset to which the custom function was added in ArcMap or ArcCatalog. An image that represents the output of the custom function applied to the raster dataset appears.

NDVICustomFunction\NDVICustomFunction.cs Source file that implements the custom raster function.
NDVICustomFunctionUI\NDVICustomFunctionUIClass.cs Source file that implements the custom raster function UI.
TestNDVICustomFunction\TestNDVICustomFunction.cs Source file for the test application that can be used to apply the custom function to a raster dataset or a mosaic dataset.
Download the C# files
NDVICustomFunction\NDVICustomFunction.vb Source file that implements the custom raster function.
NDVICustomFunctionUI\NDVICustomFunctionUIClass.vb Source file that implements the custom raster function UI.
TestNDVICustomFunction\TestNDVICustomFunction.vb Source file for the test application that can be used to apply the custom function to a raster dataset or a mosaic dataset.
Download the VB.NET files

Download the files for all languages




Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine