View on GitHub Sample viewer app
Find a route that reaches all stops without crossing any barriers.
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 sampleClick '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 worksRouteTask
with the URL to a Network Analysis route service.RouteParameters
for the service, and create the desired Stop
s and PolygonBarrier
s.routeParameters.setStops(routeStops)
and routeParameters.setPolygonBarriers(routeBarriers)
.returnStops
and returnDirections
to true
.findBestSequence
to true
to find the most optimal route.preserveFirstStop
to true
.preserveLastStop
to true
.routeTask.solveRouteAsync(routeParameters)
to get a RouteResult
.routeResult.routes()[0]
.This sample uses an Esri-hosted sample street network for San Diego.
barriers, best sequence, directions, maneuver, network analysis, routing, sequence, stop order, stops
Sample CodeRouteAroundBarriers.cpp RouteAroundBarriers.cpp RouteAroundBarriers.h RouteAroundBarriers.qml
Use dark colors for code blocks Copy
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// [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]
#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD
// sample headers
#include "RouteAroundBarriers.h"
// ArcGIS Maps SDK headers
#include "CompositeSymbol.h"
#include "DirectionManeuverListModel.h"
#include "Error.h"
#include "GeometryEngine.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "PictureMarkerSymbol.h"
#include "Point.h"
#include "Polygon.h"
#include "PolygonBarrier.h"
#include "Polyline.h"
#include "Route.h"
#include "RouteResult.h"
#include "RouteTask.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleRenderer.h"
#include "Stop.h"
#include "SymbolTypes.h"
#include "TextSymbol.h"
#include "Viewpoint.h"
// Qt headers
#include <QDir>
#include <QFuture>
#include <QUuid>
using namespace Esri::ArcGISRuntime;
namespace
{
const QUrl pinUrl("qrc:/Samples/Routing/RouteAroundBarriers/orange_symbol.png");
const QUrl routeTaskUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/Route");
}
RouteAroundBarriers::RouteAroundBarriers(QObject* parent /* = nullptr */):
QObject(parent),
m_map(new Map(BasemapStyle::ArcGISStreets, this)),
m_routeOverlay(new GraphicsOverlay(this)),
m_stopsOverlay(new GraphicsOverlay(this)),
m_barriersOverlay(new GraphicsOverlay(this))
{
// create symbols for displaying the barriers and the route line
m_barrierSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::DiagonalCross, Qt::red, this);
SimpleLineSymbol* routeLineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::blue, 3, this);
SimpleRenderer* routeRenderer = new SimpleRenderer(routeLineSymbol, this);
m_routeOverlay->setRenderer(routeRenderer);
m_pinSymbol = new PictureMarkerSymbol(pinUrl, this);
m_pinSymbol->setHeight(50);
m_pinSymbol->setWidth(50);
m_pinSymbol->setOffsetY(m_pinSymbol->height() / 2);
// create route task
m_routeTask = new RouteTask(routeTaskUrl, this);
}
RouteAroundBarriers::~RouteAroundBarriers() = default;
void RouteAroundBarriers::init()
{
// Register the map view for QML
qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
qmlRegisterType<RouteAroundBarriers>("Esri.Samples", 1, 0, "RouteAroundBarriersSample");
}
MapQuickView* RouteAroundBarriers::mapView() const
{
return m_mapView;
}
// Set the view (created in QML)
void RouteAroundBarriers::setMapView(MapQuickView* mapView)
{
if (!mapView || mapView == m_mapView)
return;
m_mapView = mapView;
m_mapView->setMap(m_map);
m_mapView->setViewpointAsync(Viewpoint(32.727, -117.1750, 40000));
// add the graphics overlays to the MapView
m_mapView->graphicsOverlays()->append(m_routeOverlay);
m_mapView->graphicsOverlays()->append(m_stopsOverlay);
m_mapView->graphicsOverlays()->append(m_barriersOverlay);
connectRouteSignals();
m_routeTask->load();
emit mapViewChanged();
}
void RouteAroundBarriers::connectRouteSignals()
{
connect(m_routeTask, &RouteTask::doneLoading, this, [this](const Error& loadError)
{
if (!loadError.isEmpty())
{
qDebug() << loadError.message() << loadError.additionalMessage();
}
m_routeTask->createDefaultParametersAsync().then(this, [this](const RouteParameters& defaultParameters)
{
m_routeParameters = defaultParameters;
// set flags to return stops and directions
m_routeParameters.setReturnStops(true);
m_routeParameters.setReturnDirections(true);
});
});
connect(m_mapView, &MapQuickView::mouseClicked, this, [this](QMouseEvent& e)
{
const Point clickedPoint = m_mapView->screenToLocation(e.position().x(), e.position().y());
if (m_addStops)
{
// add stop to list of stops
const Stop stopPoint(clickedPoint);
m_stopsList << stopPoint;
// create a marker symbol and graphics, and add the graphics to the graphics overlay
TextSymbol* textSymbol = new TextSymbol(QString::number(m_stopsList.size()), Qt::white, 16,
HorizontalAlignment::Center, VerticalAlignment::Bottom, this);
textSymbol->setOffsetY(m_pinSymbol->height() / 2);
CompositeSymbol* newStopSymbol = new CompositeSymbol(QList<Symbol*>{m_pinSymbol, textSymbol}, this);
Graphic* stopGraphic = new Graphic(clickedPoint, newStopSymbol, this);
m_stopsOverlay->graphics()->append(stopGraphic);
createAndDisplayRoute();
}
else if (m_addBarriers)
{
// add barrier to list
const Polygon barrierPolygon = GeometryEngine::buffer(clickedPoint, 500);
const PolygonBarrier barrier(barrierPolygon);
m_barriersList << barrier;
Graphic* barrierGraphic = new Graphic(barrierPolygon, m_barrierSymbol, this);
m_barriersOverlay->graphics()->append(barrierGraphic);
createAndDisplayRoute();
}
});
}
void RouteAroundBarriers::createAndDisplayRoute()
{
if (m_stopsList.size() > 1)
{
// clear the previous route, if it exists
if (m_routeOverlay)
m_routeOverlay->graphics()->clear();
// clear the directions list
if (m_directions)
{
delete m_directions;
m_directions = nullptr;
}
m_routeParameters.setStops(m_stopsList);
m_routeParameters.setPolygonBarriers(m_barriersList);
m_routeParameters.setFindBestSequence(m_findBestSequence);
m_routeParameters.setPreserveFirstStop(m_preserveFirstStop);
m_routeParameters.setPreserveLastStop(m_preserveLastStop);
m_routeTask->solveRouteAsync(m_routeParameters).then(this, [this](const RouteResult& routeResult)
{
if (routeResult.isEmpty())
return;
const Route route = std::as_const(routeResult).routes()[0];
const Geometry routeGeometry = route.routeGeometry();
Graphic* routeGraphic = new Graphic(routeGeometry, this);
m_routeOverlay->graphics()->append(routeGraphic);
m_directions = route.directionManeuvers(this);
emit directionsChanged();
});
}
}
void RouteAroundBarriers::clearRouteAndGraphics()
{
// clear stops from route parameters and stops list
m_routeParameters.clearStops();
m_stopsList.clear();
// clear barriers
m_routeParameters.clearPolygonBarriers();
m_barriersList.clear();
// clear directions list
if (m_directions)
{
delete m_directions;
m_directions = nullptr;
emit directionsChanged();
}
// delete graphics from overlay, then clear graphics overlays
for (GraphicsOverlay* overlay : *m_mapView->graphicsOverlays())
{
if (overlay)
{
for (Graphic* graphic : *overlay->graphics())
{
delete graphic;
}
overlay->graphics()->clear();
}
}
}
void RouteAroundBarriers::clearDirections()
{
if (m_directions)
{
delete m_directions;
m_directions = nullptr;
emit directionsChanged();
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4