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); }
public void LoadAnalysisObjectsByField(ESRI.ArcGIS.Geodatabase.ITable inputClass, string naClassName, ESRI.ArcGIS.NetworkAnalyst.INAContext naContext) { // 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 Network Analyst 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; } } // Bind for a fields agent will properly associate the agent with the network dataset. // This is useful internally for mapping SourceID back to network sources. ((INALocatorAgent)fieldsAgent).Bind(naContext.NetworkDataset, null); var posFieldsLocator = new ESRI.ArcGIS.NetworkAnalyst.NALocatorClass() as INALocator; posFieldsLocator.FindClosestAmongAllAgents = false; posFieldsLocator.SnapToleranceUnits = ESRI.ArcGIS.esriSystem.esriUnits.esriMeters; posFieldsLocator.SnapTolerance = 0.0; // Bind to network dataset is required. Bind has a side effect of adding feature locator agents for all // feature sources. In this case we only want a field locator agent so remove the feature agents after Bind // and before adding the field agent to NALocator. posFieldsLocator.Bind(naContext.NetworkDataset, null); int locatorAgentCount = posFieldsLocator.LocatorAgentCount; for (int i = locatorAgentCount - 1; i >= 0; --i) posFieldsLocator.RemoveLocatorAgent(i); posFieldsLocator.AddLocatorAgent((INALocatorAgent)fieldsAgent); ((INAContextEdit)naContext).Locator = posFieldsLocator; // 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); // 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 reset the locator naContext.Locator.RemoveLocatorAgent(0); naContext.Locator.Bind(naContext.NetworkDataset, null); }