How to load data into a network analysis problem


Summary This topic describes how to load data into a network analysis problem in a stand-alone application using ArcObjects.

The recommended way to load data into a network analysis problem is using the Add Locations geoprocessing tool.

In this topic

NAClassLoader

NAClassLoader

If your input data is stored in a feature class, use NAClassLoader (LoadFromAFeatureClass method). If your input is a single geometry, then add that geometry to an in-memory feature class (or an feature class stored in a geodatabase).  Then, use the following code to load your locations.
The reason the class loader is used instead of manually creating features and setting values is that the loader takes care of details that could otherwise be missed.  For example, polygon and polyline barrier features are not defined by the feature geometry, but by the values stored in the Locations field.  The class loader will populate this field correctly for you.
Pass the feature class containing the input data, the name of the destination network analysis class (NAClass), and the destination network analysis context (NAContext) as shown in the following code:
[Java]
public void loadAnalysisObjectsByGeometry(IFeatureClass inputFeatureClass, String
    naClassName, INAContext naContext)throws AutomationException, IOException{
    // Both Initialize and Load take a cursor from the input feature class
    ICursor cursor = new ICursorProxy(inputFeatureClass.search(null, false));
    // Initialize the default field mappings.
    // If you want to specify field mappings beyond the default ones, use naClassLoader.FieldMap to retrieve
    // and edit the mappings between the input class and the naclass.
    INAClassLoader2 naClassLoader = new NAClassLoader();
    naClassLoader.initialize(naContext, naClassName, cursor);
    // Use ExcludeRestrictedElements and CacheRestrictedElements to prevent locations from being placed on restricted elements.
    // Some ways to restrict elements include restriction barriers and restriction attributes.
    // If you are loading barriers into barrier classes, or not loading locations (for example, seedpoints)
    // then you should not exclude the restricted elements. Also, if you do have barriers in your analysis problem,
    // then you should load those first, to make sure the restricted elements are established before loading 
    // non-barrier classes.
    INALocator3 naLocator3 = (INALocator3)naClassLoader.getLocator();
    naLocator3.setExcludeRestrictedElements(true);
    naLocator3.cacheRestrictedElements(naContext);
    // After Loading is complete, the rowsIn and rowsLocated variable can be used to verify
    // that every row from the input feature class has been loaded into the network analysis class
    int[] rowsIn = {
        0
    };
    int[] rowsLocated = {
        0
    };
    naClassLoader.load(cursor, null, rowsIn, rowsLocated);
}
[Java]
public void loadAnalysisObjectsByField(ITable inputClass, String naClassName,
    INAContext naContext)throws AutomationException, IOException{
    // Both Initialize and Load take a cursor from the input class
    ICursor cursor = new ICursorProxy(inputClass.ITable_search(null, false));
    INAClassLoader2 naClassLoader = new NAClassLoader();
    naClassLoader.initialize(naContext, naClassName, cursor);
    // Store the current set of locator agents, so they can be added back
    // later
    int agentCount = naContext.getLocator().getLocatorAgentCount();
    LinkedList < INALocatorAgent > listOfAgents = new LinkedList < INALocatorAgent >
        ();
    for (int locIndex = 0; locIndex < agentCount; locIndex++)
        listOfAgents.add(naContext.getLocator().getLocatorAgent(locIndex));
    // Remove the existing locator agents from the locator
    // This for loop is done in reverse order, because agents are being
    // removed as the loop executes
    for (int locIndex = agentCount - 1; locIndex >= 0; locIndex--)
        naContext.getLocator().removeLocatorAgent(locIndex);
    // Create and add a fields agent
    INALocatorLocationFieldsAgent2 fieldsAgent = new NALocatorLocationFieldsAgent();
    // Set the field names appropriately based on input data and NAClass
    INAClass naClass = (INAClass)naContext.getNAClasses().getItemByName(naClassName);
    IFeatureClass naFeatureClass = (IFeatureClass)naClass;
    // Check to see if the NAClass is of type NALocation or NALocationRanges
    UID naLocationFeatureUID = new UID();
    naLocationFeatureUID.setValue("esriNetworkAnalyst.NALocationFeature");
    UID naLocationFeatureRangesUID = new UID();
    naLocationFeatureRangesUID .setValue(
        "esriNetworkAnalyst.NALocationRangesFeature");
    if (naFeatureClass.getCLSID().compare(naLocationFeatureUID)){
        // The field names listed below are the names used in Network
        // Analyst classes to represent NALocations.
        // These are also the names of fields added by the
        // CalculateLocations geoprocessing tool
        fieldsAgent.setOIDFieldName("SourceOID");
        fieldsAgent.setSourceIDFieldName("SourceID");
        fieldsAgent.setPositionFieldName("PosAlong");
        fieldsAgent.setSideFieldName("SideOfEdge");
    }
    else if (naFeatureClass.getCLSID().compare(naLocationFeatureRangesUID)){
        // The location ranges input field must be of type BLOB
        fieldsAgent.setLocationRangesFieldName("Locations");
        IField blobField = inputClass.getFields().getField(inputClass.findField
            (fieldsAgent .getLocationRangesFieldName()));
        if (blobField.getType() != esriFieldType.esriFieldTypeBlob){
            System.err .println(
                "Loading location ranges by field requires a blob field");
            return ;
        }
    }
    naContext.getLocator().addLocatorAgent((INALocatorAgent)fieldsAgent);
    // After Loading is complete, the rowsIn and rowsLocated variable can be
    // used to verify
    // that every row from the input feature class has been loaded into the
    // network analysis class
    int[] rowsIn = {
        0
    };
    int[] rowsLocated = {
        0
    };
    naClassLoader.load(cursor, null, rowsIn, rowsLocated);
    // Now remove the custom fields agent and add back the stored agents
    naContext.getLocator().removeLocatorAgent(0);
    for (INALocatorAgent agent: listOfAgents)
        naContext.getLocator().addLocatorAgent(agent);
}
Note: When loading into the Stops class of a Route analysis, you must set a valid Sequence value in order for the analysis to solve correctly.






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