예제 #1
0
        private void Initialize()
        {
            this.CheckOutNetworkAnalystExtension();
            IFeatureWorkspace workspace =
                this.OpenWorkspace(Application.StartupPath + @"\..\..\..\..\Data\NetworkAnalyst") as IFeatureWorkspace;
            INetworkDataset pNetDataset = this.OpenNetworkDataset(workspace as IWorkspace, "Streets_nd");

            this.m_pNAContext = this.CreateSolverContext(pNetDataset);
            for (int i = 0; i < (pNetDataset.AttributeCount - 1); i++)
            {
                INetworkAttribute attribute = pNetDataset.get_Attribute(i);
                if (attribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                    this.cboCostAttribute.Items.Add(attribute.Name);
                    this.cboCostAttribute.SelectedIndex = 0;
                }
            }
            this.txtTargetFacility.Text = "1";
            this.txtCutOff.Text         = "";
            IFeatureClass pInputFC = workspace.OpenFeatureClass("BayAreaIncident");

            this.LoadNANetworkLocations("Incidents", pInputFC, 100.0);
            pInputFC = workspace.OpenFeatureClass("BayAreaLocations");
            this.LoadNANetworkLocations("Facilities", pInputFC, 100.0);
            INetworkLayer layer2 = new NetworkLayerClass
            {
                NetworkDataset = pNetDataset
            };
            ILayer layer = layer2 as ILayer;

            layer.Name = "Network Dataset";
            layer      = this.m_pNAContext.Solver.CreateLayer(this.m_pNAContext) as ILayer;
            layer.Name = this.m_pNAContext.Solver.DisplayName;
        }
예제 #2
0
        public static void GetPermittedAttributes(INALayer layer, out string[] impedences, out string[] restrictions,
                                                  out string[] hierarchies)
        {
            ArrayList       list           = new ArrayList();
            ArrayList       list2          = new ArrayList();
            ArrayList       list3          = new ArrayList();
            INetworkDataset networkDataset = layer.Context.NetworkDataset;

            for (int i = 0; i < networkDataset.AttributeCount; i++)
            {
                INetworkAttribute attribute = networkDataset.get_Attribute(i);
                if (attribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                    list.Add(attribute.Name);
                }
                else if (attribute.UsageType == esriNetworkAttributeUsageType.esriNAUTRestriction)
                {
                    list2.Add(attribute.Name);
                }
                else if (attribute.UsageType == esriNetworkAttributeUsageType.esriNAUTHierarchy)
                {
                    list3.Add(attribute.Name);
                }
            }
            impedences   = (string[])list.ToArray(typeof(string));
            restrictions = (string[])list2.ToArray(typeof(string));
            hierarchies  = (string[])list3.ToArray(typeof(string));
        }
예제 #3
0
        /// <summary>
        /// Update the CheckedListBox control based on the network dataset attributes (checking the ones currently chosen by the solver)
        /// </summary>
        private void PopulateAttributeControl(CheckedListBox chklstBox, INetworkDataset networkDataset, IStringArray strArray, esriNetworkAttributeUsageType usageType)
        {
            chklstBox.Items.Clear();

            //  Loop through the network dataset attributes
            for (int i = 0; i < networkDataset.AttributeCount; i++)
            {
                INetworkAttribute networkAttribute = networkDataset.get_Attribute(i);
                if (networkAttribute.UsageType == usageType)
                {
                    string     attributeName = networkAttribute.Name;
                    CheckState checkState    = CheckState.Unchecked;

                    // If the attribute is in the strArray, it should be checked
                    for (int j = 0; j < strArray.Count; j++)
                    {
                        if (strArray.get_Element(j) == attributeName)
                        {
                            checkState = CheckState.Checked;
                        }
                    }

                    // Add the attribute to the control
                    chklstBox.Items.Add(attributeName, checkState);
                }
            }
        }
        /// <summary>
		/// Initialize the solver by calling the ArcGIS Network Analyst extension functions.
		/// </summary>
		private void Initialize()
		{
			IFeatureWorkspace featureWorkspace = null;
			INetworkDataset networkDataset = null;
			try
			{
				// Open the Network Dataset
				IWorkspace workspace = OpenWorkspace(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"ArcGIS\data\SanFrancisco\SanFrancisco.gdb"));
                networkDataset = OpenNetworkDataset(workspace, "Transportation", "Streets_ND");
				featureWorkspace = workspace as IFeatureWorkspace;
			}
			catch (Exception ex)
			{
				System.Windows.Forms.MessageBox.Show("Unable to open dataset. Error Message: " + ex.Message);
				this.Close();
				return;
			}

			// Create NAContext and NASolver
			m_NAContext = CreateSolverContext(networkDataset);

			// Get available cost attributes from the network dataset
			INetworkAttribute networkAttribute;
			for (int i = 0; i < networkDataset.AttributeCount - 1; i++)
			{
				networkAttribute = networkDataset.get_Attribute(i);
				if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
				{
					comboCostAttribute.Items.Add(networkAttribute.Name);
				}
			}
			comboCostAttribute.SelectedIndex = 0;

			textTargetFacility.Text = "";
			textCutoff.Text = "";

			// Load locations from feature class
			IFeatureClass inputFClass = featureWorkspace.OpenFeatureClass("Stores");
			LoadNANetworkLocations("Origins", inputFClass, 100);
			inputFClass = featureWorkspace.OpenFeatureClass("Hospitals");
			LoadNANetworkLocations("Destinations", inputFClass, 5100);

			// Create layer for network dataset and add to ArcMap
			INetworkLayer networkLayer = new NetworkLayerClass();
			networkLayer.NetworkDataset = networkDataset;
			var layer = networkLayer as ILayer;
			layer.Name = "Network Dataset";
			axMapControl.AddLayer(layer, 0);

			// Create a network analysis layer and add to ArcMap
			INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);
			layer = naLayer as ILayer;
			layer.Name = m_NAContext.Solver.DisplayName;
			axMapControl.AddLayer(layer, 0);
		}
예제 #5
0
        /// <summary>
        /// Update the Impedance control based on the network dataset cost attributes
        /// </summary>
        private void PopulateImpedanceNameControl(ComboBox cboImpedance, INetworkDataset networkDataset, string impedanceName)
        {
            cboImpedance.Items.Clear();

            for (int i = 0; i < networkDataset.AttributeCount; i++)
            {
                INetworkAttribute networkAttribute = networkDataset.get_Attribute(i);
                if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                    cboImpedance.Items.Add(networkAttribute.Name);
                }
            }

            if (cboImpedance.Items.Count > 0)
            {
                cboImpedance.Text = impedanceName;
            }
        }
        public static bool ParameterExists(INetworkDataset nds, string searchName, VarType vt)
        {
            bool found = false;

            INetworkAttribute2         netAttribute;
            IArray                     netParams;
            INetworkAttributeParameter netParam;

            int cAttributes = nds.AttributeCount;

            for (int a = 0; a < cAttributes; ++a)
            {
                netAttribute = nds.get_Attribute(a) as INetworkAttribute2;
                netParams    = null;
                int cParams = 0;
                if (netAttribute != null)
                {
                    netParams = netAttribute.Parameters;
                }

                if (netParams != null)
                {
                    cParams = netParams.Count;
                }

                string compareName;
                for (int p = 0; p < cParams; ++p)
                {
                    netParam    = netParams.get_Element(p) as INetworkAttributeParameter;
                    compareName = netParam.Name;
                    if (String.Compare(searchName, compareName, true) == 0)
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }

            return(found);
        }
        /// <summary>
        /// Find and load the cost attributes into a combo box
        /// <summary>
        private void LoadCostAttributes(INetworkDataset networkDataset)
        {
            cbCostAttribute.Items.Clear();

            int attrCount = networkDataset.AttributeCount;

            for (int attrIndex = 0; attrIndex < attrCount; attrIndex++)
            {
                INetworkAttribute networkAttribute = networkDataset.get_Attribute(attrIndex);
                if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                    cbCostAttribute.Items.Add(networkAttribute.Name);
                }
            }

            if (cbCostAttribute.Items.Count > 0)
            {
                cbCostAttribute.SelectedIndex = 0;
            }
        }
예제 #8
0
        /// <summary>
        /// Initialize the solver by calling the ArcGIS Network Analyst extension functions.
        /// </summary>
        public void Initialize(string workspacePath, string dsName)
        {
            this.Attribute = "minutes";

            IFeatureWorkspace featureWorkspace = null;

            try
            {
                // Open Geodatabase and network dataset
                IWorkspace workspace = null;// OpenGDBWorkspace(Application.StartupPath + @"\..\..\..\..\..\Data\SanFrancisco\SanFrancisco.gdb");
                //networkDataset = OpenNetworkDataset(workspace, "Transportation", "Streets_ND");

                workspace = WorkspaceUtil.OpenShapeWorkspace(workspacePath) as IWorkspace;

                _networkDataset = ShapefileToNetwork(workspace, dsName);

                featureWorkspace = workspace as IFeatureWorkspace;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Unable to open dataset. Error Message: " + ex.Message);
                return;
            }

            // Create NAContext and NASolver
            CreateSolverContext(_networkDataset);

            // Get available cost attributes from the network dataset
            INetworkAttribute networkAttribute;

            for (int i = 0; i < _networkDataset.AttributeCount; i++)
            {
                networkAttribute = _networkDataset.get_Attribute(i);
                if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                }
            }


            /*
             *
             * // Load incidents from a feature class
             * featureWorkspace = OpenShapeWorkspace(@"F:\17\private\Disaster\震后基本应急物资与设备供应计算程序") as IFeatureWorkspace;
             * //IFeatureClass inputFClass = featureWorkspace.OpenFeatureClass("Stores");
             * IFeatureClass inputFClass = featureWorkspace.OpenFeatureClass("灾区位置分布点");
             *
             * LoadNANetworkLocations("Incidents", inputFClass, 500);
             *
             * // Load facilities from a feature class
             * //inputFClass = featureWorkspace.OpenFeatureClass("FireStations");
             * inputFClass = featureWorkspace.OpenFeatureClass("物资贮备分布点");
             * LoadNANetworkLocations("Facilities", inputFClass, 500);
             *
             *
             * //Create Layer for Network Dataset and add to ArcMap
             * INetworkLayer networkLayer = new NetworkLayerClass();
             * networkLayer.NetworkDataset = _networkDataset;
             * var layer = networkLayer as ILayer;
             * layer.Name = "Network Dataset";
             * // axMapControl.AddLayer(layer, 0);
             *
             * //Create a Network Analysis Layer and add to ArcMap
             * INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);
             * layer = naLayer as ILayer;
             * layer.Name = m_NAContext.Solver.DisplayName;
             * //axMapControl.AddLayer(layer, 0);
             */
        }
        /// <summary>
        /// Find and load the cost attributes into a combo box
        /// <summary>
        private void LoadCostAttributes(INetworkDataset networkDataset)
		{
			cbCostAttribute.Items.Clear();

			int attrCount = networkDataset.AttributeCount;
			for (int attrIndex = 0; attrIndex < attrCount; attrIndex++)
			{
				INetworkAttribute networkAttribute = networkDataset.get_Attribute(attrIndex);
				if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
					cbCostAttribute.Items.Add(networkAttribute.Name);
			}

			if (cbCostAttribute.Items.Count > 0)
				cbCostAttribute.SelectedIndex = 0;
		}
        //*********************************************************************************
        // Initialize the form, create a NA context, load some locations and draw the map
        //*********************************************************************************
        private void Initialize()
        {
            // Open Geodatabase and network dataset
            IFeatureWorkspace featureWorkspace = OpenWorkspace(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"ArcGIS\data\SanFrancisco\SanFrancisco.gdb")) as IFeatureWorkspace;
            INetworkDataset   networkDataset   = OpenNetworkDataset(featureWorkspace as IWorkspace, "Transportation", "Streets_ND");

            // Create NAContext and NASolver
            m_NAContext = CreateSolverContext(networkDataset);

            // Get Cost Attributes and populate the combo drop down box
            INetworkAttribute networkAttribute;

            for (int i = 0; i < networkDataset.AttributeCount - 1; i++)
            {
                networkAttribute = networkDataset.get_Attribute(i);
                if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                    cboCostAttribute.Items.Add(networkAttribute.Name);
                    cboCostAttribute.SelectedIndex = 0;
                }
            }

            // Set the default number of facilities to solve for
            txtFacilitiesToLocate.Text = "1";

            // Set up the no cutoff for the Minimize Impedance case.
            // See the cboProblemType_SelectedIndexChanged routine for how this is managed for other problem types
            txtCutOff.Text = "<None>";

            // Populate combo box with Location-Allocation problem types
            cboProblemType.Items.Add("Minimize Impedance");
            cboProblemType.Items.Add("Maximize Coverage");
            cboProblemType.Items.Add("Maximize Capacitated Coverage");
            cboProblemType.Items.Add("Minimize Facilities");
            cboProblemType.Items.Add("Maximize Attendance");
            cboProblemType.Items.Add("Maximize Market Share");
            cboProblemType.Items.Add("Target Market Share");
            cboProblemType.Text = "Minimize Impedance";
            m_ProblemType       = "Minimize Impedance";

            // Populate combo box with Impedance Transformation choices
            cboImpTransformation.Items.Add("Linear");
            cboImpTransformation.Items.Add("Power");
            cboImpTransformation.Items.Add("Exponential");
            cboImpTransformation.Text = "Linear";

            // Set the default impedance transformation parameter
            txtImpParameter.Text = "1.0";

            // Set up the default percentage for the Target Market Share problem type
            txtTargetMarketShare.Text = "10.0";

            // Set up the default capacity
            txtDefaultCapacity.Text = "1.0";

            // Load facility locations from feature class
            IFeatureClass inputFClass = featureWorkspace.OpenFeatureClass("CandidateStores");

            LoadNANetworkLocations("Facilities", inputFClass, 500);

            // Load demand point locations from feature class
            inputFClass = featureWorkspace.OpenFeatureClass("TractCentroids");
            LoadNANetworkLocations("DemandPoints", inputFClass, 500);

            // Create Layer for Network Dataset and add to Ax Map Control
            ILayer        layer;
            INetworkLayer networkLayer;

            networkLayer = new NetworkLayerClass();
            networkLayer.NetworkDataset = networkDataset;
            layer      = networkLayer as ILayer;
            layer.Name = "Network Dataset";
            axMapControl.AddLayer(layer, 0);

            // Create a Network Analysis Layer and add to Ax Map Control
            INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);

            layer      = naLayer as ILayer;
            layer.Name = m_NAContext.Solver.DisplayName;
            axMapControl.AddLayer(layer, 0);
        }
예제 #11
0
        /// <summary>
        /// Initialize the solver by calling the ArcGIS Network Analyst extension functions.
        /// </summary>
        private void Initialize()
        {
            IFeatureWorkspace featureWorkspace = null;
            INetworkDataset   networkDataset   = null;

            try
            {
                // Open Geodatabase and network dataset
                IWorkspace workspace = OpenWorkspace(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"ArcGIS\data\SanFrancisco\SanFrancisco.gdb"));
                networkDataset   = OpenNetworkDataset(workspace, "Transportation", "Streets_ND");
                featureWorkspace = workspace as IFeatureWorkspace;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Unable to open dataset. Error Message: " + ex.Message);
                this.Close();
                return;
            }

            // Create NAContext and NASolver
            m_NAContext = CreateSolverContext(networkDataset);

            // Get available cost attributes from the network dataset
            INetworkAttribute networkAttribute;

            for (int i = 0; i < networkDataset.AttributeCount; i++)
            {
                networkAttribute = networkDataset.get_Attribute(i);
                if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                    string unitType = GetAttributeUnitType(networkAttribute.Units);
                    if (unitType == "Time")
                    {
                        comboTimeAttribute.Items.Add(networkAttribute.Name);
                    }
                    else if (unitType == "Distance")
                    {
                        comboDistanceAttribute.Items.Add(networkAttribute.Name);
                    }
                }
            }
            comboTimeAttribute.SelectedIndex     = 0;
            comboDistanceAttribute.SelectedIndex = 0;

            // Populate time field unit in comboBox
            m_unitTimeList = new System.Collections.Hashtable();
            m_unitTimeList.Add("Seconds", esriNetworkAttributeUnits.esriNAUSeconds);
            m_unitTimeList.Add("Minutes", esriNetworkAttributeUnits.esriNAUMinutes);
            foreach (System.Collections.DictionaryEntry timeUnit in m_unitTimeList)
            {
                comboTimeUnits.Items.Add(timeUnit.Key.ToString());
            }
            comboTimeUnits.SelectedIndex = 1;

            // Populate distance field unit in comboBox
            m_unitDistList = new System.Collections.Hashtable();
            m_unitDistList.Add("Miles", esriNetworkAttributeUnits.esriNAUMiles);
            m_unitDistList.Add("Meters", esriNetworkAttributeUnits.esriNAUMeters);
            foreach (System.Collections.DictionaryEntry distUnit in m_unitDistList)
            {
                comboDistUnits.Items.Add(distUnit.Key.ToString());
            }
            comboDistUnits.SelectedIndex = 0;

            // Populate time window importance attribute in comboBox
            comboTWImportance.Items.Add("High");
            comboTWImportance.Items.Add("Medium");
            comboTWImportance.Items.Add("Low");
            comboTWImportance.SelectedIndex = 0;

            // Load locations
            IFeatureClass inputFClass = featureWorkspace.OpenFeatureClass("Stores");

            LoadNANetworkLocations("Orders", inputFClass as ITable);
            inputFClass = featureWorkspace.OpenFeatureClass("DistributionCenter");
            LoadNANetworkLocations("Depots", inputFClass as ITable);
            inputFClass = featureWorkspace.OpenFeatureClass("Routes");
            LoadNANetworkLocations("Routes", inputFClass as ITable);
            ITable inputTable = featureWorkspace.OpenTable("Breaks");

            LoadNANetworkLocations("Breaks", inputTable);

            // Create layer for network dataset and add to ArcMap
            INetworkLayer networkLayer = new NetworkLayerClass();

            networkLayer.NetworkDataset = networkDataset;
            ILayer layer = networkLayer as ILayer;

            layer.Name = "Network Dataset";
            axMapControl.AddLayer(layer, 0);

            // Create a network analysis layer and add to ArcMap
            INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);

            layer      = naLayer as ILayer;
            layer.Name = m_NAContext.Solver.DisplayName;
            axMapControl.AddLayer(layer, 0);
        }
		public static bool ParameterExists(INetworkDataset nds, string searchName, VarType vt)
		{
			bool found = false;

			INetworkAttribute2 netAttribute;
			IArray netParams;
			INetworkAttributeParameter netParam;

			int cAttributes = nds.AttributeCount;
			for (int a = 0; a < cAttributes; ++a)
			{
				netAttribute = nds.get_Attribute(a) as INetworkAttribute2;
				netParams = null;
				int cParams = 0;
				if (netAttribute != null)
					netParams = netAttribute.Parameters;

				if (netParams != null)
					cParams = netParams.Count;

				string compareName;
				for (int p = 0; p < cParams; ++p)
				{
					netParam = netParams.get_Element(p) as INetworkAttributeParameter;
					compareName = netParam.Name;
					if (String.Compare(searchName, compareName, true) == 0)
					{
						found = true;
						break;
					}
				}
				if (found)
					break;
			}

			return found;
		}
        public static void PushParameterValuesToNetwork(INetworkAnalystExtension nax)
        {
            try
            {
                if (nax == null)
                {
                    return;
                }

                bool             naxEnabled = false;
                IExtensionConfig naxConfig  = nax as IExtensionConfig;
                naxEnabled = naxConfig.State == esriExtensionState.esriESEnabled;

                if (!naxEnabled)
                {
                    return;
                }

                INAWindow       naWindow  = nax.NAWindow;
                INALayer        naLayer   = null;
                INAContext      naContext = null;
                INetworkDataset nds       = null;

                naLayer = naWindow.ActiveAnalysis;
                if (naLayer != null)
                {
                    naContext = naLayer.Context;
                }

                if (naContext != null)
                {
                    nds = naContext.NetworkDataset;
                }

                if (nds == null)
                {
                    return;
                }

                IDatasetComponent dsComponent = nds as IDatasetComponent;
                IDENetworkDataset deNet       = dsComponent.DataElement as IDENetworkDataset;

                INASolver          naSolver          = naContext.Solver;
                INASolverSettings2 naSolverSettings2 = naSolver as INASolverSettings2;

                if (naSolverSettings2 == null)
                {
                    return;
                }

                INetworkAttribute2 netAttribute;
                string             attributeName;

                IArray netParameters;
                INetworkAttributeParameter netParameter;
                string paramName;
                int    cParameters;

                object paramValue;

                int cAttributes = nds.AttributeCount;
                for (int a = 0; a < cAttributes; ++a)
                {
                    netAttribute  = nds.get_Attribute(a) as INetworkAttribute2;
                    attributeName = netAttribute.Name;
                    netParameters = netAttribute.Parameters;

                    cParameters = netParameters.Count;
                    for (int p = 0; p < cParameters; ++p)
                    {
                        netParameter = netParameters.get_Element(p) as INetworkAttributeParameter;
                        paramName    = netParameter.Name;

                        paramValue         = naSolverSettings2.get_AttributeParameterValue(attributeName, paramName);
                        netParameter.Value = paramValue;
                    }

                    netAttribute.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Push Parameter Values To Network");
            }
        }
        public static void UpdateEIDArrayParameterValuesFromEIDLists(INetworkAnalystExtension nax, Dictionary <string, List <int> > eidsBySourceName, string baseName)
        {
            if (nax == null)
            {
                return;
            }

            bool             naxEnabled = false;
            IExtensionConfig naxConfig  = nax as IExtensionConfig;

            naxEnabled = naxConfig.State == esriExtensionState.esriESEnabled;

            if (!naxEnabled)
            {
                return;
            }

            INAWindow       naWindow  = nax.NAWindow;
            INALayer        naLayer   = null;
            INAContext      naContext = null;
            INetworkDataset nds       = null;

            naLayer = naWindow.ActiveAnalysis;
            if (naLayer != null)
            {
                naContext = naLayer.Context;
            }

            if (naContext != null)
            {
                nds = naContext.NetworkDataset;
            }

            if (nds == null)
            {
                return;
            }

            IDatasetComponent dsComponent = nds as IDatasetComponent;
            IDENetworkDataset deNet       = dsComponent.DataElement as IDENetworkDataset;

            INASolver          naSolver          = naContext.Solver;
            INASolverSettings2 naSolverSettings2 = naSolver as INASolverSettings2;

            if (naSolverSettings2 == null)
            {
                return;
            }

            string  prefix = GetEIDArrayPrefixFromBaseName(baseName);
            VarType vt     = GetEIDArrayParameterType();

            int cAttributes = nds.AttributeCount;

            for (int a = 0; a < cAttributes; ++a)
            {
                INetworkAttribute2 netAttribute = nds.get_Attribute(a) as INetworkAttribute2;
                IArray             netParams    = netAttribute.Parameters;
                int    cParams = netParams.Count;
                object paramValue;
                for (int p = 0; p < cParams; ++p)
                {
                    INetworkAttributeParameter param = netParams.get_Element(p) as INetworkAttributeParameter;
                    if (param.VarType != (int)vt)
                    {
                        continue;
                    }

                    string paramName  = param.Name;
                    string sourceName = GetSourceNameFromParameterName(prefix, paramName);
                    if (sourceName.Length == 0)
                    {
                        continue;
                    }

                    List <int> eids = null;
                    if (eidsBySourceName.TryGetValue(sourceName, out eids))
                    {
                        if (eids != null)
                        {
                            if (eids.Count == 0)
                            {
                                eids = null;
                            }
                        }
                    }

                    paramValue = (eids != null) ? eids.ToArray() : null;
                    naSolverSettings2.set_AttributeParameterValue(netAttribute.Name, param.Name, paramValue);
                }
            }
        }
        /// <summary>
        /// Initialize the solver by calling the ArcGIS Network Analyst extension functions.
        /// </summary>
        private void Initialize()
        {
            IFeatureWorkspace featureWorkspace = null;
            INetworkDataset   networkDataset   = null;

            try
            {
                // Open Geodatabase and network dataset
                IWorkspace workspace = OpenWorkspace(Application.StartupPath + @"\..\..\..\..\..\Data\SanFrancisco\SanFrancisco.gdb");
                networkDataset   = OpenNetworkDataset(workspace, "Transportation", "Streets_ND");
                featureWorkspace = workspace as IFeatureWorkspace;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Unable to open dataset. Error Message: " + ex.Message);
                this.Close();
                return;
            }

            // Create NAContext and NASolver
            CreateSolverContext(networkDataset);

            // Get available cost attributes from the network dataset
            INetworkAttribute networkAttribute;

            for (int i = 0; i < networkDataset.AttributeCount - 1; i++)
            {
                networkAttribute = networkDataset.get_Attribute(i);
                if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
                {
                    cboCostAttribute.Items.Add(networkAttribute.Name);
                }
            }
            cboCostAttribute.SelectedIndex = 0;

            txtTargetFacility.Text = "1";
            txtCutOff.Text         = "";

            // Load incidents from a feature class
            IFeatureClass inputFClass = featureWorkspace.OpenFeatureClass("Stores");

            LoadNANetworkLocations("Incidents", inputFClass, 500);

            // Load facilities from a feature class
            inputFClass = featureWorkspace.OpenFeatureClass("FireStations");
            LoadNANetworkLocations("Facilities", inputFClass, 500);

            //Create Layer for Network Dataset and add to ArcMap
            INetworkLayer networkLayer = new NetworkLayerClass();

            networkLayer.NetworkDataset = networkDataset;
            var layer = networkLayer as ILayer;

            layer.Name = "Network Dataset";
            axMapControl.AddLayer(layer, 0);

            //Create a Network Analysis Layer and add to ArcMap
            INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);

            layer      = naLayer as ILayer;
            layer.Name = m_NAContext.Solver.DisplayName;
            axMapControl.AddLayer(layer, 0);
        }
예제 #16
0
        /// <summary>
        /// Set controls based on the current NALayer settings
        /// This function takes the current NALayer and determines what type of solver it's pointing to
        /// and populates the corresponding controls and hides the tabs for the other solvers.
        /// </summary>
        private void PopulateControls(INALayer naLayer)
        {
            ILayer                      layer            = naLayer as ILayer;
            INAContext                  naContext        = naLayer.Context;
            INetworkDataset             networkDataset   = naContext.NetworkDataset;
            INALocator2                 naLocator        = naContext.Locator as INALocator2;
            INASolver                   naSolver         = naContext.Solver;
            INASolverSettings           naSolverSettings = naSolver as INASolverSettings2;
            INARouteSolver2             routeSolver      = naSolver as INARouteSolver2;
            INAClosestFacilitySolver    cfSolver         = naSolver as INAClosestFacilitySolver;
            INAODCostMatrixSolver       odSolver         = naSolver as INAODCostMatrixSolver;
            INAServiceAreaSolver2       saSolver         = naSolver as INAServiceAreaSolver2;
            INAVRPSolver                vrpSolver        = naSolver as INAVRPSolver;
            INALocationAllocationSolver laSolver         = naSolver as INALocationAllocationSolver;

            // Populate general Layer controls
            txtLayerName.Text          = layer.Name;
            txtMaxSearchTolerance.Text = naLocator.MaxSnapTolerance.ToString();
            cboMaxSearchToleranceUnits.SelectedIndex = Convert.ToInt32(naLocator.SnapToleranceUnits);

            // Populate controls for the particular solver

            if (routeSolver != null)  // ROUTE LAYER
            {
                // Remove unnecessary tabs
                tabPropPages.TabPages.Remove(tabClosestFacility);
                tabPropPages.TabPages.Remove(tabODCostMatrix);
                tabPropPages.TabPages.Remove(tabServiceArea);
                tabPropPages.TabPages.Remove(tabVRP);
                tabPropPages.TabPages.Remove(tabLocationAllocation);

                // INARouteSolver2
                chkRouteFindBestSequence.Checked  = routeSolver.FindBestSequence;
                chkRoutePreserveFirstStop.Checked = routeSolver.PreserveFirstStop;
                chkRoutePreserveLastStop.Checked  = routeSolver.PreserveLastStop;
                chkRouteUseTimeWindows.Checked    = routeSolver.UseTimeWindows;
                chkRouteUseStartTime.Checked      = routeSolver.UseStartTime;
                txtRouteStartTime.Text            = routeSolver.StartTime.ToShortTimeString();
                cboRouteOutputLines.SelectedIndex = System.Convert.ToInt32(routeSolver.OutputLines);

                // INASolverSettings
                PopulateImpedanceNameControl(cboRouteImpedance, networkDataset, naSolverSettings.ImpedanceAttributeName);
                chkRouteUseHierarchy.Enabled           = (naSolverSettings.HierarchyAttributeName.Length > 0);
                chkRouteUseHierarchy.Checked           = (chkRouteUseHierarchy.Enabled && naSolverSettings.UseHierarchy);
                chkRouteIgnoreInvalidLocations.Checked = naSolverSettings.IgnoreInvalidLocations;
                cboRouteRestrictUTurns.SelectedIndex   = System.Convert.ToInt32(naSolverSettings.RestrictUTurns);
                PopulateAttributeControl(chklstRouteAccumulateAttributeNames, networkDataset, naSolverSettings.AccumulateAttributeNames, esriNetworkAttributeUsageType.esriNAUTCost);
                PopulateAttributeControl(chklstRouteRestrictionAttributeNames, networkDataset, naSolverSettings.RestrictionAttributeNames, esriNetworkAttributeUsageType.esriNAUTRestriction);
            }
            else if (cfSolver != null)  // CLOSEST FACILITY LAYER
            {
                // Remove unnecessary tabs
                tabPropPages.TabPages.Remove(tabRoute);
                tabPropPages.TabPages.Remove(tabODCostMatrix);
                tabPropPages.TabPages.Remove(tabServiceArea);
                tabPropPages.TabPages.Remove(tabVRP);
                tabPropPages.TabPages.Remove(tabLocationAllocation);

                // INAClosestFacilitySolver
                txtCFDefaultCutoff.Text = GetStringFromObject(cfSolver.DefaultCutoff);
                txtCFDefaultTargetFacilityCount.Text = cfSolver.DefaultTargetFacilityCount.ToString();
                cboCFTravelDirection.SelectedIndex   = Convert.ToInt32(cfSolver.TravelDirection);
                cboCFOutputLines.SelectedIndex       = Convert.ToInt32(cfSolver.OutputLines);

                // INASolverSettings
                PopulateImpedanceNameControl(cboCFImpedance, networkDataset, naSolverSettings.ImpedanceAttributeName);
                chkCFUseHierarchy.Enabled           = (naSolverSettings.HierarchyAttributeName.Length > 0);
                chkCFUseHierarchy.Checked           = (chkCFUseHierarchy.Enabled && naSolverSettings.UseHierarchy);
                chkCFIgnoreInvalidLocations.Checked = naSolverSettings.IgnoreInvalidLocations;
                cboCFRestrictUTurns.SelectedIndex   = System.Convert.ToInt32(naSolverSettings.RestrictUTurns);
                PopulateAttributeControl(chklstCFAccumulateAttributeNames, networkDataset, naSolverSettings.AccumulateAttributeNames, esriNetworkAttributeUsageType.esriNAUTCost);
                PopulateAttributeControl(chklstCFRestrictionAttributeNames, networkDataset, naSolverSettings.RestrictionAttributeNames, esriNetworkAttributeUsageType.esriNAUTRestriction);
            }
            else if (odSolver != null) // OD COST MATRIX LAYER
            {
                // Remove unnecessary tabs
                tabPropPages.TabPages.Remove(tabRoute);
                tabPropPages.TabPages.Remove(tabClosestFacility);
                tabPropPages.TabPages.Remove(tabServiceArea);
                tabPropPages.TabPages.Remove(tabVRP);

                // INAODCostMatrixSolver
                txtODDefaultCutoff.Text = GetStringFromObject(odSolver.DefaultCutoff);
                txtODDefaultTargetDestinationCount.Text = GetStringFromObject(odSolver.DefaultTargetDestinationCount);
                cboODOutputLines.SelectedIndex          = Convert.ToInt32(odSolver.OutputLines);

                // INASolverSettings
                PopulateImpedanceNameControl(cboODImpedance, networkDataset, naSolverSettings.ImpedanceAttributeName);
                chkODUseHierarchy.Enabled           = (naSolverSettings.HierarchyAttributeName.Length > 0);
                chkODUseHierarchy.Checked           = (chkODUseHierarchy.Enabled && naSolverSettings.UseHierarchy);
                chkODIgnoreInvalidLocations.Checked = naSolverSettings.IgnoreInvalidLocations;
                cboODRestrictUTurns.SelectedIndex   = System.Convert.ToInt32(naSolverSettings.RestrictUTurns);
                PopulateAttributeControl(chklstODAccumulateAttributeNames, networkDataset, naSolverSettings.AccumulateAttributeNames, esriNetworkAttributeUsageType.esriNAUTCost);
                PopulateAttributeControl(chklstODRestrictionAttributeNames, networkDataset, naSolverSettings.RestrictionAttributeNames, esriNetworkAttributeUsageType.esriNAUTRestriction);
            }
            else if (saSolver != null)  //SERVICE AREA SOLVER
            {
                // Remove unnecessary tabs
                tabPropPages.TabPages.Remove(tabRoute);
                tabPropPages.TabPages.Remove(tabClosestFacility);
                tabPropPages.TabPages.Remove(tabODCostMatrix);
                tabPropPages.TabPages.Remove(tabVRP);
                tabPropPages.TabPages.Remove(tabLocationAllocation);

                // INAServiceAreaSolver2
                txtSADefaultBreaks.Text = "";
                for (int iBreak = 0; iBreak < saSolver.DefaultBreaks.Count; iBreak++)
                {
                    txtSADefaultBreaks.Text = txtSADefaultBreaks.Text + " " + saSolver.DefaultBreaks.get_Element(iBreak).ToString();
                }
                cboSATravelDirection.SelectedIndex = Convert.ToInt32(saSolver.TravelDirection);

                cboSAOutputPolygons.SelectedIndex           = -1;
                cboSAOutputPolygons.SelectedIndex           = Convert.ToInt32(saSolver.OutputPolygons);
                chkSAOverlapPolygons.Checked                = saSolver.OverlapPolygons;
                chkSASplitPolygonsAtBreaks.Checked          = saSolver.SplitPolygonsAtBreaks;
                chkSAMergeSimilarPolygonRanges.Checked      = saSolver.MergeSimilarPolygonRanges;
                chkSATrimOuterPolygon.Checked               = saSolver.TrimOuterPolygon;
                txtSATrimPolygonDistance.Text               = saSolver.TrimPolygonDistance.ToString();
                cboSATrimPolygonDistanceUnits.SelectedIndex = Convert.ToInt32(saSolver.TrimPolygonDistanceUnits);

                cboSAOutputLines.SelectedIndex  = -1;
                cboSAOutputLines.SelectedIndex  = Convert.ToInt32(saSolver.OutputLines);
                chkSAOverlapLines.Checked       = saSolver.OverlapLines;
                chkSASplitLinesAtBreaks.Checked = saSolver.SplitLinesAtBreaks;
                chkSAIncludeSourceInformationOnLines.Checked = saSolver.IncludeSourceInformationOnLines;

                // INASolverSettings
                PopulateImpedanceNameControl(cboSAImpedance, networkDataset, naSolverSettings.ImpedanceAttributeName);
                chkSAIgnoreInvalidLocations.Checked = naSolverSettings.IgnoreInvalidLocations;
                cboSARestrictUTurns.SelectedIndex   = System.Convert.ToInt32(naSolverSettings.RestrictUTurns);
                PopulateAttributeControl(chklstSAAccumulateAttributeNames, networkDataset, naSolverSettings.AccumulateAttributeNames, esriNetworkAttributeUsageType.esriNAUTCost);
                PopulateAttributeControl(chklstSARestrictionAttributeNames, networkDataset, naSolverSettings.RestrictionAttributeNames, esriNetworkAttributeUsageType.esriNAUTRestriction);
            }
            else if (vrpSolver != null) // VRP Solver
            {
                // Remove unnecessary tabs
                tabPropPages.TabPages.Remove(tabRoute);
                tabPropPages.TabPages.Remove(tabClosestFacility);
                tabPropPages.TabPages.Remove(tabODCostMatrix);
                tabPropPages.TabPages.Remove(tabServiceArea);
                tabPropPages.TabPages.Remove(tabLocationAllocation);

                cboVRPOutputShapeType.SelectedIndex = Convert.ToInt32(vrpSolver.OutputLines);
                cboVRPAllowUTurns.SelectedIndex     = Convert.ToInt32(naSolverSettings.RestrictUTurns);
                // VRP cannot have unknown units, so the index is offset by 1 from the solver field units
                cboVRPDistanceFieldUnits.SelectedIndex = Convert.ToInt32(vrpSolver.DistanceFieldUnits) - 1;
                cboVRPTransitTime.SelectedIndex        = Convert.ToInt32(vrpSolver.ExcessTransitTimePenaltyFactor);
                cboVRPTimeWindow.SelectedIndex         = Convert.ToInt32(vrpSolver.TimeWindowViolationPenaltyFactor);
                cboVRPTimeFieldUnits.SelectedIndex     = Convert.ToInt32(vrpSolver.TimeFieldUnits - 20);

                txtVRPCapacityCount.Text = vrpSolver.CapacityCount.ToString();
                txtVRPDefaultDate.Text   = vrpSolver.DefaultDate.ToShortDateString();

                chkVRPUseHierarchy.Checked = naSolverSettings.UseHierarchy;

                PopulateAttributeControl(chklstVRPRestrictionAttributeNames, networkDataset, naSolverSettings.RestrictionAttributeNames, esriNetworkAttributeUsageType.esriNAUTRestriction);

                //populate the time attribute combo box
                cboVRPTimeAttribute.Items.Clear();

                for (int i = 0; i < networkDataset.AttributeCount; i++)
                {
                    INetworkAttribute networkAttribute = networkDataset.get_Attribute(i);

                    if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost &&
                        networkAttribute.Units >= esriNetworkAttributeUnits.esriNAUSeconds)
                    {
                        cboVRPTimeAttribute.Items.Add(networkAttribute.Name);
                    }
                }

                if (cboVRPTimeAttribute.Items.Count > 0)
                {
                    cboVRPTimeAttribute.Text = naSolverSettings.ImpedanceAttributeName;
                }


                // for VRP, the AccumulateAttributeNames hold the length, and it can only hold one length.
                //  Loop through the network dataset attributes
                cboVRPDistanceAttribute.Items.Clear();
                cboVRPDistanceAttribute.SelectedIndex = cboVRPDistanceAttribute.Items.Add("");

                for (int i = 0; i < networkDataset.AttributeCount; i++)
                {
                    INetworkAttribute networkAttribute = networkDataset.get_Attribute(i);
                    if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost &&
                        networkAttribute.Units < esriNetworkAttributeUnits.esriNAUSeconds)
                    {
                        string attributeName = networkAttribute.Name;

                        int cboindex = cboVRPDistanceAttribute.Items.Add(networkAttribute.Name);

                        // If the attribute is in the strArray, it should be the selected one
                        for (int j = 0; j < naSolverSettings.AccumulateAttributeNames.Count; j++)
                        {
                            if (naSolverSettings.AccumulateAttributeNames.get_Element(j) == attributeName)
                            {
                                cboVRPDistanceAttribute.SelectedIndex = cboindex;
                            }
                        }
                    }
                }
            }
            else if (laSolver != null)  // Location-Allocation LAYER
            {
                // Remove unnecessary tabs
                tabPropPages.TabPages.Remove(tabRoute);
                tabPropPages.TabPages.Remove(tabClosestFacility);
                tabPropPages.TabPages.Remove(tabODCostMatrix);
                tabPropPages.TabPages.Remove(tabServiceArea);
                tabPropPages.TabPages.Remove(tabVRP);

                // INALocationAllocationSolver
                txtLACutOff.Text             = GetStringFromObject(laSolver.DefaultCutoff);
                txtLAFacilitiesToLocate.Text = laSolver.NumberFacilitiesToLocate.ToString();
                txtLAImpParameter.Text       = laSolver.TransformationParameter.ToString();
                txtLATargetMarketShare.Text  = laSolver.TargetMarketSharePercentage.ToString();

                cboLAImpTransformation.SelectedIndex = Convert.ToInt32(laSolver.ImpedanceTransformation);
                cboLAProblemType.SelectedIndex       = Convert.ToInt32(laSolver.ProblemType);
                cboLAOutputLines.SelectedIndex       = Convert.ToInt32(laSolver.OutputLines);
                cboLATravelDirection.SelectedIndex   = Convert.ToInt32(laSolver.TravelDirection);

                //// INASolverSettings
                PopulateImpedanceNameControl(cboLAImpedance, networkDataset, naSolverSettings.ImpedanceAttributeName);
                PopulateAttributeControl(chklstLAAccumulateAttributeNames, networkDataset, naSolverSettings.AccumulateAttributeNames, esriNetworkAttributeUsageType.esriNAUTCost);
                PopulateAttributeControl(chklstLARestrictionAttributeNames, networkDataset, naSolverSettings.RestrictionAttributeNames, esriNetworkAttributeUsageType.esriNAUTRestriction);
                chkLAUseHierarchy.Enabled           = (naSolverSettings.HierarchyAttributeName.Length > 0);
                chkLAUseHierarchy.Checked           = (chkCFUseHierarchy.Enabled && naSolverSettings.UseHierarchy);
                chkLAIgnoreInvalidLocations.Checked = naSolverSettings.IgnoreInvalidLocations;
            }
            else  // Unknown type of layer
            {
                // Remove unnecessary tabs
                tabPropPages.TabPages.Remove(tabRoute);
                tabPropPages.TabPages.Remove(tabClosestFacility);
                tabPropPages.TabPages.Remove(tabODCostMatrix);
                tabPropPages.TabPages.Remove(tabServiceArea);
                tabPropPages.TabPages.Remove(tabVRP);
                tabPropPages.TabPages.Remove(tabLocationAllocation);
            }
        }