Network Analysis service Solve method

Performs network analysis based on the input solve parameters.

Solve(NAServerSolverParams SolverParams)

Parameter

Description

SolverParams

Input parameters for one of three different network operations: route, closest facility and service area.

Return Value

A NAServerSolverResults object containing results defined by the input network operation type (route, closest facility, or service area).

Remarks

This method is designed to be generic, thus the network operation is defined by the type of NAServerSolverParams provided as an input parameter. The Solve routine takes in solver properties (e.g. cost attribute, reorder stops, breaks, etc), input network locations (e.g. stops, barriers, etc), and result parameters specifying what should be returned (e.g. directions, map, network locations like stops with populated analysis values, analysis result classes, simple geometries, etc).

Examples

C#

namespace NetworkSoapSamples
{

  public class ExtendedNAServerProxy : ESRI.ArcGIS.SOAP.NAServerProxy
  {
    private string m_referer;
    public ExtendedNAServerProxy(string referer)
    {
      m_referer = referer;
    }

    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {

      System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

      webRequest.Referer = m_referer;

      return webRequest;

    }
  }

  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void btnRun_Click(object sender, EventArgs e)
    {
      SolveRoute();
      SolveClosestFacililty();
    }


    private void SolveRoute()
    {
      string username = "org_username";
      string password = "org_password";
      string referer = "soapSample";
      string url = "https://route.arcgis.com/arcgis/services/World/Route/MapServer/NAServer";

      // Create a reference to the NAServer service and append the arcgis.com token
      NAServerProxy naService = new ExtendedNAServerProxy(referer);
      naService.Url = GetURLWithToken(url, username, password, referer);


      // Get the default settings for the world route layer
      NAServerRouteParams naServerRouteParams = (NAServerRouteParams)naService.GetSolverParameters("Route_World");


      // Create 2 stops
      List<PropertySet> propertySets = new List<PropertySet>(2);
      propertySets.Add(CreatePropertySet("My starting point", -117.195905, 34.057783));
      propertySets.Add(CreatePropertySet("My ending point", -117.180943, 34.056286));
      NAServerPropertySets naServerPropertySets = new NAServerPropertySets();
      naServerPropertySets.PropertySets = propertySets.ToArray();
      naServerRouteParams.Stops = naServerPropertySets;

      // specify to return compact directions
      naServerRouteParams.ReturnCompactDirections = true;


      // Solve
      NAServerRouteResults naServerRouteResults = (NAServerRouteResults)naService.Solve(naServerRouteParams);


      // Output directions
      NACompactStreetDirections streetDirections = naServerRouteResults.CompactDirections[0];
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("DriveTime = {0}", streetDirections.Summary.TotalDriveTime);
      sb.AppendLine();
      foreach (NACompactStreetDirection streetDirection in streetDirections.Directions)
        sb.AppendLine(streetDirection.Text);
      MessageBox.Show(sb.ToString());

    }

    void SolveClosestFacililty()
    {
      string username = "org_username";
      string password = "org_password";
      string referer = "soapSample";
      string url = "https://route.arcgis.com/arcgis/services/World/ClosestFacility/MapServer/NAServer";

      // Create a reference to the NAServer service and append the arcgis.com token
      NAServerProxy naService = new ExtendedNAServerProxy(referer);
      naService.Url = GetURLWithToken(url, username, password, referer);


      // Get the default settings for the world closest facility layer
      NAServerClosestFacilityParams naServerClosestFacilityParams = (NAServerClosestFacilityParams)naService.GetSolverParameters("ClosestFacility_World");


      // Create 2 facilities
      List<PropertySet> propertySets = new List<PropertySet>(2);
      propertySets.Add(CreatePropertySet("My starting point", -117.195905, 34.057783));
      propertySets.Add(CreatePropertySet("My ending point", -117.180943, 34.056286));
      NAServerPropertySets facilitiesPropertySets = new NAServerPropertySets();
      facilitiesPropertySets.DoNotLocateOnRestrictedElements = true;
      facilitiesPropertySets.DoNotLocateOnRestrictedElementsSpecified = true;
      facilitiesPropertySets.PropertySets = propertySets.ToArray();
      naServerClosestFacilityParams.Facilities = facilitiesPropertySets;


      // Create 1 incident
      propertySets = new List<PropertySet>(1);
      propertySets.Add(CreatePropertySet("My incident", -117.0, 34.0));
      NAServerPropertySets incidentsPropertySets = new NAServerPropertySets();
      incidentsPropertySets.DoNotLocateOnRestrictedElements = true;
      incidentsPropertySets.DoNotLocateOnRestrictedElementsSpecified = true;
      incidentsPropertySets.PropertySets = propertySets.ToArray();
      naServerClosestFacilityParams.Incidents = incidentsPropertySets;

      // specify to return compact directions
      naServerClosestFacilityParams.ReturnCompactDirections = true;


      // Solve
      NAServerClosestFacilityResults naServerClosestFacilityResults = (NAServerClosestFacilityResults)naService.Solve(naServerClosestFacilityParams);


      // Output directions
      NACompactStreetDirections streetDirections = naServerClosestFacilityResults.CompactDirections[0];
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("DriveTime = {0}", streetDirections.Summary.TotalDriveTime);
      sb.AppendLine();
      foreach (NACompactStreetDirection streetDirection in streetDirections.Directions)
        sb.AppendLine(streetDirection.Text);

      MessageBox.Show(sb.ToString());
    }

    private string GetURLWithToken(string url, string username, string password, string referer)
    {
      // Add token to service URL if required

      string tokenServiceURL;
      ServiceCatalogProxy serviceCatalogProxy = new ServiceCatalogProxy();
      serviceCatalogProxy.Url = url.Substring(0, url.IndexOf("/services") + 9);
      if (serviceCatalogProxy.RequiresTokens())
      {
        tokenServiceURL = serviceCatalogProxy.GetTokenServiceURL();

        // Get token
        string token;
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.CreateDefault(new Uri(tokenServiceURL + "?f=json&expiration=60" +
                    "&username=" + username + "&password=" + password + "&referer=" + referer));
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        using (StreamReader reader = new StreamReader(responseStream))
        {
          string responseText = reader.ReadToEnd();
          if ((responseText.Length == 0) || (responseText.IndexOf("error") > -1))
          {
            MessageBox.Show("Error getting token.");
            return url;
          }

          int startPos = 12;
          int endPos = responseText.IndexOf("\"", startPos);
          token = responseText.Substring(startPos, endPos - startPos);

          reader.Close();
        }
        responseStream.Close();
        response.Close();

        // Add token to the URL
        return url + "?token=" + token;
      }
      else
      {
        return url;
      }
    }

    private PropertySet CreatePropertySet(string name, double x, double y)
    {
      List<PropertySetProperty> propertySetPropertyList = new List<PropertySetProperty>(3);
      propertySetPropertyList.Add(CreatePropertySetProperty("Name", name));
      propertySetPropertyList.Add(CreatePropertySetProperty("X", x));
      propertySetPropertyList.Add(CreatePropertySetProperty("Y", y));

      PropertySet propertySet = new PropertySet();
      propertySet.PropertyArray = propertySetPropertyList.ToArray();

      return propertySet;
    }

    private PropertySetProperty CreatePropertySetProperty(string key, object value)
    {
      PropertySetProperty propertySetProperty = new PropertySetProperty();
      propertySetProperty.Key = key;
      propertySetProperty.Value = value;

      return propertySetProperty;
    }
  }
}

10/23/2013