Retrieving Features in the Route Response
This article discusses the usecase of calculating a route A route corresponds to a path of a vehicle through the underlying transport network. The main attributes of a route are the distance and the time that the vehicle travels along the path. and retrieving features in RouteResponse.
Benefits
Retrieves the features from the Feature Layers of every segment in the route list. This can be used for example to get information on traffic incidents on the route.
Prerequisites
Check if the following prerequisites are fulfilled before you start with the use case.
-
Installed and licensed PTV xRoute service.
-
Installed map includes Luxembourg by HERE with the Feature Layer theme PTV_TrafficIncidents.
Concepts
In this scenario different elements in the request are filled and all the features of route segments are returned.
For this purpose, in the request the SegmentResultFields of ResultFields is used.
In this SegmentResultFields
-
The boolean field
enabled
should be set totrue
-
featureThemeIds
contains a list of the Feature Layer themes for which the features are to be returned. These themes should belong to the feature layers declared in featureLayerProfile of RequestProfile.
The features are returned in SegmentAttributes with a list of features.
This last contains the themeId
and attributes
in key value pair form can be retrieved. This last lists all attributes of a segment. For each attribute its type (key) and value is provided via key-value pairs. See the documentation of the layer-specific attributes for more information on the type and the range of available attributes.
Restrictions
Only the themeId
and feature descriptions of already declared Feature Layer in the featureLayerProfile of RequestProfile can be used.
Programming Guide
The example below shows how to get the segment features in conjunction with the Feature Layer PTV_TrafficIncidents. Therefor the JavaScript bindings to the xRoute service API is used:
var referenceTime = "2019-01-31T12:00:00-04:00"; var featureLayerProfile = { "themes": [{ "enabled": true, "id": "PTV_TrafficIncidents" }] } var A = { "$type": "OffRoadWaypoint", "location": { "offRoadCoordinate": { "x": -71.55670166015626, "y": 41.99643418669205 } } }; var B = { "$type": "OffRoadWaypoint", "location": { "offRoadCoordinate": { "x": -71.5648126602173, "y": 42.00545957779332 } } }; var map = new L.Map('map', { center: [41.82, -71.412], zoom: 13 }); // Add tile layer to map new L.tileLayer.xserver(xServerUrl + '/services/rest/XMap/experimental/tile/{z}/{x}/{y}' + '?layers=background,transport,labels,PTV_TrafficIncidents' + '&timeConsideration=SNAPSHOT&referenceTime=' + encodeURIComponent(referenceTime) + '&showOnlyRelevantByTime=false' + '&contentType=JSON', { pane: "overlayPane", maxZoom: 20 }).addTo(map); var outputString = ""; var featureString = ""; function getMessages(features) { for (var j = 0; j < features.length; j++) { var featureAtt = features[j]; for (var k = 0; k < featureAtt.attributes.length; k++) { if(featureAtt.attributes[k].key == "message.text") { featureString = " "+ features[j].themeId + " has a message : "; featureString += '"' + featureAtt.attributes[k].value + '"'; break; } } } } //----- Route ----- function calculateSpecificRoute() { xroute.calculateRoute({ "requestProfile": { "featureLayerProfile": featureLayerProfile, "userLanguage" : "de" }, "waypoints": [A, B], "routeOptions": { "timeConsideration": { "$type": "ExactTimeConsiderationAtStart", "referenceTime": referenceTime } }, "resultFields": { "polyline": true, "segments": { "enabled": true, "descriptors": true, "roadAttributes": true, "featureThemeIds":["PTV_TrafficIncidents"] } }, "geometryOptions": { "responseGeometryTypes": ["GEOJSON"] } }, function(route, exception) { var geoJson = route.polyline.geoJSON; displayGeoJson(geoJson, '#2882C8'); for (var i = 0; i < route.segments.length; i++) { if (route.segments[i].type == "NETWORK_SEGMENT") { if(route.segments[i].attributes.features !== undefined) { getMessages(route.segments[i].attributes.features); } } if(featureString != "") { break; } } outputString += featureString; print(outputString); }); } 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()); }; calculateSpecificRoute();In the example above, a route is calculated and goes through the Traffic Incidents. The message of Traffic Incidents is displayed.