Create routes

The Create Routes tool finds the best route between a series of input points. The resulting DataFrame contains a linestring column with the routes that visit the input points.

The returned routes can be:

  • The shortest route between two locations that minimizes the travel distance or travel time.
  • A route that visits a series of locations in a specified order. This is referred to as a simple route.
  • A route that finds the best sequence in which to visit a series of locations. This is referred to as an optimized route.
cr overview

Usage notes

  • The Create Routes tool returns a route for each input record.

  • A network dataset is required to run any network analysis tool. It must be locally accessible to all nodes in your Spark cluster. Use setNetwork() to load the network dataset from a mobile map package or a mobile geodatabase.

  • A travel mode refers to the mode of transportation, such as driving or walking. Use setTravelMode() to choose a mode defined in the network datasource, or a custom travel mode defined in a JSON string. By default, the tool uses the default travel mode in the network datasource.

  • The setStops() parameter takes an array of points, referred to as stops, which are locations that the returned route will visit.

  • The setRouteGeometry() parameter accepts one of the following inputs:

    • "AlongNetwork"—Generates true paths along the network. cr along network
    • "StraightLines"—Outputs simplified paths as straight lines. cr straight lines
    • "NoLines"—No lines are generated. cr no lines
  • The setSequence() setter determines the order in which the input stops will be visited to create a route. You can optimally route the stops by setting the find_best parameter to True. When you are optimially routing you can preserve one or both of the first and last stops by setting the preserve_first and preserve_last parameters to True. If you set the find_best parameter to False, the route will use the sequence provided by your input in setStops(), and the preserve_first and preserve_last parameters are not applied.

  • Below is an example of how the setSequence() setter works given a series of stops that were set by calling setStops(A,B,C,D).

    • Using setSequence(find_best=False), the Create Routes tool will return a route that visits the points in the order they were passed in, which is A,B,C,D.

      cr sequence use current
    • Using setSequence(find_best=True), the Create Routes tool will return a route that visits the points in an optimized order that minimizes travel costs, which is A,C,D,B.

      cr sequence find best
    • Using setSequence(find_best=False, preserve_first=True, preserve_last=True), the Create Routes tool will return a route that visits the points in an optimized order that minimizes travel costs while still preserving the order of the first and last points, which is A,C,B,D:

      cr sequence find best preserve first and last
  • When travel mode is configured wth traffic data, you can specify the start time in a day of week for the route. Route start time is typically used to find routes based on the impedance attribute that varies with the time. For example, a start time of 7:00 a.m. can be used to find a route that considers rush hour traffic.

    Traffic information will not be used in network analysis when setTime() is not used, or when the setter is used but no traffic data is configured with the travel mode .

  • GeoAnalytics Engine supports setting the start time as a specific time in a generic weekday using setTime(day_of_week, time, time_zone = "UTC").

    day_of_week is a string representing the day of the week. Acceptable values are:

    • Sunday
    • Monday
    • Tuesday
    • Wednesday
    • Thursday
    • Friday
    • Saturday

    time is the time of day when traffic will be modeled. It can be provided in two formats:

    • A string in the format "HH:mm:ss", for example, "14:30:00".
    • A datetime.time object.

    time_zone is an optional string representing the time zone. The default time zone is Coordinated Universal Time (UTC). You can specify a time zone ID in the following formats to use local time.

    • UTC offset—a fixed offset from UTC. For example "UTC-05:00" representing the time zone that is five hours behind UTC. GeoAnalytics Engine does not account for Daylight Savings Time (DST). Only Standard Time (SDT) is used for UTC offset.
    • Time zone identifier—a standardized string that uniquely identifies a time zone region (e.g., "America/New_York"). For a comprehensive list of time zone identifiers, refer to this list of tz database time zones.

    For example, to set the start time as Friday at 8:15 a.m. in the "America/New_York" time zone, you can use setTime("Friday", "08:15:00", "America/New_York"), setTime("Friday", datetime.time(8,15), "America/New_York"), or setTime("Friday", "13:15:00", "UTC").

Limitations

  • Network analysis requires a network dataset from a mobile map package or a mobile geodatabase. Loading network data from a file geodatabase is not supported. Using a network service, such as the ArcGIS Online network analysis service, is not supported.

  • GeoAnalytics Engine does not support adding barriers in network analysis.

Results

In addition to the fields from the input DataFrame, the fields in the following table are included in the output DataFrame. The only exception to this is if the NoLines option is used for .setRouteGeometry(), in which the route_geometry column won't be returned:

FieldDescription
travel_timeThe total travel time for the route in minutes.
travel_distanceThe total travel distance for the route in meters.
route_geometryThe linestring geometry representing the route.

Performance notes

Improve the performance of Create Routes by doing one or more of the following:

  • Only analyze the records in your area of interest. You can pick the records of interest by using one of the following SQL functions:

    • ST_Intersection—Clip to an area of interest represented by a polygon. This will modify your input records.
    • ST_BboxIntersects—Select records that intersect an envelope.
    • ST_EnvIntersects—Select records having an evelope that intersects the envelope of another geometry.
    • ST_Intersects—Select records that intersect another dataset or area of intersect represented by a polygon.
  • Set the route geometry to Nolines instead of the AlongNetwork or StraightLines if you are only interested in determining the total travel time or travel distance between a series of input points.

Similar capabilities

Syntax

For more details, go to the GeoAnalytics Engine API reference for create routes.

SetterDescriptionRequired
run(dataframe)Runs the Create Routes tool using the provided DataFrame.Yes
setNetwork(path)The path of the network dataset that will be used for the network analysis.Yes
setStops(*stops)A DataFrame column containing arrays of points that will be used to create the route.Yes
setSequence(find_best, preserve_first, preserve_last)Determines the order in which the route will visit the input stops.No
setRouteGeometry(route_geometry)Sets the route geometry representing the route between the stops. Choose from 'AlongNetwork' (default), 'StraightLines' or 'NoLines'.No
setTime(day_of_week, time, time_zone = "UTC")Sets the start time for the routes.No
setTravelMode(travel_mode)Sets the travel mode. By default, the tool uses the default travel mode in the network datasource.No

Examples

Run Create Routes

Python
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Log in
import geoanalytics
geoanalytics.auth(username="myusername", password="mypassword")

# Imports
from pyspark.sql import functions as F
from geoanalytics.sql import functions as ST
from geoanalytics.tools import CreateRoutes

# Path to the New York vehicle start and end points
vehicle_locations_path = r"C:\nyc_vehicle_start_end_points"

# Path to the New York network dataset
network_dataset_path = r"C:\New_York.mmpk"

# Create a DataFrame with an array column made up of the vehicle start and end points along with two additional points
# to visit in New York city as part of the route
trip_locations_df = spark.read.format("geoparquet").load(vehicle_locations_path)\
                    .withColumn("empire_state_building", ST.point(F.lit(-73.9814406), F.lit(40.7342447), sr=4326))\
                    .withColumn("area_53", ST.point(F.lit(-73.9282016), F.lit(40.7122077), sr=4326))\
                    .withColumn("trip_locations", F.array("trip_start", "area_53", "empire_state_building", "trip_end"))

# Run the Create Routes tool to generate a route for each vehicle that minimizes the driving time to visit all of the
# stops, while preserving the trip start and end locations.
optimal_trip_routes = CreateRoutes()\
                        .setNetwork(network_dataset_path)\
                        .setStops("trip_locations")\
                        .setTravelMode("Driving Time")\
                        .setRouteGeometry("AlongNetwork")\
                        .setSequence(find_best=True, preserve_first=True, preserve_last=True)\
                        .run(trip_locations_df)

# Show selected columns from the results DataFrame
optimal_trip_routes.select("unique_id", "trip_locations", "travel_time", "travel_distance", "route_geometry").show(5)
Result
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
+---------+--------------------+-----------------+------------------+--------------------+
|unique_id|      trip_locations|      travel_time|   travel_distance|      route_geometry|
+---------+--------------------+-----------------+------------------+--------------------+
|        0|[{"x":-73.8710784...|47.27411416634998| 23385.41041389714|{"paths":[[[-73.8...|
|        1|[{"x":-73.9837799...|55.21945154864122|22991.525780181702|{"paths":[[[-73.9...|
|        2|[{"x":-73.9820556...| 65.9056057881696|25997.134204527312|{"paths":[[[-73.9...|
|        3|[{"x":-74.0005111...|47.14527620656458|19792.443213209775|{"paths":[[[-74.0...|
|        4|[{"x":-73.8659591...|51.05305474030447| 25497.14207067214|{"paths":[[[-73.8...|
+---------+--------------------+-----------------+------------------+--------------------+
only showing top 5 rows

Plot results

Python
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Select the route for vehicle 11 and tranform the route geometry and the input points for plotting to 32118 (NAD83 - New York Long Island)
vehicle_11_route = optimal_trip_routes.where("unique_id == 11")\
                                      .withColumn("route_geometry", ST.transform("route_geometry", sr=32118))\
                                      .withColumn("trip_start", ST.transform("trip_start", sr=32118))\
                                      .withColumn("empire_state_building", ST.transform("empire_state_building", sr=32118))\
                                      .withColumn("area_53", ST.transform("area_53", sr=32118))\
                                      .withColumn("trip_end", ST.transform("trip_end", sr=32118))

# Plot the route for vehicle 11 that minimizes driving time
route_plot = vehicle_11_route.st.plot(geometry="route_geometry", basemap="light", figsize=(16, 10))
start_plot = vehicle_11_route.st.plot(ax=route_plot, geometry="trip_start", c="green", marker="d", s=100, label="Trip Start")
empire_state_building_plot = vehicle_11_route.st.plot(ax=start_plot, geometry="empire_state_building", c="darkorange", marker="d", s=100, label="Empire State Building")
area_53_plot = vehicle_11_route.st.plot(ax=empire_state_building_plot, geometry="area_53", c="violet", marker="d", s=100, label="Area 53 Adventure Park")
result_plot = vehicle_11_route.st.plot(ax=area_53_plot, geometry="trip_end", c="red", marker="d", s=100, legend=True, label="Trip End")
result_plot.legend(loc="upper right")
result_plot.set_title("Route That Minimizes The Driving Time To Visit All Of The New York Stops For Vehicle 11")
result_plot.set_xlabel("X (Meters)")
result_plot.set_ylabel("Y (Meters)");
Plotting example for the Create Routes result. The optimal route to visit the locations in New York using the
specified start and stop locations for the trip is shown.

Version table

ReleaseNotes

1.3.0

Python tool introduced

1.4.0

Added support for setting start time.

1.5.0

Added support for loading the network dataset using SparkContext.addFile.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.