Ultrafast Distance Matrix
Contents
Transportation, parcel, and logistics companies often need to calculate distances and travel times between thousands of locations in order to:
- Optimize the order of stops for driver routes based on delivery windows, distances, etc.
- Identify optimal locations for warehouses
- Identify the nearest hub for parcel consolidation and distribution
Trimble’s Ultrafast Distance Matrix (UFDM) Service is a RESTful web service that can perform distance and time calculations for a matrix of thousands of locations in near real time. What’s more, you can select a vehicle type so that distances and times are accurately calculated based on safe and legal commercial vehicle routes.
UFDM calculations can then be fed into any optimization algorithm that requires a distance matrix. (For example, Trimble uses UFDM in its own Daily Planner route optimization and editing application.)
Highlights
Matrix Input Limit: 50,000 x 50,000 locations
Job Handling: One queue where jobs are handled in first-in, first-out (FIFO) order
UFDM Engine Local Processing Time:
- ~5 minutes for 50,000 x 50,000
- ~30 seconds for 10,000 x 10,000
- ~13 seconds for 1 x 5,000
- ~2 seconds for 1 x 1,000
- ~700 milliseconds for 1 x 500
Note: Service performance may differ from the above processing times, which were calculated during our testing.
Getting Started
To gain access to the Ultrafast Distance Matrix API, you must be a Premium Web Services subscriber with an add-on license. Please contact your Trimble Maps representative or our Sales team for more information.
Workflow
To use the Distance Matrix API:
- Authenticate with your Trimble Maps API key.
- Submit a POST request with your origins and destinations and two optional parameters: a
profileName, which describes the vehicle type used in routing calculations, and aregion, which specifies the worldwide region—North America (NA), Europe (EU), Oceania (OC), and South America (SA). - The service provides one of the following responses:
- Success: returns a
responseobject with the “de-hydrated” matrix and anidentifierthat can be used to retrieve theresponseat a later time with a GET request. - Failure: based on one of the following reasons:
- Empty request body
- Improper JSON format
- Improper Stops string-list format (required: lon1,lat1;lon2,lat2;…;lonN,latN)
- Unauthorized
The GET request can be used to retrieve results asynchronously by passing the identifier returned by the POST request. The GET call will return either Complete or Processing. Once a Success: Completed response is returned, the field named response contains a JSON object with the matrix. However, this matrix still needs to be re-hydrated.
Authentication
Authenticate your request to the server by using the API key provided by Trimble Maps. Insert the API Key in the http Authorization: header with all requests.
profileName Options
You can submit a vehicle routing profile (profileName) with your request to be sure times and distances are based on routes that are safe and legal for the vehicle type. If you do not include a profileName, a default profile is applied based on the region:
- North America:
Heavy Duty Semitrailer - Europe:
Heavy Articulated - Oceania:
Heavy Semitrailer - South America:
Heavy Articulated
The best practice is to select a profile with dimensions equal to or larger than the dimensions of the vehicles you are routing.
North America
| Name | Description |
|---|---|
Heavy Duty Semitrailer
| 53 feet long x 102 inches wide semitrailer |
Default Rating Profile
| 48 feet long x 96 inches wide semitrailer |
Medium Duty Straight
| 26 feet long x 96 inches wide straight truck |
Commercial Light
| 20 feet long x 96 inches wide |
Auto
| Passenger car |
Europe
| Name | Description |
|---|---|
Heavy Articulated
| 16.5 meters long x 2.55 meters wide |
Heavy Rigid
| 12 meters long x 2.55 meters wide |
Midsize
| 10 meters long x 2.55 meters wide |
Light
| 8 meters long x 2.4 meters wide |
Auto
| Passenger car |
Oceania
| Name | Description |
|---|---|
Heavy Semitrailer
| 19 meters long x 2.44 meters wide |
Heavy Rigid
| 12.5 meters long x 2.44 meters wide |
Midsize
| 12.5 meters long x 2.44 meters wide |
Light
| 8 meters long x 2.4 meters wide |
Auto
| Passenger car |
South America
| Name | Description |
|---|---|
Heavy Articulated
| 18.6 meters long x 2.6 meters wide |
Full details about each profile can be retrieved using the GET Default Profiles endpoint.
Sample Request Body
originStops and destStops are semicolon-separated lists of longitude and latitude coordinates for the locations in your matrix.
{
"originStops": "-89.33257925,34.93112826;-98.22538877,37.79842125",
"destStops": "-89.33257925,34.93112826;-98.22538877,37.79842125",
"profileName": "Heavy Duty Semitrailer",
"region": "NA"
}
Sample Python Script
This sample includes “rehydrator” code to get the actual distances from the service.
- Distances are returned as miles. The integer value returned must be multiplied by .001 to get the actual mileage.
- Times are returned in seconds. The integer value returned must be multiplied by .1 to get the actual time.
import requests
import time
import json
API_KEY = '<INSERT API KEY>'
INFINITY = (2 ** 32) - 1
UFDM_URL = 'https://routematrix-prod.az.trimblemaps.com/routing/v1/distancematrix'
def post_matrix(origins, destinations, region, profile):
request_payload = {
'originStops': origins,
'destStops': destinations,
'region': region,
'profileName': profile
}
headers = {'Content-Type': 'application/json', 'Authorization': API_KEY}
response = requests.post(UFDM_URL, data=json.dumps(request_payload), headers=headers)
return response.json()['identifier']
def get_response(request_id):
start = time.time()
while time.time() - start < 5.0:
response = requests.get(f"{UFDM_URL}/{request_id}", headers={'Authorization': API_KEY})
response_body = response.json()
if response_body['status'] == 'Completed':
return response_body
return None
def rehydrate_row(packed_matrix, row):
cost_row = [INFINITY] * packed_matrix['cols']
time_row = [INFINITY] * packed_matrix['cols']
dist_row = [INFINITY] * packed_matrix['cols']
fw_index = packed_matrix['fwIndex'][row]
while fw_index < packed_matrix['fwIndex'][row + 1]:
fw_via = packed_matrix['fwVia'][fw_index]
fw_cost = packed_matrix['fwCost'][fw_index]
fw_time = packed_matrix['fwTime'][fw_index]
fw_dist = packed_matrix['fwDistance'][fw_index]
bw_index = packed_matrix['bwIndex'][fw_via]
while bw_index < packed_matrix['bwIndex'][fw_via + 1]:
bw_cost = packed_matrix['bwCost'][bw_index]
bw_time = packed_matrix['bwTime'][bw_index]
bw_dist = packed_matrix['bwDistance'][bw_index]
col = packed_matrix['bwTarget'][bw_index]
if fw_cost != INFINITY and (fw_cost + bw_cost) < cost_row[col]:
cost_row[col] = fw_cost + bw_cost
time_row[col] = fw_time + bw_time
dist_row[col] = fw_dist + bw_dist
bw_index += 1
fw_index += 1
if packed_matrix['symmetric']:
cost_row[row] = 0
time_row[row] = 0
dist_row[row] = 0
return time_row, dist_row
def request_and_display(origins, destinations, region, profile):
request_id = post_matrix(origins, destinations, region, profile)
print(f"Submitted request: {request_id}")
response = get_response(request_id)
if response:
processing_time = response['response']['processingTimeMs']
packed = response['response']['packed']
print(f"Processing took {processing_time} ms")
print(f"Received {packed['rows']} x {packed['cols']} matrix")
for row in range(packed['rows']):
times, dists = rehydrate_row(packed, row)
print(f"Row {row}: dist {dists} time {times}")
def main():
cust_origin_tx = '''-104.37207,29.5607;
-104.3846,29.5607;
-104.37214,29.5627;
-95.42344,29.80335;
-95.41794,29.92169;
-104.54222,29.9219;
-95.48346,29.94145;
-97.81095,30.09231;
-102.40046,30.142;
-97.69731,30.26275'''
cust_origin_uk = '''0.8507,52.328465;
-2.691114,52.126265;
-2.488092,51.408395;
-3.53254,50.480772;
-3.51683,50.398578;
-3.529639,50.496136;
-3.515783,50.478186;
0.719566,52.302898;
-3.538942,50.470835;
-2.482926,54.620316'''
request_and_display(cust_origin_tx, cust_origin_tx, 'NA', 'Auto')
request_and_display(cust_origin_tx, cust_origin_tx, 'NA', 'Heavy Duty Semitrailer')
request_and_display(cust_origin_uk, cust_origin_uk, 'EU', 'Auto')
request_and_display(cust_origin_uk, cust_origin_uk, 'EU', 'Heavy Articulated')
if __name__ == "__main__":
main()