Route around barriers

View inQMLC++
View on GitHub
Sample viewer app

Find a route that reaches all stops without crossing any barriers.

screenshot

Use case

You can define barriers to avoid unsafe areas, for example flooded roads, when planning the most efficient route to evacuate a hurricane zone. When solving a route, barriers allow you to define portions of the road network that cannot be traversed. You could also use this functionality to plan routes when you know an area will be inaccessible due to a community activity like an organized race or a market night.

In some situations, it is further beneficial to find the most efficient route that reaches all stops, reordering them to reduce travel time. For example, a delivery service may target a number of drop-off addresses, specifically looking to avoid congested areas or closed roads, arranging the stops in the most time-effective order.

How to use the sample

Click 'Add stop' to add stops to the route. Click 'Add barrier' to add areas that can't be crossed by the route. Select 'Find best sequence' to allow stops to be re-ordered in order to find an optimal route. Select 'Preserve first stop' to preserve the first stop. Select 'Preserve last stop' to preserve the last stop.

How it works

  1. Construct a RouteTask with the URL to a Network Analysis route service.
  2. Get the default RouteParameters for the service, and create the desired Stops and PolygonBarriers.
  3. Add the stops and barriers to the route's parameters, routeParameters.setStops(routeStops) and routeParameters.setPolygonBarriers(routeBarriers).
  4. Set the returnStops and returnDirections to true.
  5. If the user will accept routes with the stops in any order, set findBestSequence to true to find the most optimal route.
  6. If the user has a definite start point, set preserveFirstStop to true.
  7. If the user has a definite final destination, set preserveLastStop to true.
  8. Call routeTask.solveRoute(routeParameters) to get a RouteResult.
  9. Get the first returned route by calling routeResult.routes()[0].
  10. Get the geometry from the route to display the route to the map.

Relevant API

  • DirectionManeuverListModel
  • PolygonBarrier
  • Route
  • RouteParameters
  • RouteResult
  • RouteTask
  • Stop

About the data

This sample uses an Esri-hosted sample street network for San Diego.

Tags

barriers, best sequence, directions, maneuver, network analysis, routing, sequence, stop order, stops

Sample Code

RouteAroundBarriers.qmlRouteAroundBarriers.cppRouteAroundBarriers.h
                                                                                                                                                                                                                                     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// [WriteFile Name=RouteAroundBarriers, Category=Routing]
// [Legal]
// Copyright 2020 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.Samples 1.0
import QtQuick.Layouts 1.11

Item {
    readonly property int checkBoxPadding: 20

    ButtonGroup {
        buttons: buttonsRow.children
    }

    // add a mapView component
    MapView {
        id: view
        anchors.fill: parent
        ColumnLayout {
            spacing: 0
            Layout.alignment: Qt.AlignTop

            Rectangle {
                id: backBox
                z: 1
                Layout.alignment: Qt.AlignLeft
                Layout.margins: 3
                Layout.bottomMargin: 0
                width: Qt.platform.os === "ios" || Qt.platform.os === "android" ? 200 : 300
                height: childrenRect.height
                color: "lightgrey"

                GridLayout {
                    id: grid
                    anchors.horizontalCenter: parent.horizontalCenter
                    rows: 4
                    columns: 1
                    rowSpacing: 10
                    columnSpacing: 2
                    Row {
                        id: buttonsRow
                        Layout.alignment: Qt.AlignHCenter
                        spacing: 5
                        padding: 5
                        Button {
                            id: stopButton
                            text: "Add stop"
                            checked: true
                            highlighted: checked
                            onClicked: {
                                checked = true;
                            }
                            onCheckedChanged: {
                                sampleModel.addStops = checked;
                            }
                        }
                        Button {
                            id: barrierButton
                            text: "Add barrier"
                            highlighted: checked;
                            onClicked: {
                                checked = true;
                            }
                            onCheckedChanged: {
                                sampleModel.addBarriers = checked;
                            }
                        }
                    }

                    Column {
                        spacing: 2

                        CheckBox {
                            id: bestSequenceBox
                            text: "Find best sequence"
                            onCheckedChanged: {
                                sampleModel.findBestSequence = checked;
                                sampleModel.createAndDisplayRoute();
                            }
                        }
                        CheckBox {
                            id: firstStopBox
                            text: "Preserve first stop"
                            leftPadding: checkBoxPadding
                            enabled: bestSequenceBox.checked
                            onCheckedChanged: {
                                sampleModel.preserveFirstStop = checked;
                                sampleModel.createAndDisplayRoute();
                            }
                        }
                        CheckBox {
                            id: lastStopBox
                            text: "Preserve last stop"
                            leftPadding: checkBoxPadding
                            enabled: bestSequenceBox.checked
                            onCheckedChanged: {
                                sampleModel.preserveLastStop = checked;
                                sampleModel.createAndDisplayRoute();
                            }
                        }
                    }

                    Row {
                        Layout.alignment: Qt.AlignHCenter
                        Button {
                            id: resetButton
                            text: "Reset"
                            onClicked: {
                                sampleModel.clearRouteAndGraphics();
                                sampleModel.clearDirections();
                            }
                        }
                    }

                    Row {
                        Layout.alignment: Qt.AlignHCenter
                        Button {
                            text: "Hide directions"
                            onClicked: {
                                if (text === "Hide directions") {
                                    directionsView.delegate = blankDelegate;
                                    text = "Show directions";
                                } else {
                                    directionsView.delegate = directionDelegate;
                                    text = "Hide directions";
                                }
                            }
                        }
                    }
                }
            }

            // Create window for displaying the route directions
            Rectangle {
                id: directionWindow
                Layout.alignment: Qt.AlignBottom
                Layout.topMargin: 0
                visible: true
                Layout.preferredWidth: backBox.width
                Layout.preferredHeight: 300
                Layout.margins: 3
                color: "lightgrey"

                ScrollView {
                    anchors.fill: parent

                    ListView {
                        id: directionsView
                        anchors {
                            fill: parent
                            margins: 5
                        }
                        header: Text {
                            text: "Directions:"
                            font.pixelSize: 22
                            bottomPadding: 8
                        }

                        // set the model to the DirectionManeuverListModel returned from the route
                        model: sampleModel.directions
                        delegate: directionDelegate
                    }
                }
            }
        }
    }

    Component {
        id: blankDelegate
        Rectangle {
            width: parent.width
            height: 35
            color: directionWindow.color
        }
    }

    Component {
        id: directionDelegate
        Rectangle {
            id: rect
            width: parent.width
            height: 35
            color: directionWindow.color

            Rectangle {
                anchors {
                    top: parent.top;
                    left: parent.left;
                    right: parent.right;
                    topMargin: -8
                    leftMargin: 20
                    rightMargin: 20
                }
                color: "darkgrey"
                height: 1
            }

            Text {
                text: directionText
                anchors {
                    fill: parent
                    leftMargin: 5
                }
                elide: Text.ElideRight
                font.pixelSize: 14
            }
        }
    }

    // Declare the C++ instance which creates the scene etc. and supply the view
    RouteAroundBarriersSample {
        id: sampleModel
        mapView: view
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.