Considering custom Feature Layers with high performance routing
The custom Feature Layers can be used when generating a high performance routing network.
The example below shows the creation of a high performance routing network with a custom Feature Layer. The high performance routing network is then used to create a distance matrix and compare results with xRoute. Therefore, the JavaScript bindings to the xRoute, xData and xDima service APIs are used:
var A = { "$type": "OffRoadWaypoint", "location": { "offRoadCoordinate": { "x": 6.111967, "y": 49.614536 } } }; var B = { "$type": "OffRoadWaypoint", "location": { "offRoadCoordinate": { "x": 6.106796, "y": 49.615252 } } }; var startLocation = { "$type": "OffRoadRouteLocation", "offRoadCoordinate": { "x": 6.111967, "y": 49.614536 } }; var destinationLocation = { "$type": "OffRoadRouteLocation", "offRoadCoordinate": { "x": 6.106796, "y": 49.615252 } }; var nearPoint = { "x": 6.109486, "y": 49.614904 }; var outputString = ""; var map = new L.Map('map', { center: [49.615525, 6.109560], zoom: 16 }); // Add tile layer to map new L.tileLayer.xserver(xServerUrl + '/services/rest/XMap/experimental/tile/{z}/{x}/{y}' + '?layers=background,transport,labels,PTV_TruckAttributes' + '&contentType=JSON', { pane: "overlayPane", maxZoom: 20, }).addTo(map); // Display the point L.circle([49.615525, 6.109560], { radius: 3 }).addTo(map); //----- Find segment near coordinate ----- function getSegmentToBlock(searchCoord) { var foundSegmentId; xdata.getSegments({ "$type": "SegmentsByCoordinateRequest", "resultFields": { "polyline": true, "descriptors": true }, "coordinate": searchCoord, "geometryOptions": { "responseGeometryTypes": ["GEOJSON"] } }, function (result, exception) { var geoJson = result.segments[0].polyline.geoJSON; displayGeoJson(geoJson, '#481111'); foundSegmentId = result.segments[0].id; }); return foundSegmentId; } function displayGeoJson(geoJson, color) { var jsonObject = JSON.parse(geoJson); var geoJsonLayer = new L.GeoJSON(jsonObject, { style: { color: color, weight: 8 } }).addTo(map); map.fitBounds(geoJsonLayer.getBounds()); }; function cleanup(hprId) { xdata.deleteHighPerformanceRoutingNetwork({ "id": hprId }); }; //----- Create FeatureLayer ----- function createCustomFeatureLayer(segmentIdToBlock) { var createdFeatureLayer; xdata.createFeatureLayer({ "themeId": "PTV_RoadAttributes", "features": [{ "segmentIds": [segmentIdToBlock], "descriptions": [{ "attributes": [{ "key": "opening", "value": 0 }] }] }] }, function (result, exception) { createdFeatureLayer = result.binaryFeatureLayer; }); return createdFeatureLayer; }; //----- HPR ----- function createHprWithBinaryFeatureLayer(binaryFeatureLayer) { var hprId; var options = { "geographicRestrictions": { "allowedCountries": [ "LU" ] }, "binaryFeatureLayer": binaryFeatureLayer }; job = xdata.startCreateHighPerformanceRoutingNetwork({ "label": "Luxembourg with car, custom road blocking", "storedProfile": "car", "highPerformanceRoutingNetworkOptions": options }); result = waitForJobToFinish(job); if (result.status == true) { id = result.content; xdata.listHighPerformanceRoutingNetworks({ "storedProfile": "car", "returnOnlyMatchingNetworks": true, "highPerformanceRoutingNetworkOptions": options, "resultFields": { "profiles": true, "highPerformanceRoutingNetworkOptions": true } }, listCompleted); } else if (result.status == false) { // no-op } else { print("Unknown error during high-performance routing network creation."); } function listCompleted(response, exception) { if (response.highPerformanceRoutingNetworkInformation) { hprId = response.highPerformanceRoutingNetworkInformation[0].highPerformanceRoutingNetworkDescription.id; print("Found a suitable high-performance routing network with id " + network.highPerformanceRoutingNetworkDescription.id + "! You can use high-performance routing with your current profile!"); } else { print("No suitable high-performance routing network available. Consider creating one!"); } } function waitForJobToFinish(job, exception) { do { job = xdata.watchJob({ "id": job.id }); } while (job.status != "SUCCEEDED" && job.status != "FAILED"); try { fetchResult = xdata.fetchHighPerformanceRoutingNetworkResponse(job.id); return { "status": true, "content": fetchResult.id }; } catch (e) { return { "status": false, "content": "" }; } } return hprId; }; //----- Create distance matrix ----- var dimaDistance = 0; var dimaTravelTime = 0; async function createAndGetDistanceMatrix(hprId) { let promise = new Promise((resolve, reject) => { xdima.createDistanceMatrix({ "$type": "CreateAndGetDistanceMatrixRequest", "startLocations": [startLocation], "destinationLocations": [destinationLocation], "distanceMatrixOptions": { "highPerformanceRoutingNetworkId": hprId, "routingType": "HIGH_PERFORMANCE_ROUTING" }, "resultFields": { "distances": true, "travelTimes": true } }, function (result, exception) { if (result) { dimaDistance = result.contents.distances[0]; dimaTravelTime = result.contents.travelTimes[0]; resolve(result); } else { reject(null); } }); }); await promise; }; //----- Route ----- var xRouteDistance = 0; var xRouteTravelTime = 0; var xRoutePolyline = null function calculateRoute(hprId) { var routeResult; xroute.calculateRoute({ "waypoints": [A, B], "routeOptions": { "highPerformanceRoutingNetworkId": hprId, "routingType": "HIGH_PERFORMANCE_ROUTING" }, "resultFields": { "polyline": true }, "geometryOptions": { "responseGeometryTypes": ["GEOJSON"] } }, function (route, exception) { xRoutePolyline = route.polyline.geoJSON; xRouteDistance = route.distance; xRouteTravelTime = route.travelTime; }); }; var segmentIds = getSegmentToBlock(nearPoint); var newBinaryFeatureLayer = createCustomFeatureLayer(segmentIds); hprId = createHprWithBinaryFeatureLayer(newBinaryFeatureLayer); createAndGetDistanceMatrix(hprId); calculateRoute(hprId); cleanup(hprId); //----- compare Dima & xRoute results ----- var haveTheSameResults = xRouteDistance === dimaDistance && xRouteTravelTime === dimaTravelTime; if (haveTheSameResults) { print("xDima and xRoute have the same results."); displayGeoJson(xRoutePolyline, '#2882C8'); } else { print("An error occurred."); }