How to set up, solve, and save a network analysis problem


Summary
There are many ways to modify a network analysis problem to fit a specific application. This topic shows the simplest way to create a problem instance, solve it, then save it to disk using solvers, contexts, and layers.

Setting up, solving, and saving a network analysis layer

Do the following steps for a standard workflow:
  1. Set up your network dataset (the following code example works for a file geodatabase). Set up your network dataset slightly differently for other types of datasets. To open other types of network datasets, see How to open a network dataset.
To automate your workflow, use the ArcGIS Network Analyst extension geoprocessing tools. In this topic, use the Make Route Layer, Add Locations, and Solve tools.
[Java]
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
IFeatureWorkspace featureWorkspace = new IFeatureWorkspaceProxy
    (workspaceFactory.openFromFile(fileGDBPath, 0));
IFeatureDataset featureDataset = featureWorkspace.openFeatureDataset
    (featureDatasetName);
IFeatureDatasetExtensionContainer fdsExtCont = new
    IFeatureDatasetExtensionContainerProxy(featureDataset);
IFeatureDatasetExtension fdsExt = fdsExtCont.findExtension
    (esriDatasetType.esriDTNetworkDataset);
IDatasetContainer2 dsCont = new IDatasetContainer2Proxy(fdsExt);
IDataset dataset = dsCont.getDatasetByName(esriDatasetType.esriDTNetworkDataset,
    networkDatasetName);
NetworkDataset networkDataset = new NetworkDataset(dataset);
IDENetworkDataset deNetworkDataset = (IDENetworkDataset)((IDatasetComponent)
    networkDataset).getDataElement();
  1. Create and prepare your solver. There are many more settings for solvers than are listed here. In your code, this is the place you would tweak the solver to your specification. See the following code example:
[Java]
INASolver routeSolver = new NARouteSolver();
INASolverSettings naSolverSettings = (INASolverSettings)routeSolver;
naSolverSettings.setImpedanceAttributeName(impedanceAttributeName);
  1. Set up your context by creating it, then binding it to a network dataset. See the following code example:
[Java]
INAContext context = routeSolver.createContext(deNetworkDataset, 
    "Name Your Context Here");
INAContextEdit contextEdit = (INAContextEdit)context;
IGPMessages gpMessages = new GPMessages();
contextEdit.bind(networkDataset, gpMessages);
  1. Load your input data. In this case, you are loading stops using the input feature class and a NAClassLoader. The code assumes that your input stops are from the same workspace. For more information about how to load input, including single input loading, batch loading, and polygon and polyline loading, see How to load data into a network analysis problem. See the following code example:
[Java]
IFeatureDataset inputFeatureDataset = featureWorkspace.openFeatureDataset
    (inputFeatureDatasetName);
IFeatureClassContainer inputStopsFeatureCC = new IFeatureClassContainerProxy
    (inputFeatureDataset);
IFeatureClass inputStopsFClass = inputStopsFeatureCC.getClassByName(inputStopsFCName)
    ;
ICursor cursor = new ICursorProxy(inputStopsFClass.search(null, false));
INAClassLoader classLoader = new NAClassLoader();
classLoader.setNAClassByRef((INAClass)context.getNAClasses().getItemByName("Stops"));
classLoader.setLocatorByRef(context.getLocator());
// Prevent locations from being placed on restricted elements.
INALocator3 naLocator3 = (INALocator3)classLoader.getLocator();
naLocator3.setExcludeRestrictedElements(true);
naLocator3.cacheRestrictedElements(context);
int[] rowsInCursor = {
    0
};
int[] rowsLocated = {
    0
};
classLoader.load(cursor, null, rowsInCursor, rowsLocated);
  1. Solve the route. If you are getting an exception during the solve, put a Try-Catch around the Solve call and look at GPMessages for specific information as to the cause of the failure. You can also check GPMessages after a successful solve, to see if there are any warning or informational messages. See the following code example:
[Java]
Boolean partialSolution = routeSolver.solve(context, gpMessages, null);
  1. If you want to work with the results of the solve, obtain the FeatureClass containing the route results. Iterate over the route class features' attribute values to examine the results. See the following code example:
[Java]
IFeatureClass routesClass = (IFeatureClass)context.getNAClasses().getItemByName(
    "Routes");
  1. Save your solved route as a layer file. Be sure that your output file ends with the .lyr extension. See the following code example:
[Java]
try{
    INALayer naLayer = routeSolver.createLayer(context);
    ILayerFile layerfile = new LayerFile();
    layerfile.esri_new(outputFile);
    layerfile.replaceContents((ILayer)naLayer);
    layerfile.save();
    layerfile.close();
}

catch (Exception e){
    // Check here if there was an error when saving your layer file.
}
The following method is composed of the previous code examples:
[Java]
String fileGDBPath = 
    "C:\\ArcGIS\\DeveloperKit10.0\\Samples\\data\\SanFrancisco\\SanFrancisco.gdb";
String featureDatasetName = "Transportation";
String networkDatasetName = "Streets_ND";
String impedanceAttributeName = "TravelTime";
String inputFeatureDatasetName = "Analysis";
String inputStopsFCName = "Stores";
String outputFile = "c:\\temp\\output.lyr";

// 1. Set up your network dataset. 
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
IFeatureWorkspace featureWorkspace = new IFeatureWorkspaceProxy
    (workspaceFactory.openFromFile(fileGDBPath, 0));
IFeatureDataset featureDataset = featureWorkspace.openFeatureDataset
    (featureDatasetName);
IFeatureDatasetExtensionContainer fdsExtCont = new
    IFeatureDatasetExtensionContainerProxy(featureDataset);
IFeatureDatasetExtension fdsExt = fdsExtCont.findExtension
    (esriDatasetType.esriDTNetworkDataset);
IDatasetContainer2 dsCont = new IDatasetContainer2Proxy(fdsExt);
IDataset dataset = dsCont.getDatasetByName(esriDatasetType.esriDTNetworkDataset,
    networkDatasetName);
NetworkDataset networkDataset = new NetworkDataset(dataset);
IDENetworkDataset deNetworkDataset = (IDENetworkDataset)((IDatasetComponent)
    networkDataset).getDataElement();

// 2. Create and prepare your solver. 
INASolver routeSolver = new NARouteSolver();
INASolverSettings naSolverSettings = (INASolverSettings)routeSolver;
naSolverSettings.setImpedanceAttributeName(impedanceAttributeName);

// 3. Set up your context by creating it, then binding it to a network dataset.
INAContext context = routeSolver.createContext(deNetworkDataset, 
    "Name Your Context Here");
INAContextEdit contextEdit = (INAContextEdit)context;
IGPMessages gpMessages = new GPMessages();
contextEdit.bind(networkDataset, gpMessages);

// 4. Load your input data. 
IFeatureDataset inputFeatureDataset = featureWorkspace.openFeatureDataset
    (inputFeatureDatasetName);
IFeatureClassContainer inputStopsFeatureCC = new IFeatureClassContainerProxy
    (inputFeatureDataset);
IFeatureClass inputStopsFClass = inputStopsFeatureCC.getClassByName(inputStopsFCName)
    ;
ICursor cursor = new ICursorProxy(inputStopsFClass.search(null, false));
INAClassLoader classLoader = new NAClassLoader();
classLoader.setNAClassByRef((INAClass)context.getNAClasses().getItemByName("Stops"));
classLoader.setLocatorByRef(context.getLocator());
INALocator3 naLocator3 = (INALocator3)classLoader.getLocator();
naLocator3.setExcludeRestrictedElements(true);
naLocator3.cacheRestrictedElements(context);
int[] rowsInCursor = {
    0
};
int[] rowsLocated = {
    0
};
classLoader.load(cursor, null, rowsInCursor, rowsLocated);

// 5. Solve the route. 
Boolean partialSolution = routeSolver.solve(context, gpMessages, null);

// 6. Get the FeatureClass containing the route results. 
IFeatureClass routesClass = (IFeatureClass)context.getNAClasses().getItemByName(
    "Routes");

// 7. Save your solved route as a layer file. 
try{
    INALayer naLayer = routeSolver.createLayer(context);
    ILayerFile layerfile = new LayerFile();
    layerfile.esri_new(outputFile);
    layerfile.replaceContents((ILayer)naLayer);
    layerfile.save();
    layerfile.close();
}

catch (Exception e){
    // Check here if there was an error when saving your layer file.
}


See Also:

How to open a network dataset
How to load data into a network analysis problem
What is ArcGIS Network Analyst extension?
About the ArcGIS Network Analyst extension Tutorial
Essential ArcGIS Network Analyst extension vocabulary
NetworkAnalyst
ArcGIS Network Analyst extension Object Model Diagram




Development licensingDeployment licensing
Engine Developer KitEngine: Network Analyst
ArcGIS for Desktop Advanced: Network AnalystArcGIS for Desktop Advanced: Network Analyst
ArcGIS for Desktop Standard: Network AnalystArcGIS for Desktop Standard: Network Analyst
ArcGIS for Desktop Basic: Network AnalystArcGIS for Desktop Basic: Network Analyst