Пример #1
0
        /// <summary>
        /// Output Results map, Directions
        /// </summary>
        private void OutputResults(NAServerSolverParams solverParams, NAServerSolverResults solverResults)
        {
            string messagesSolverResults = "";

            // Output Solve messages
            GPMessages gpMessages = solverResults.SolveMessages;

            GPMessage[] arrGPMessage = gpMessages.GPMessages1;
            if (arrGPMessage != null)
            {
                for (int i = 0; i < arrGPMessage.GetLength(0); i++)
                {
                    GPMessage gpMessage = arrGPMessage[i];
                    messagesSolverResults += "\n" + gpMessage.MessageDesc;
                }
            }

            // Output the total impedance of each route
            NAServerRouteResults routeSolverResults = solverResults as NAServerRouteResults;

            //Output Map
            pictureBox.Image = null;
            if (solverParams.ReturnMap)
            {
                pictureBox.Image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(solverResults.MapImage.ImageData));
            }
            pictureBox.Refresh();

            if (routeSolverResults != null)
            {
                OutputDirections(routeSolverResults.Directions);                 // Return Directions if generated
            }
            tabCtrlOutput.Enabled = true;
        }
Пример #2
0
        /// <summary>
        /// This function
        ///     - sets the server and solver parameters
        ///     - populates the stops NALocations
        ///     - gets and displays the server results (map, directions)
        /// </summary>
        private void cmdSolve_Click(object sender, System.EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            try
            {
                // Get SolverParams
                NAServerSolverParams solverParams = m_naServer.GetSolverParameters(cboNALayers.Text) as NAServerSolverParams;

                // Set Solver parameters
                SetServerSolverParams(solverParams);

                // Load Locations
                LoadLocations(solverParams);

                //Solve the Route
                NAServerSolverResults solverResults;
                solverResults = m_naServer.Solve(solverParams);

                //Get NAServer results in the tab controls
                OutputResults(solverParams, solverResults);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "An error has occurred");
            }

            this.Cursor = Cursors.Default;
        }
        ///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="serviceUrl">Service url.</param>
        /// <param name="layerName">Layer name.</param>
        /// <param name="server">Server.</param>
        internal NetworkDescription(string serviceUrl, string layerName,
                                    AgsServer server)
        {
            Debug.Assert(serviceUrl != null);

            // Create connection.
            var             connection = server.OpenConnection();
            NAServiceClient client     = new NAServiceClient(serviceUrl, connection);

            try
            {
                NAServerNetworkDescription desc = client.GetNetworkDescription(layerName);

                NAServerSolverParams solverParams = client.GetSolverParameters(layerName);

                var parameterValues = solverParams.AttributeParameterValues.ToLookup(
                    value => value.AttributeName, StringComparer.OrdinalIgnoreCase);

                // Create attributes.
                foreach (NAServerNetworkAttribute attr in desc.NetworkAttributes)
                {
                    var routingAttributeName = attr.Name;
                    var attributeParameter   = parameterValues[attr.Name].FirstOrDefault();
                    if (attributeParameter != null)
                    {
                        routingAttributeName = attributeParameter.AttributeName;
                    }

                    var usageType = _ConvertUsageType(attr.UsageType);

                    var usageParameter =
                        _GetRestrictionUsageParameter(attr.RestrictionUsageParameterName,
                                                      parameterValues[attr.Name], usageType);

                    var attribute = new NetworkAttribute(attr.Name, routingAttributeName,
                                                         _ConvertUnits(attr.Units), usageType,
                                                         _GetAttrParams(attr, parameterValues[attr.Name]), usageParameter);

                    _attributes.Add(attribute);
                }

                // Enabled restriction names.
                _enabledRestrictionNames = solverParams.RestrictionAttributeNames;
                if (_enabledRestrictionNames == null)
                {
                    _enabledRestrictionNames = new string[0];
                }

                // Get impedance attribute name.
                _impedanceAttrName = solverParams.ImpedanceAttributeName;
            }
            finally
            {
                client.Close();
            }
        }
Пример #4
0
        /// <summary>
        /// Set server solver paramaters  (ReturnMap, SnapTolerance, etc.)
        /// </summary>
        private void SetServerSolverParams(NAServerSolverParams solverParams)
        {
            solverParams.ReturnMap = chkReturnMap.Checked;
            solverParams.ImageDescription.ImageDisplay.ImageWidth  = pictureBox.Width;
            solverParams.ImageDescription.ImageDisplay.ImageHeight = pictureBox.Height;


            NAServerRouteParams routeParams = solverParams as NAServerRouteParams;

            if (routeParams != null)
            {
                routeParams.ReturnDirections = chkReturnDirections.Checked;
            }
        }
Пример #5
0
        /// <summary>
        /// This function shows how to populate stop locations using an array of PropertySets
        /// </summary>
        private void LoadLocations(NAServerSolverParams solverParams)
        {
            // Geocode Addresses
            PropertySet[] propSets = new PropertySet[2];
            propSets[0] = GeocodeAddress(txtStartStreetAddress.Text, txtStartCity.Text, txtStartState.Text, txtStartZipCode.Text);
            propSets[1] = GeocodeAddress(txtEndStreetAddress.Text, txtEndCity.Text, txtEndState.Text, txtEndZipCode.Text);

            NAServerPropertySets StopsPropSets = new NAServerPropertySets();

            StopsPropSets.PropertySets = propSets;

            NAServerRouteParams routeParams = solverParams as NAServerRouteParams;

            routeParams.Stops = StopsPropSets;
        }
		/// <summary>
		/// Set server solver paramaters  (ReturnMap, SnapTolerance, etc.)
		/// </summary>
		private void SetServerSolverParams(NAServerSolverParams solverParams)
		{
			solverParams.ReturnMap = chkReturnMap.Checked;
			solverParams.ImageDescription.ImageDisplay.ImageWidth = pictureBox.Width;
			solverParams.ImageDescription.ImageDisplay.ImageHeight = pictureBox.Height;


			NAServerRouteParams routeParams = solverParams as NAServerRouteParams;
			if (routeParams != null)
				routeParams.ReturnDirections = chkReturnDirections.Checked;
		}
		/// <summary>
		/// Get ServerSolverParams controls (ReturnMap, SnapTolerance, etc.)
		/// </summary>
		private void GetServerSolverParams(NAServerNetworkDescription networkDescription, NAServerSolverParams solverParams)
		{
			chkReturnMap.Checked = true;
			txtSnapTolerance.Text = solverParams.SnapTolerance.ToString();
			txtMaxSnapTolerance.Text = solverParams.MaxSnapTolerance.ToString();
			cboSnapToleranceUnits.SelectedIndex = Convert.ToInt32(solverParams.SnapToleranceUnits);

			//Set Route Defaults
			chkReturnRouteGeometries.Checked = false;
			chkReturnRoutes.Checked = true;
			chkReturnStops.Checked = true;
			chkReturnDirections.Checked = true;
			checkReturnBarriers.Checked = true;

			//Set Directions Defaults
			cboRouteDirectionsTimeAttribute.Items.Clear();
			NAServerNetworkAttribute[] attributes = networkDescription.NetworkAttributes;
			for (int i = 0; i < attributes.Length; i++)
			{
				NAServerNetworkAttribute networkAttribute = attributes[i];
				if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
				{
					if (String.Compare(networkAttribute.Units.ToString(), "esriNAUMinutes") == 0)
					{
						cboRouteDirectionsTimeAttribute.Items.Add(networkAttribute.Name);
					}
				}
			}

			// Set the default direction settings
			NAServerRouteParams routeParams = solverParams as NAServerRouteParams;
			if (routeParams != null)
			{
				cboRouteDirectionsLengthUnits.Text = routeParams.DirectionsLengthUnits.ToString().Substring(7);
				//Select the first time attribute
				if (cboRouteDirectionsTimeAttribute.Items.Count > 0)
					cboRouteDirectionsTimeAttribute.Text = routeParams.DirectionsTimeAttributeName;
			}
		}
		/// <summary>
		/// Set Default route solver controls  (BestOrder, UseTimeWindows, UseStartTime, etc.)
		/// </summary>
		private void GetSolverSpecificInterface(NAServerSolverParams solverParams)
		{
			NAServerRouteParams routeSolver = solverParams as NAServerRouteParams;
			if (routeSolver != null)
			{
				chkBestOrder.Checked = routeSolver.FindBestSequence;
				chkPreserveFirst.Checked = routeSolver.PreserveFirstStop;
				chkPreserveLast.Checked = routeSolver.PreserveLastStop;
				if (chkBestOrder.Checked == true)
				{
					this.chkPreserveFirst.Enabled = true;
					this.chkPreserveLast.Enabled = true;
				}
				else
				{
					this.chkPreserveFirst.Enabled = false;
					this.chkPreserveLast.Enabled = false;
				}
				chkUseTimeWindows.Checked = routeSolver.UseTimeWindows;
				chkUseStartTime.Checked = routeSolver.UseStartTime;
				if (chkUseStartTime.Checked)
				{
					txtStartTime.Enabled = true;
					txtStartTime.Text = routeSolver.StartTime.ToString();
				}
				else
				{
					txtStartTime.Enabled = false;
					txtStartTime.Text = System.DateTime.Now.ToString();
				}
				cboRouteOutputLines.SelectedIndex = System.Convert.ToInt32(routeSolver.OutputLines);
			}
		}
		/// <summary>
		/// Get Default NASolverSettings controls (Cost Attributes, Restrictions Attributes. etc.)
		/// </summary>
		private void GetNASolverSettings(NAServerNetworkDescription networkDescription, NAServerSolverParams solverSettings)
		{
			int ImpedanceIndex = 0;

			//Get Attributes
			cboImpedance.Items.Clear();
			chklstAccumulateAttributes.Items.Clear();
			chklstRestrictions.Items.Clear();
			cboUturnPolicy.SelectedIndex = -1;

			NAServerNetworkAttribute[] attributes = networkDescription.NetworkAttributes;
			string[] accumulateAttributeNames = solverSettings.AccumulateAttributeNames;
			string[] restrictionAttributeNames = solverSettings.RestrictionAttributeNames;

			for (int i = 0; i < attributes.Length; i++)
			{
				NAServerNetworkAttribute networkAttribute = attributes[i];
				string networkAttributeName = networkAttribute.Name;
				if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)
				{
					chklstAccumulateAttributes.Items.Add(networkAttributeName, IsStringInStringArray(networkAttributeName, accumulateAttributeNames));

					int index = cboImpedance.Items.Add(networkAttributeName + " (" + networkAttribute.Units.ToString().Substring(7) + ")");
					if (networkAttributeName == solverSettings.ImpedanceAttributeName)
						ImpedanceIndex = index;
				}

				if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTRestriction)
				{
					chklstRestrictions.Items.Add(networkAttributeName, IsStringInStringArray(networkAttributeName, restrictionAttributeNames));
				}
			}

			if (cboImpedance.Items.Count > 0)
				cboImpedance.SelectedIndex = ImpedanceIndex;

			chkUseHierarchy.Checked = solverSettings.UseHierarchy;
			chkUseHierarchy.Enabled = solverSettings.HierarchyAttributeName.Length > 0;
			chkIgnoreInvalidLocations.Checked = solverSettings.IgnoreInvalidLocations;
			cboUturnPolicy.SelectedIndex = System.Convert.ToInt32(solverSettings.RestrictUTurns);
		}
		/// <summary>
		/// Output Results Messages, Map, Route Geometries, Directions
		/// </summary>
		private void OutputResults(NAServerSolverParams solverParams, NAServerSolverResults solverResults)
		{
			string messagesSolverResults = "";

			// Output Solve messages
			GPMessages gpMessages = solverResults.SolveMessages;
			GPMessage[] arrGPMessage = gpMessages.GPMessages1;
			if (arrGPMessage != null)
			{
				for (int i = 0; i < arrGPMessage.GetLength(0); i++)
				{
					GPMessage gpMessage = arrGPMessage[i];
					messagesSolverResults += "\n" + gpMessage.MessageDesc;
				}
			}

			// Uncomment the following section to output the total impedance of each route in a MessageBox

			//NAServerRouteResults routeSolverResults = solverResults as NAServerRouteResults;
			//if (routeSolverResults != null)
			//{
			//    for (int i = 0; i < routeSolverResults.TotalImpedances.GetLength(0); i++)
			//    {
			//        messagesSolverResults += "\nTotal Impedance for Route[" + (i + 1) + "] = ";
			//        messagesSolverResults += routeSolverResults.TotalImpedances[i].ToString("f");
			//        messagesSolverResults += " " + ExtractImpedanceUnits(cboImpedance.Text).ToLower();
			//    }
			//}

			// Show a message box displaying both solver messages and total_impedance per route
			if (messagesSolverResults.Length > 0)
				MessageBox.Show(messagesSolverResults, "NAServer Route Results");

			//Output Map
			pictureBox.Image = null;
			if (solverParams.ReturnMap)
			{
				pictureBox.Image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(solverResults.MapImage.ImageData));
			}
			pictureBox.Refresh();

			if (((solverParams as NAServerRouteParams) != null) && ((solverResults as NAServerRouteResults) != null))
				OutputRouteResults(solverParams as NAServerRouteParams, solverResults as NAServerRouteResults);
		}
		/// <summary>
		/// This function shows how to populate stop locations using two different options:
		///     1) From Record Set using a Point Feature Class - Uncommented
		///     2) From an Array of PropertySets - Commented out
		/// Uncomment the option, you would like to use
		/// </summary>
		private void LoadLocations(NAServerSolverParams solverParams)
		{
			// Set first point
			PropertySet[] propSets = new PropertySet[2];

			propSets[0] = CreateLocationPropertySet("Stop 1", "-122.49024904900", "37.74811940430", null);
			propSets[1] = CreateLocationPropertySet("Stop 2", "-122.43083365400", "37.75396354490", null);

			NAServerRouteParams routeParams = solverParams as NAServerRouteParams;
			NAServerPropertySets stopsPropSets = new NAServerPropertySets();
			stopsPropSets.PropertySets = propSets;

			routeParams.Stops = stopsPropSets;
		}
		/// <summary>
		/// Set server solver parameters  (ReturnMap, SnapTolerance, etc.)
		/// </summary>
		private void SetServerSolverParams(NAServerSolverParams solverParams)
		{
			solverParams.ReturnMap = chkReturnMap.Checked;
			solverParams.SnapTolerance = Convert.ToDouble(txtSnapTolerance.Text);
			solverParams.MaxSnapTolerance = Convert.ToDouble(txtMaxSnapTolerance.Text);
			solverParams.SnapToleranceUnits = (esriUnits)cboSnapToleranceUnits.SelectedIndex;
			solverParams.ImageDescription.ImageDisplay.ImageWidth = pictureBox.Width;
			solverParams.ImageDescription.ImageDisplay.ImageHeight = pictureBox.Height;

			//			// This code shows how to specify the output spatial reference in order to get the map
			//			// in a different spatial reference than the Network Dataset
			//			ESRISpatialReferenceGEN sr = pServerContext.CreateObject("esriGeometry.GeographicCoordinateSystem") as IESRISpatialReferenceGEN;
			//			int read;
			//			sr.ImportFromESRISpatialReference("GEOGCS[GCS_North_American_1983,DATUM[D_North_American_1983,SPHEROID[GRS_1980,6378137.0,298.257222101]],PRIMEM[Greenwich,0.0],UNIT[Degree,0.0174532925199433]]", out read);
			//			solverParams.OutputSpatialReference = sr as SpatialReference;

			NAServerRouteParams routeParams = solverParams as NAServerRouteParams;
			if (routeParams != null)
			{
				routeParams.ReturnRouteGeometries = chkReturnRouteGeometries.Checked;
				routeParams.ReturnRoutes = chkReturnRoutes.Checked;
				routeParams.ReturnStops = chkReturnStops.Checked;
				routeParams.ReturnBarriers = checkReturnBarriers.Checked;
				routeParams.ReturnDirections = chkReturnDirections.Checked;
				routeParams.DirectionsLengthUnits = GetstringToesriUnits(cboRouteDirectionsLengthUnits.Text);
				routeParams.DirectionsTimeAttributeName = cboRouteDirectionsTimeAttribute.Text;
			}
		}
		/// <summary>
		/// Set specific solver settings  (FindBestSequence, UseTimeWindows, etc.)      
		/// </summary>  
		private void SetSolverSpecificInterface(NAServerSolverParams solverParams)
		{
			NAServerRouteParams routeSolver = solverParams as NAServerRouteParams;
			if (routeSolver != null)
			{
				routeSolver.FindBestSequence = chkBestOrder.Checked;
				routeSolver.PreserveFirstStop = chkPreserveFirst.Checked;
				routeSolver.PreserveLastStop = chkPreserveLast.Checked;
				routeSolver.UseTimeWindows = chkUseTimeWindows.Checked;
				routeSolver.OutputLines = (esriNAOutputLineType)cboRouteOutputLines.SelectedIndex;

				routeSolver.UseStartTime = chkUseStartTime.Checked;
				if (routeSolver.UseStartTime == true)
					routeSolver.StartTime = System.Convert.ToDateTime(txtStartTime.Text.ToString());
			}
		}
		/// <summary>
		/// Set general solver settings  (Impedance, Restrictions, Accumulates, etc.)
		/// </summary>
		private void SetNASolverSettings(NAServerSolverParams solverSettings)
		{
			solverSettings.ImpedanceAttributeName = ExtractImpedanceName(cboImpedance.Text);

			string[] restrictionAttributes = new string[chklstRestrictions.CheckedItems.Count];
			for (int i = 0; i < chklstRestrictions.CheckedItems.Count; i++)
				restrictionAttributes[i] = chklstRestrictions.Items[chklstRestrictions.CheckedIndices[i]].ToString();
			solverSettings.RestrictionAttributeNames = restrictionAttributes;

			string[] accumulateAttributes = new string[chklstAccumulateAttributes.CheckedItems.Count];
			for (int i = 0; i < chklstAccumulateAttributes.CheckedItems.Count; i++)
				accumulateAttributes[i] = chklstAccumulateAttributes.Items[chklstAccumulateAttributes.CheckedIndices[i]].ToString();
			solverSettings.AccumulateAttributeNames = accumulateAttributes;

			solverSettings.RestrictUTurns = (esriNetworkForwardStarBacktrack)cboUturnPolicy.SelectedIndex;
			solverSettings.IgnoreInvalidLocations = chkIgnoreInvalidLocations.Checked;
			solverSettings.UseHierarchy = chkUseHierarchy.Checked;
		}
		/// <summary>
		/// This function shows how to populate stop locations using an array of PropertySets
		/// </summary>
		private void LoadLocations(NAServerSolverParams solverParams)
		{
			// Geocode Addresses
			PropertySet[] propSets = new PropertySet[2];
			propSets[0] = GeocodeAddress(txtStartStreetAddress.Text, txtStartCity.Text, txtStartState.Text, txtStartZipCode.Text);
			propSets[1] = GeocodeAddress(txtEndStreetAddress.Text, txtEndCity.Text, txtEndState.Text, txtEndZipCode.Text);

			NAServerPropertySets StopsPropSets = new NAServerPropertySets();
			StopsPropSets.PropertySets = propSets;

			NAServerRouteParams routeParams = solverParams as NAServerRouteParams;
			routeParams.Stops = StopsPropSets;
		}
		/// <summary>
		/// Output Results map, Directions
		/// </summary>
		private void OutputResults(NAServerSolverParams solverParams, NAServerSolverResults solverResults)
		{
			string messagesSolverResults = "";

			// Output Solve messages
			GPMessages gpMessages = solverResults.SolveMessages;
			GPMessage[] arrGPMessage = gpMessages.GPMessages1;
			if (arrGPMessage != null)
			{
				for (int i = 0; i < arrGPMessage.GetLength(0); i++)
				{
					GPMessage gpMessage = arrGPMessage[i];
					messagesSolverResults += "\n" + gpMessage.MessageDesc;
				}
			}

			// Output the total impedance of each route
			NAServerRouteResults routeSolverResults = solverResults as NAServerRouteResults;

			//Output Map
			pictureBox.Image = null;
			if (solverParams.ReturnMap)
			{
				pictureBox.Image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(solverResults.MapImage.ImageData));
			}
			pictureBox.Refresh();

			if (routeSolverResults != null)
				OutputDirections(routeSolverResults.Directions); // Return Directions if generated

			tabCtrlOutput.Enabled = true;
		}
		/// <summary>
		/// Make frames Enabled
		/// </summary> 
		private void MakeFramesEnabled(NAServerSolverParams solverParams)
		{
			fraINARouteSolver.Enabled = ((solverParams as NAServerRouteParams) != null);
			fraINAServerRouteParams.Enabled = ((solverParams as NAServerRouteParams) != null);
			fraINASolverSettings.Enabled = ((solverParams as NAServerRouteParams) != null);
		}