How to setup, 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, and save it to disk using solvers, contexts, and layers.

To automate your workflow, use the Network Analyst geoprocessing tools. For this topic, use the Make Route Layer, Add Locations, and Solve tools.

Setting up, solving, and saving a network analysis layer

The following documents a standard workflow:
Set up your network dataset. This code works for a File Geodatabase. You will need to 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:
[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();
2. Create and prepare your solver.  There are many more settings for solvers than are listed here.  In your own code, this is the place you would tweak the solver to your specification.
[Java]
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.
[Java]
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.  In this case, we are loading stops using the input feature class and a NAClassLoader. This code assumes that your input stops are from the same workspace.  To find more 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.
[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);
5. Solve the route. If you are getting an exception during the solve, put a Try-Catch around the Solve call and look at the GPMessages for specific information as to the cause of the failure.  You can also check the GPMessages after a successful solve to see if there are any warning or informational messages.
[Java]
Boolean partialSolution = routeSolver.solve(context, gpMessages, null);
6. 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.
[Java]
IFeatureClass routesClass = (IFeatureClass)context.getNAClasses().getItemByName(
    "Routes");
7. Save your solved route as a layer file.  Be sure that your output file ends with the extension .lyr.
[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 fragments:
[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 Network Analyst?
About the Network Analyst Tutorial
Essential Network Analyst vocabulary
NetworkAnalyst
Network Analyst Object Model Diagram




Development licensing Deployment licensing
Engine Developer Kit Engine Runtime: Network
ArcInfo: Network Analyst ArcInfo: Network Analyst
ArcEditor: Network Analyst ArcEditor: Network Analyst
ArcView: Network Analyst ArcView: Network Analyst