Calculating a route with prohibited segments by intersecting polylines
This use-case describes how to calculate a routeA 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. with prohibited segments by intersecting polylinesA polyline is a continuous line composed of one or more line segments given as a set of tuples with x,y and optional z coordinates..
Benefits
Using the xRoute API makes it easy to obtain routes 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. avoiding certain segments by defining a list of polylines A polyline is a continuous line composed of one or more line segments given as a set of tuples with x,y and optional z coordinates..
Prerequisites
Please ensure following prerequisites are fulfilled before you start with the use case:
- Installed and licensed PTV xRoute service
Programming Guide
The following xRoute example shows how to compute route avoiding to pass through a certain segment.
To apply the restriction, we use the GeographicRestrctions options in the route request. In particular, these options offer the possibility to specify a list of polylines via the attribute prohibitedSegmentsByIntersectingPolylines from which every segment intersected are prohibited from the routing.
var map = new L.Map('map', { center: [49.591270, 6.124217], zoom: 17 }); // Add tile layer to map var tileUrl = xServerUrl + '/services/rest/XMap/tile/{z}/{x}/{y}'; var tileLayer = new L.TileLayer(tileUrl, { minZoom: 3, maxZoom: 18, noWrap: true }).addTo(map); var outputString = ''; var start = { "x": 6.1208, "y": 49.5923 }; var destination = { "x": 6.1236, "y": 49.5913 }; var intersectingLine = [ [49.5918, 6.1218], [49.5921, 6.1222] ]; function calculateSpecificRoute(withIntersectingPolyline) { let request = { "waypoints" : [{ "$type": "OffRoadWaypoint", "location": { "offRoadCoordinate": start } }, { "$type": "OffRoadWaypoint", "location": { "offRoadCoordinate": destination } }], "resultFields": { "polyline": true }, "geometryOptions": { "responseGeometryTypes": ["GEOJSON"] } }; if (withIntersectingPolyline) { request.routeOptions = { "geographicRestrictions": { "prohibitedSegmentsByIntersectingPolylines": [{ "plain": { "$type": "Polyline", "polyline": [{ "x": intersectingLine[0][1], "y": intersectingLine[0][0] }, { "x": intersectingLine[1][1], "y": intersectingLine[1][0] }] } }] } }; } xroute.calculateRoute(request, function(route, exception) { var geoJson = route.polyline.geoJSON; if (withIntersectingPolyline) { displayGeoJson(geoJson, '#ffa225', 4, true); outputString += 'restricted route by intersecting polyline: ' + route.distance + 'm'; } else { displayGeoJson(geoJson, '#2882C8', 12, false); outputString += 'standard route: ' + route.distance + 'm'; } }); }; function displayGeoJson(geoJson, color, weight, zoomOn) { var jsonObject = JSON.parse(geoJson); var geoJsonLayer = new L.GeoJSON(jsonObject, { style: { color: color, weight: weight } }).addTo(map); if (zoomOn) { map.fitBounds(geoJsonLayer.getBounds()); } }; calculateSpecificRoute(false); outputString += "; "; calculateSpecificRoute(true); print(outputString); new L.circle([start.y, start.x], { color: 'green', fillOpacity: 0.7, radius: 3 }).addTo(map); new L.circle([destination.y, destination.x], { color: 'red', fillOpacity: 0.7, radius: 3 }).addTo(map); new L.polyline(intersectingLine, {color: 'red'}).addTo(map);In this example, the blue route is the best route without any restrictions to reach the destination.
The orange route has been calculated using geographic restrictions containing the intersecting line from which the segments are prohibited. As a result a detour is observed.