Create a 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 watermark function that uses GDI+ to read a small watermark image and blend it with the raster to which it is applied.
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 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 WatermarkFunction and WatermarkFunctionUI projects.
  3. The function can now be used like any of the built-in functions in the system.

Updating XmlSupport.dat to be able to serialize functions to and from xml
  1. Open the XmlSupportSubsetWM.dat file in the TestWatermarkFunction 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 file 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, open the Insert menu, and click the NDVI Custom function. This opens the Watermark Function UI page, which allows you to set parameters.
  8. Input the location of the watermark, the path to the watermark image, and the blend percentage.
  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 TestWatermarkFunction project.
  3. Set the flags to add the Watermark 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 Watermark Custom function (that is, the location of the watermark, the path to the watermark image and the blending percentage).
  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.

WatermarkFunction\WatermarkFunction.cs Source file that implements the custom raster function.
WatermarkFunctionUI\WatermarkFunctionUIClass.cs Source file that implements the custom raster function UI.
TestWatermarkFunction\TestWatermarkFunction.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
WatermarkFunction\WatermarkFunction.vb Source file that implements the custom raster function.
WatermarkFunctionUI\WatermarkFunctionUIClass.vb Source file that implements the custom raster function UI.
TestWatermarkFunction\TestWatermarkFunction.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