Example #1
0
        public void LoadAnalysisObjectsByGeometry(ESRI.ArcGIS.Geodatabase.IFeatureClass
                                                  inputFeatureClass, string naClassName, ESRI.ArcGIS.NetworkAnalyst.INAContext
                                                  naContext)
        {
            // Both Initialize and Load take a cursor from the input feature class
            ESRI.ArcGIS.Geodatabase.ICursor cursor = inputFeatureClass.Search(null, false) as
                                                     ESRI.ArcGIS.Geodatabase.ICursor;

            // 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.
            ESRI.ArcGIS.NetworkAnalyst.INAClassLoader2 naClassLoader = new
                                                                       ESRI.ArcGIS.NetworkAnalyst.NAClassLoaderClass();
            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.
            ESRI.ArcGIS.NetworkAnalyst.INALocator3 naLocator3 = naClassLoader.Locator as
                                                                ESRI.ArcGIS.NetworkAnalyst.INALocator3;
            naLocator3.ExcludeRestrictedElements = 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, ref rowsIn, ref rowsLocated);
        }
Example #2
0
        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);
            }
        }