This document is archived and information here might be outdated.  Recommended version.


Create OD Cost Matrix Layer Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Create OD Cost Matrix Layer Snippet

Create a new OD cost matrix layer.

[C#]
///<summary>Create a new OD cost matrix layer.</summary>
///  
///<param name="networkDataset">An INetworkDataset interface that is the network dataset on which to perform the OD cost matrix analysis.</param>
///  
///<returns>An INALayer3 interface that is the newly created network analysis layer.</returns>
public static ESRI.ArcGIS.NetworkAnalyst.INALayer3 CreateODCostMatrixLayer(ESRI.ArcGIS.Geodatabase.INetworkDataset networkDataset)
{
  ESRI.ArcGIS.NetworkAnalyst.INAODCostMatrixSolver naAODCostMatrixSolver=new ESRI.ArcGIS.NetworkAnalyst.NAODCostMatrixSolverClass();
  ESRI.ArcGIS.NetworkAnalyst.INASolver naSolver=naAODCostMatrixSolver as ESRI.ArcGIS.NetworkAnalyst.INASolver;

  ESRI.ArcGIS.Geodatabase.IDatasetComponent datasetComponent=networkDataset as ESRI.ArcGIS.Geodatabase.IDatasetComponent; // Dynamic Cast
  ESRI.ArcGIS.Geodatabase.IDENetworkDataset deNetworkDataset=datasetComponent.DataElement as ESRI.ArcGIS.Geodatabase.IDENetworkDataset; // Dynamic Cast
  ESRI.ArcGIS.NetworkAnalyst.INAContext naContext=naSolver.CreateContext(deNetworkDataset, naSolver.Name);
  ESRI.ArcGIS.NetworkAnalyst.INAContextEdit naContextEdit=naContext as ESRI.ArcGIS.NetworkAnalyst.INAContextEdit; // Dynamic Cast

  ESRI.ArcGIS.Geodatabase.IGPMessages gpMessages=new ESRI.ArcGIS.Geodatabase.GPMessagesClass();
  naContextEdit.Bind(networkDataset, gpMessages);

  ESRI.ArcGIS.NetworkAnalyst.INALayer naLayer=naSolver.CreateLayer(naContext);
  ESRI.ArcGIS.NetworkAnalyst.INALayer3 naLayer3=naLayer as ESRI.ArcGIS.NetworkAnalyst.INALayer3;

  return naLayer3;
}
[Visual Basic .NET]
'''<summary>Create a new OD cost matrix layer.</summary>
'''  
'''<param name="networkDataset">An INetworkDataset interface that is the network dataset on which to perform the OD cost matrix analysis.</param>
'''  
'''<returns>An INALayer3 interface that is the newly created network analysis layer.</returns>
Public Shared Function CreateODCostMatrixLayer(ByVal networkDataset As ESRI.ArcGIS.Geodatabase.INetworkDataset) As ESRI.ArcGIS.NetworkAnalyst.INALayer3

  Dim naAODCostMatrixSolver As ESRI.ArcGIS.NetworkAnalyst.INAODCostMatrixSolver=New ESRI.ArcGIS.NetworkAnalyst.NAODCostMatrixSolverClass
  Dim naSolver As ESRI.ArcGIS.NetworkAnalyst.INASolver=CType(naAODCostMatrixSolver, ESRI.ArcGIS.NetworkAnalyst.INASolver) ' Explicit Cast

  Dim datasetComponent As ESRI.ArcGIS.Geodatabase.IDatasetComponent=CType(networkDataset, ESRI.ArcGIS.Geodatabase.IDatasetComponent) ' Explicit Cast
  Dim deNetworkDataset As ESRI.ArcGIS.Geodatabase.IDENetworkDataset=CType(datasetComponent.DataElement, ESRI.ArcGIS.Geodatabase.IDENetworkDataset) ' Explicit Cast
  Dim naContext As ESRI.ArcGIS.NetworkAnalyst.INAContext=naSolver.CreateContext(deNetworkDataset, naSolver.Name)
  Dim naContextEdit As ESRI.ArcGIS.NetworkAnalyst.INAContextEdit=CType(naContext, ESRI.ArcGIS.NetworkAnalyst.INAContextEdit) ' Explicit Cast

  Dim gpMessages As ESRI.ArcGIS.Geodatabase.IGPMessages=New ESRI.ArcGIS.Geodatabase.GPMessagesClass
  naContextEdit.Bind(networkDataset, gpMessages)

  Dim naLayer As ESRI.ArcGIS.NetworkAnalyst.INALayer=naSolver.CreateLayer(naContext)
  Dim naLayer3 As ESRI.ArcGIS.NetworkAnalyst.INALayer3=CType(naLayer, ESRI.ArcGIS.NetworkAnalyst.INALayer3) ' Explicit Cast

  Return naLayer3

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.NetworkAnalyst
  • ESRI.ArcGIS.System