Accessing distance matrix contents with binary encoded arrays

This use-case describes how to create a distance matrix and get the content in encoded format. As the content is in the binary compact encoded arrays, here is shown how to get in uncoded format

Benefits

Prerequisites

Please ensure following prerequisites are fulfilled before you start with the use case:

Programming Guide

Javascript

This below Javascript shows how to use in a simple way the uncoding process:

var locations = [ { "$type": "OffRoadRouteLocation", "offRoadCoordinate": { "x": 147.35255956649783, "y": -43.06534499935546 } }, { "$type": "OffRoadRouteLocation", "offRoadCoordinate": { "x": 147.37571239471438, "y": -43.100011493947584 } }, { "$type": "OnRoadRouteLocation", "coordinate": { "x": 147.31614589691165, "y": -43.003077911658735 } } ]; var contentsResultFields = { "distances" : true, "travelTimes" : true, "violated" : true, "estimatedByDirectDistance" : true, }; var resultFields = { "summary" : true, "distanceMatrixContentsResultFields" : contentsResultFields }; var distanceMatrixResponse; var uncodedResponse; var encodedResponse; xdima.createDistanceMatrix({ "startLocations": locations }, dimaReturn); // first request is not encoded var contentsOptions = { "returnEncodedArrays" : false }; function dimaReturn(distanceMatrixResp, exception) { distanceMatrixResponse = distanceMatrixResp; var byLocationsRequest = { "$type": "GetDistanceMatrixByLocationsRequest", "id" : distanceMatrixResponse.summary.id, "startLocations": locations, "destinationLocations": locations, "resultFields": resultFields, "contentsOptions" : contentsOptions }; xdima.getDistanceMatrix(byLocationsRequest, uncodedCallback); }; function uncodedCallback(resultUncoded, exception) { uncodedResponse = resultUncoded; encodedVersionCalling(); }; function encodedVersionCalling() { var byLocationsRequest = { "$type": "GetDistanceMatrixByLocationsRequest", "id" : distanceMatrixResponse.summary.id, "startLocations": locations, "destinationLocations": locations, "resultFields": resultFields, "contentsOptions" : {"returnEncodedArrays" : true} // second request in encoded }; xdima.getDistanceMatrix(byLocationsRequest, encodedCallback); }; function encodedCallback(resultEncoded, exception) { encodedResponse = resultEncoded; convertAndCheckResults(); }; // returns the value of the integer in the buffer function getUint32FromIndex( byteArray, index ){ // set the endianness in the second parameter, tha data is in little endian return byteArray.getUint32(index * 4, true); // the offset is 4, this can be calculated like byteArray.length / elementCount }; // the byteArray is encoded in base64 // getBytes makes conversion from base64 to Uint8Array function getBytes( byteArray ){ var decoded = atob(byteArray); var i, il = decoded.length; var array = new Uint8Array(il); for (i = 0; i< il; ++i) { array[i] = decoded.charCodeAt(i); } return array; }; function getBoolValueAt(byteArray, index) { var bitMask = 1 << (index % 8); var val = byteArray[Math.trunc(index / 8)] & bitMask; return val === bitMask; }; function convertAndCheckResults(){ var elementCount = encodedResponse.summary.numberOfStartLocations * encodedResponse.summary.numberOfDestinationLocations ; var distancesBytes = getBytes(encodedResponse.contents.distances); // uncoding the contents which is encoded in base64 var distances = new DataView(distancesBytes.buffer); var travelTimesBytes = getBytes(encodedResponse.contents.travelTimes ); var travelTimes = new DataView(travelTimesBytes.buffer); var violatedBytes = getBytes(encodedResponse.contents.violated); var estimatedByDirectDistanceBytes = getBytes(encodedResponse.contents.estimatedByDirectDistance); for (i = 0; i < elementCount; ++i) { var valuesAreSame = true; if (getUint32FromIndex(distances, i) != uncodedResponse.contents.distances[i]) { print('Distance is not same'); valuesAreSame = false; break; } if (getUint32FromIndex(travelTimes, i) / 1000 != uncodedResponse.contents.travelTimes[i]) { print('TravelTimes is not same'); valuesAreSame = false; break; } if (getBoolValueAt(violatedBytes, i) != uncodedResponse.contents.violated[i]) { print('Violated is not same'); valuesAreSame = false; break; } if (getBoolValueAt(estimatedByDirectDistanceBytes, i) != uncodedResponse.contents.estimatedByDirectDistance[i]) { print('estimatedByDirectDistance is not same'); valuesAreSame = false; break; } } if(valuesAreSame){ print('Uncoded and encoded versions are same '); } };

Java

The following code shows an example in Java:

C#

The following code shows an example in C#: