Find closest facility to multiple incidents (service)
View inQMLC++View on GitHubSample viewer app
Find routes from several locations to the respective closest facility.
Use case
Quickly and accurately determining the most efficient route between a location and a facility is a frequently encountered task. For example, a city's fire department may need to know which fire stations in the vicinity offer the quickest routes to multiple fires. Solving for the closest fire station to the fire's location using an impedance of "travel time" would provide this information.
How to use the sample
Click 'Solve Routes' to solve and display the route from each incident (fire) to the nearest facility (fire station). Click 'Reset' to clear the results.
How it works
Create a ClosestFacilityTask using a URL from an online service.
Get the default set of ClosestFacilityParameters from the task: closestFacilityTask.createDefaultParameters().
Create ServiceFeatureTables for both facilities incidents.
Add all facilities to the task parameters: closestFacilityParameters.setFacilitiesWithFeatureTable(facilitiesFeatureTable, parameters).
Add all incidents to the task parameters: closestFacilityParameters.setIncidentsWithFeatureTable(incidentsFeatureTable, parameters).
Get ClosestFacilityResult by solving the task with the provided parameters: closestFacilityTask.solveClosestFacility(closestFacilityParameters).
Find the closest facility for each incident by iterating over the list of Incidents.
Display the route as a Graphic using the closestFacilityRoute.routeGeometry().
Relevant API
ClosestFacilityParameters
ClosestFacilityResult
ClosestFacilityRoute
ClosestFacilityTask
Facility
Graphic
GraphicsOverlay
Incident
Tags
incident, network analysis, route, search
Sample Code
FindClosestFacilityToMultipleIncidentsService.qml
Use dark colors for code blocksCopy
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
// [WriteFile Name=FindClosestFacilityToMultipleIncidentsService, Category=Routing]// [Legal]// Copyright 2019 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
import Esri.ArcGISRuntime
import QtQuick.Controls
import QtQuick.Layouts
Rectangle {
id: rootRectangleclip: truewidth: 800height: 600property bool busy: falseproperty var mySetViewpointTaskIdproperty var facilityParams: nullsignal bothDoneLoading(var facilityLoadStatus, var incidentLoadStatus)
property bool layersLoaded : (facilitiesLayer.loadStatus === Enums.LoadStatusLoaded) && (incidentsLayer.loadStatus === Enums.LoadStatusLoaded)
MapView {
id: mapViewanchors.fill: parentComponent.onCompleted: {
// Set the focus on MapView to initially enable keyboard navigation forceActiveFocus();
}
Map {
Basemap {
initStyle: Enums.BasemapStyleArcGISStreetsRelief
}
FeatureLayer {
id: facilitiesLayerServiceFeatureTable {
id: facilitiesFeatureTableurl: "https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/ArcGIS/rest/services/San_Diego_Facilities/FeatureServer/0" }
renderer: SimpleRenderer {
symbol: facilitySymbol
}
onLoadStatusChanged: setViewpointGeometry();
}
FeatureLayer {
id: incidentsLayerServiceFeatureTable {
id: incidentsFeatureTableurl: "https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/ArcGIS/rest/services/San_Diego_Incidents/FeatureServer/0" }
renderer: SimpleRenderer {
symbol: incidentSymbol
}
onLoadStatusChanged: setViewpointGeometry();
}
Component.onCompleted: busy = true;
}
GraphicsOverlay {
id: resultsOverlay }
Rectangle {
anchors {
margins: 5left: parent.left
top: parent.top
}
width: childrenRect.width
height: childrenRect.height
color: "#000000"opacity: .70radius: 5// catch mouse signals from propagating to parentMouseArea {
anchors.fill: parentonClicked: mouse => mouse.accepted = trueonWheel: wheel => wheel.accepted = true }
ColumnLayout {
Button {
id: solveButtontext: qsTr("Solve Routes")
Layout.margins: 2Layout.fillWidth: trueenabled: falseonClicked: {
busy = true;
solveButton.enabled = false;
task.solveClosestFacility(facilityParams);
}
}
Button {
id: resetButtontext: qsTr("Reset")
Layout.margins: 2Layout.fillWidth: trueenabled: falseonClicked: {
resultsOverlay.graphics.clear();
solveButton.enabled = true;
resetButton.enabled = false;
}
}
}
}
}
PictureMarkerSymbol {
id: facilitySymbolurl: "https://static.arcgis.com/images/Symbols/SafetyHealth/FireStation.png"height: 30width: 30 }
PictureMarkerSymbol {
id: incidentSymbolurl: "https://static.arcgis.com/images/Symbols/SafetyHealth/esriCrimeMarker_56_Gradient.png"height: 30width: 30 }
SimpleLineSymbol {
id: routeSymbolstyle: Enums.SimpleLineSymbolStyleSolid
color: "blue"width: 2.0 }
ClosestFacilityTask {
id: taskurl: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ClosestFacility"onLoadStatusChanged: {
if (loadStatus !== Enums.LoadStatusLoaded)
return;
task.createDefaultParameters();
}
onCreateDefaultParametersStatusChanged: {
if (createDefaultParametersStatus !== Enums.TaskStatusCompleted)
return;
busy = false;
solveButton.enabled = true;
facilityParams = createDefaultParametersResult;
facilityParams.setFacilitiesWithFeatureTable(facilitiesFeatureTable, params);
facilityParams.setIncidentsWithFeatureTable(incidentsFeatureTable, params);
}
onSolveClosestFacilityStatusChanged: {
if (solveClosestFacilityStatus !== Enums.TaskStatusCompleted)
return;
// finding the closest facility for each incident to create a route graphic between each pairfor (let incidentIndex = 0; incidentIndex < incidentsFeatureTable.numberOfFeaturesAsInt; incidentIndex++) {
const closestFacilityIndex = solveClosestFacilityResult.rankedFacilityIndexes(incidentIndex)[0];
const route = solveClosestFacilityResult.route(closestFacilityIndex, incidentIndex);
const routeGraphic = ArcGISRuntimeEnvironment.createObject("Graphic", { geometry: route.routeGeometry, symbol: routeSymbol});
resultsOverlay.graphics.append(routeGraphic);
}
busy = false;
resetButton.enabled = true;
}
onErrorChanged: console.log(error.message);
}
QueryParameters {
id: paramswhereClause: "1=1" }
BusyIndicator {
anchors.centerIn: parentrunning: busy
}
functionsetViewpointGeometry() {
// proceed only if both layers are loadedif ((facilitiesLayer.loadStatus !== Enums.LoadStatusLoaded) || (incidentsLayer.loadStatus !== Enums.LoadStatusLoaded))
return;
// return if set viewpoint has been executed alreadyif (mySetViewpointTaskId)
return;
mySetViewpointTaskId = mapView.setViewpointGeometryAndPadding(GeometryEngine.unionOf(facilitiesLayer.fullExtent, incidentsLayer.fullExtent), 20);
task.load();
}
}