Routing

Routing is a line feature, such as as street or river, that has a path through a network.

Add routing to your map

A Route task allows you to calculate point-to-point and multi-point routes using ArcGIS Server Network Analyst services.A Network Analyst service contains one or more Network Analysis layers. ArcGIS provides different types of analysis layers, such as Route, Service Area, Closest Facility, etc depending upon the type of analysis to be performed. A Route task relies on a Route analysis layer to compute routes.

ArcGIS Online provides Network Analyst services based on street data for North America and Europe, but you can also create your own services using ArcGIS Server. Learn more about creating Network Analysis Services

Creating a Route Task

To instantiate a Route task, you need to provide a URL to REST resource that represents a Route layer in a Network Analyst service. If the service is secured, you will also need to provide the credentials that can be used to access the service. The following code snippet shows how to create a Route task for the Route layer in the ESRI_Route_NA service on ArcGIS Online.

String routeTaskURL = "http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route";
RouteTask routeTask = new RouteTask(routeTaskURL, null);

Route Task Parameters

You provide input to the Route task using an object of RoutingParameters class. You can instantiate a new RoutingParameters object and set its parameters to provide the input. Many times you may want to calculate routes using the default parameters specified in the service. The only required parameter you need to set is the stops parameter, all others parameters not set will use the default service parameters. The following sections describe some of the inputs you can provide to a Route task.

Impedance

mpedance specifies a cost that should be minimized in the resulting route. For example, impedance could be set to Time in order to calculate the fastest route or Length to calculate the shortest route. Impedances supported by the service are listed in the Services Directory under Network Dataset → Network Attributes with a Usage Type of esriNAUTCost

NoteNote:

TODO: Insert Network Attirbutes image

In the example shown above, Length and Time are supported impedances. The following code snippets sets the impedance to Length in order to calculate the shortest route.

// set impedance for length
RoutingParameters routeParams = new RoutingParameters();
routeParams.setImpedanceAttributeName("Length");

Stops

Stops specify the locations that must be visited along the route. You need at least 2 stops to calculate a valid route, however, you can add more stops if you like. There are two ways to specify stops:

Features

The NAFeaturesAsFeatures class contains stops represented as Graphics. You can create an array of StopGraphic objects representing stop features. You can then assign these features to the parameter object using setFeatures() method. The following code snippet assumes you have constructed two points representing your current location, mLocation, and the destination location, mDestination, to route to.

// create routing features class
NAFeaturesAsFeature naFeatures = new NAFeaturesAsFeature();
// Create the stop points from point geometry
StopGraphic startPnt = new StopGraphic(mLocation);
StopGraphic endPnt = new StopGraphic(mDestination);
// set features on routing feature class
naFeatures.setFeatures(new Graphic[] {point1, point2});
// set stops on routing feature class
routeParams.setStops(naFeatures);

Layer Definition

Apart from specifying stops by-value, e.g. providing the actual values for each feature as described above, you can also specify them by-reference. This is useful when you already have a set of well known or commonly used stops stored along with the Network Analyst service. In such cases, the application does not need to know the actual details about each stop. All it needs to do is set up a layer definition specifiying which stops should be included in the analysis.

A layer definition is represented by an object of the NAFeaturesAsLayer class. You can use SQL statements and/or Spatial relationships to specify which stops should be used in the analysis. For example, the following code snippets sets up a layer definition referencing features that fall within the City of Los Angeles and have a value of “Overnight” for the delivery_type attribute.

Apart from specifying stops by-value, e.g. providing the actual values for each feature as described above, you can also specify them by-reference. This is useful when you already have a set of well known or commonly used stops stored along with the Network Analyst service. In such cases, the application does not need to know the actual details about each stop. All it needs to do is set up a layer definition specifiying which stops should be included in the analysis.

A layer definition is represented by an object of the NAFeaturesAsLayer class. You can use SQL statements and/or Spatial relationships to specify which stops should be used in the analysis. For example, the following code snippets sets up a layer definition referencing features that fall within the City of Los Angeles and have a value of Overnight for the delivery_type attribute.

NAFeaturesAsLayer naLayer = new NAFeaturesAsLayer();
// set layer name with the layer in the service
// containing stops
naLayer.setLayerName("<layer_in_service_containing_stops>");
// set where clause for 'Overnight' delivery_type
naLayer.setWhere("delivery_type='Overnight'");
// set stops on routing layer
routeParams.setStops(naLayer);

Calculatinig Routes

Once you have set up the input parameters, calculating routes is as simple as invoking solve() method on RoutingTask and passing in the RoutingParameters object to use in the calculation.

RoutingResult mResults = routeTask.solve(routeParams);

This method returns a RoutingResult object containing the relevant information for the entire server response. This includes all routes, as well as the stops, point/polyline/polygon barriers, and messages returned by the server.

Retrieving Routes

Once you have a RoutingResult returned you can use a Route class which holds the information of the route returned.

Route mRoute = mResults.getRoutes().get(0);

From the returned Route object you can obtain routing directions as a List and add the route segments to a graphics layer:

From the returned Route object you can obtain routing directions as a List and add the route segments to a graphics layer:

// List of the directions for the current route
ArrayList<String> curDirections = new ArrayList<String>();
// iterate through routing directions
for(RoutingDirection routeDirections : mRoute.getRoutingDirections()) {
   // create a map of attribute values
   HashMap<String, Object> attribs = new HashMap<String, Object>();
   // add attributes 
   attribs.put("text", routeDirections.getText());
   attribs.put("time", Double.valueOf(routeDirections.getTime()));
   attribs.put("length", Double.valueOf(routeDirections.getLength()));
   // add to the list of directions
   curDirections.add(String.format("%s%nTime: %.1f minutes, Length: %.1f miles",
       routeDirections.getText(), routeDirections.getTime(), routeDirections.getLength()));
   // populate graphics layer with route directions
   gLayer.addGraphic(new Graphic(routeDirections.getGeometry(), simpleLineSymbol, attribs, null));
}
1/24/2013