About the Create an NDVI custom raster function Sample
[C#]
NDVICustomFunctionUIForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.CatalogUI;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
namespace NDVICustomFunctionUI
{
public partial class NDVICustomFunctionUIForm : Form
{
#region Private Members
private object myInputRaster;
private string myBandIndices;
private bool myDirtyFlag;
#endregion
#region NDVICustomFunctionUIForm Properties
/// <summary>
/// Constructor
/// </summary>
public NDVICustomFunctionUIForm()
{
InitializeComponent();
myInputRaster = null;
myBandIndices = "";
HintLbl.Text = "NIR Red";
}
/// <summary>
/// Get or set the band indices.
/// </summary>
public string BandIndices
{
get
{
myBandIndices = BandIndicesTxtBox.Text;
return myBandIndices;
}
set
{
myBandIndices = value;
BandIndicesTxtBox.Text = value;
}
}
/// <summary>
/// Flag to specify if the form has changed
/// </summary>
public bool IsFormDirty
{
get
{
return myDirtyFlag;
}
set
{
myDirtyFlag = value;
}
}
/// <summary>
/// Get or set the input raster
/// </summary>
public object InputRaster
{
get
{
return myInputRaster;
}
set
{
myInputRaster = value;
inputRasterTxtbox.Text = GetInputRasterName(myInputRaster);
}
}
#endregion
#region NDVICustomFunctionUIForm Members
/// <summary>
/// This function takes a raster object and returns the formatted name of
/// the object for display in the UI.
/// </summary>
/// <param name="inputRaster">Object whose name is to be found</param>
/// <returns>Name of the object</returns>
private string GetInputRasterName(object inputRaster)
{
if ((inputRaster is IRasterDataset))
{
IRasterDataset rasterDataset = (IRasterDataset)inputRaster;
return rasterDataset.CompleteName;
}
if ((inputRaster is IRaster))
{
IRaster myRaster = (IRaster)inputRaster;
return ((IRaster2)myRaster).RasterDataset.CompleteName;
}
if (inputRaster is IDataset)
{
IDataset dataset = (IDataset)inputRaster;
return dataset.Name;
}
if (inputRaster is IName)
{
if (inputRaster is IDatasetName)
{
IDatasetName inputDSName = (IDatasetName)inputRaster;
return inputDSName.Name;
}
if (inputRaster is IFunctionRasterDatasetName)
{
IFunctionRasterDatasetName inputFRDName = (IFunctionRasterDatasetName)inputRaster;
return inputFRDName.BrowseName;
}
if (inputRaster is IMosaicDatasetName)
{
IMosaicDatasetName inputMDName = (IMosaicDatasetName)inputRaster;
return "MD";
}
IName inputName = (IName)inputRaster;
return inputName.NameString;
}
if (inputRaster is IRasterFunctionTemplate)
{
IRasterFunctionTemplate rasterFunctionTemplate =
(IRasterFunctionTemplate)inputRaster;
return rasterFunctionTemplate.Function.Name;
}
if (inputRaster is IRasterFunctionVariable)
{
IRasterFunctionVariable rasterFunctionVariable =
(IRasterFunctionVariable)inputRaster;
return rasterFunctionVariable.Name;
}
return "";
}
/// <summary>
/// Updates the UI textboxes using the properties that have been set.
/// </summary>
public void UpdateUI()
{
if (myInputRaster != null)
inputRasterTxtbox.Text = GetInputRasterName(myInputRaster);
BandIndicesTxtBox.Text = myBandIndices;
}
private void inputRasterBtn_Click(object sender, EventArgs e)
{
IEnumGxObject ipSelectedObjects = null;
ShowRasterDatasetBrowser((int)(Handle.ToInt32()), out ipSelectedObjects);
IGxObject selectedObject = ipSelectedObjects.Next();
if (selectedObject is IGxDataset)
{
IGxDataset ipGxDS = (IGxDataset)selectedObject;
IDataset ipDataset;
ipDataset = ipGxDS.Dataset;
myInputRaster = ipDataset.FullName;
inputRasterTxtbox.Text = GetInputRasterName(myInputRaster);
myDirtyFlag = true;
}
}
public void ShowRasterDatasetBrowser(int handle, out IEnumGxObject ipSelectedObjects)
{
IGxObjectFilterCollection ipFilterCollection = new GxDialogClass();
IGxObjectFilter ipFilter1 = new GxFilterRasterDatasetsClass();
ipFilterCollection.AddFilter(ipFilter1, true);
IGxDialog ipGxDialog = (IGxDialog)(ipFilterCollection);
ipGxDialog.RememberLocation = true;
ipGxDialog.Title = "Open";
ipGxDialog.AllowMultiSelect = false;
ipGxDialog.RememberLocation = true;
ipGxDialog.DoModalOpen((int)(Handle.ToInt32()), out ipSelectedObjects);
return;
}
private void BandIndicesTxtBox_TextChanged(object sender, EventArgs e)
{
myBandIndices = BandIndicesTxtBox.Text;
myDirtyFlag = true;
}
#endregion
}
}
[Visual Basic .NET]
NDVICustomFunctionUIForm.vb
Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Imports System.Text Imports System.Windows.Forms Imports ESRI.ArcGIS.Catalog Imports ESRI.ArcGIS.CatalogUI Imports ESRI.ArcGIS.DataSourcesRaster Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.esriSystem Public Partial Class NDVICustomFunctionUIForm Inherits Form #Region "Private Members" Private myInputRaster As Object Private myBandIndices As String Private myDirtyFlag As Boolean #End Region #Region "NDVICustomFunctionUIForm Properties" ''' <summary> ''' Constructor ''' </summary> Public Sub New() InitializeComponent() myInputRaster = Nothing myBandIndices = "" HintLbl.Text = "NIR Red" End Sub ''' <summary> ''' Get or set the band indices. ''' </summary> Public Property BandIndices() As String Get myBandIndices = BandIndicesTxtBox.Text Return myBandIndices End Get Set myBandIndices = value BandIndicesTxtBox.Text = value End Set End Property ''' <summary> ''' Flag to specify if the form has changed ''' </summary> Public Property IsFormDirty() As Boolean Get Return myDirtyFlag End Get Set myDirtyFlag = value End Set End Property ''' <summary> ''' Get or set the input raster ''' </summary> Public Property InputRaster() As Object Get Return myInputRaster End Get Set myInputRaster = value inputRasterTxtbox.Text = GetInputRasterName(myInputRaster) End Set End Property #End Region #Region "NDVICustomFunctionUIForm Members" ''' <summary> ''' This function takes a raster object and returns the formatted name of ''' the object for display in the UI. ''' </summary> ''' <param name="inputRaster">Object whose name is to be found</param> ''' <returns>Name of the object</returns> Private Function GetInputRasterName(inputRaster As Object) As String If (TypeOf inputRaster Is IRasterDataset) Then Dim rasterDataset As IRasterDataset = DirectCast(inputRaster, IRasterDataset) Return rasterDataset.CompleteName End If If (TypeOf inputRaster Is IRaster) Then Dim myRaster As IRaster = DirectCast(inputRaster, IRaster) Return DirectCast(myRaster, IRaster2).RasterDataset.CompleteName End If If TypeOf inputRaster Is IDataset Then Dim dataset As IDataset = DirectCast(inputRaster, IDataset) Return dataset.Name End If If TypeOf inputRaster Is IName Then If TypeOf inputRaster Is IDatasetName Then Dim inputDSName As IDatasetName = DirectCast(inputRaster, IDatasetName) Return inputDSName.Name End If If TypeOf inputRaster Is IFunctionRasterDatasetName Then Dim inputFRDName As IFunctionRasterDatasetName = DirectCast(inputRaster, IFunctionRasterDatasetName) Return inputFRDName.BrowseName End If If TypeOf inputRaster Is IMosaicDatasetName Then Dim inputMDName As IMosaicDatasetName = DirectCast(inputRaster, IMosaicDatasetName) Return "MD" End If Dim inputName As IName = DirectCast(inputRaster, IName) Return inputName.NameString End If If TypeOf inputRaster Is IRasterFunctionTemplate Then Dim rasterFunctionTemplate As IRasterFunctionTemplate = DirectCast(inputRaster, IRasterFunctionTemplate) Return rasterFunctionTemplate.[Function].Name End If If TypeOf inputRaster Is IRasterFunctionVariable Then Dim rasterFunctionVariable As IRasterFunctionVariable = DirectCast(inputRaster, IRasterFunctionVariable) Return rasterFunctionVariable.Name End If Return "" End Function ''' <summary> ''' Updates the UI textboxes using the properties that have been set. ''' </summary> Public Sub UpdateUI() If myInputRaster IsNot Nothing Then inputRasterTxtbox.Text = GetInputRasterName(myInputRaster) End If BandIndicesTxtBox.Text = myBandIndices End Sub Private Sub inputRasterBtn_Click(sender As Object, e As EventArgs) Dim ipSelectedObjects As IEnumGxObject = Nothing ShowRasterDatasetBrowser(CInt(Handle.ToInt32()), ipSelectedObjects) Dim selectedObject As IGxObject = ipSelectedObjects.[Next]() If TypeOf selectedObject Is IGxDataset Then Dim ipGxDS As IGxDataset = DirectCast(selectedObject, IGxDataset) Dim ipDataset As IDataset ipDataset = ipGxDS.Dataset myInputRaster = ipDataset.FullName inputRasterTxtbox.Text = GetInputRasterName(myInputRaster) myDirtyFlag = True End If End Sub Public Sub ShowRasterDatasetBrowser(handle__1 As Integer, ByRef ipSelectedObjects As IEnumGxObject) Dim ipFilterCollection As IGxObjectFilterCollection = New GxDialogClass() Dim ipFilter1 As IGxObjectFilter = New GxFilterRasterDatasetsClass() ipFilterCollection.AddFilter(ipFilter1, True) Dim ipGxDialog As IGxDialog = DirectCast(ipFilterCollection, IGxDialog) ipGxDialog.RememberLocation = True ipGxDialog.Title = "Open" ipGxDialog.AllowMultiSelect = False ipGxDialog.RememberLocation = True ipGxDialog.DoModalOpen(CInt(Handle.ToInt32()), ipSelectedObjects) Return End Sub Private Sub BandIndicesTxtBox_TextChanged(sender As Object, e As EventArgs) myBandIndices = BandIndicesTxtBox.Text myDirtyFlag = True End Sub #End Region End Class