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.

NAClassLoader

If your input data is stored in a feature class, use NAClassLoader (LoadFromAFeatureClass method). If your input is a single geometry, add that geometry to an in-memory feature class (or a feature class stored in a geodatabase), then use the following code example to load your locations.

The class loader is used instead of manually creating features and setting values because the loader handles the 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 correctly populates this field 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 example:
Use the Add Locations geoprocessing tool to load data into a network analysis problem.
[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),
    // do not exclude the restricted elements. Also, if you do have barriers in your analysis problem,
    // 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 following field names 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);
}
When loading into the Stops class of a Route analysis, set a valid Sequence value for the analysis to solve correctly.






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