How to load network locations based on network fields (not geometry) (.NET/C#)

471
0
10-10-2013 11:37 AM
deleted-user-Ohz6rwd1kavx
New Contributor III
Network Analyst, C# ArcObjects

I would like to load network features (e.g. facilities, point barriers).

I cannot get 'load using network fields' to work (I can load based on geometry, but this isn't what I want).

I am using the exact code from (below) from this .NET help topic. I am using 10.2.

How to load data into a network analysis problem
http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/0001/00010000023q000000.htm

rowsLocated is always coming back as 0. rowsIn shows that there are indeed rows in my input cursor. I have tried on a couple feature classes, but always the same result.

Any ideas what could cause this to not work? I can load from the same feature classes using ArcMap. Works fine.

I'm not doing anything with GP

Thanks,

-Cory

public void LoadAnalysisObjectsByField(ESRI.ArcGIS.Geodatabase.ITable inputClass,
    string naClassName, ESRI.ArcGIS.NetworkAnalyst.INAContext naContext)
{
    // Both Initialize and Load take a cursor from the input class
    ESRI.ArcGIS.Geodatabase.ICursor cursor = inputClass.Search(null, false)as
        ESRI.ArcGIS.Geodatabase.ICursor;
    ESRI.ArcGIS.NetworkAnalyst.INAClassLoader2 naClassLoader = new
        ESRI.ArcGIS.NetworkAnalyst.NAClassLoaderClass();
    naClassLoader.Initialize(naContext, naClassName, cursor);

    // Store the current set of locator agents, so they can be added back later
    int agentCount = naContext.Locator.LocatorAgentCount;
    var listOfAgents = new System.Collections.Generic.List <
        ESRI.ArcGIS.NetworkAnalyst.INALocatorAgent > ();
    for (int locIndex = 0; locIndex < agentCount; locIndex++)
        listOfAgents.Add(naContext.Locator.get_LocatorAgent(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.Locator.RemoveLocatorAgent(locIndex);

    // Create and add a fields agent
    var fieldsAgent = new
        ESRI.ArcGIS.NetworkAnalyst.NALocatorLocationFieldsAgentClass()as
        ESRI.ArcGIS.NetworkAnalyst.INALocatorLocationFieldsAgent2;

    // Set the field names appropriately based on input data and NAClass
    var naClass = naContext.NAClasses.get_ItemByName(naClassName)as
        ESRI.ArcGIS.NetworkAnalyst.INAClass;
    var naFeatureClass = naClass as ESRI.ArcGIS.Geodatabase.IFeatureClass;

    // Check to see if the NAClass is of type NALocation or NALocationRanges
    ESRI.ArcGIS.esriSystem.UID naLocationFeatureUID = new
        ESRI.ArcGIS.esriSystem.UIDClass();
    naLocationFeatureUID.Value = "esriNetworkAnalyst.NALocationFeature";
    ESRI.ArcGIS.esriSystem.UID naLocationFeatureRangesUID = new
        ESRI.ArcGIS.esriSystem.UIDClass();
    naLocationFeatureRangesUID.Value = "esriNetworkAnalyst.NALocationRangesFeature";
    if (naFeatureClass.CLSID.Compare(naLocationFeatureUID))
    {
        // The field names listed below are the names used in ArcGIS Network Analyst extension classes to represent NALocations.
        //  These are also the names of fields added by the CalculateLocations geoprocessing tool
        fieldsAgent.OIDFieldName = "SourceOID";
        fieldsAgent.SourceIDFieldName = "SourceID";
        fieldsAgent.PositionFieldName = "PosAlong";
        fieldsAgent.SideFieldName = "SideOfEdge";
    }
    else if (naFeatureClass.CLSID.Compare(naLocationFeatureRangesUID))
    {
        // The location ranges input field must be of type BLOB
        fieldsAgent.LocationRangesFieldName = "Locations";
        var blobField = inputClass.Fields.get_Field(inputClass.FindField
            (fieldsAgent.LocationRangesFieldName));
        if (blobField.Type !=
            ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob)
        {
            System.Windows.Forms.MessageBox.Show(
                "Loading location ranges by field requires a blob field");
            return ;
        }
    }
    naContext.Locator.AddLocatorAgent(fieldsAgent as
        ESRI.ArcGIS.NetworkAnalyst.INALocatorAgent);

    // 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, ref rowsIn, ref rowsLocated);

    // Now remove the custom fields agent and add back the stored agents
    naContext.Locator.RemoveLocatorAgent(0);
    foreach (var agent in listOfAgents)
        naContext.Locator.AddLocatorAgent(agent);
}
0 Kudos
0 Replies