Find closest facility to an incident (interactive)

View inC++QMLView on GitHubSample viewer app

Find a route to the closest facility from a location.

screenshot

Use case

Quickly and accurately determining the most efficient route between a location and a facility is a frequently encountered task. For example, a paramedic may need to know which hospital in the vicinity offers the possibility of getting an ambulance patient critical medical care in the shortest amount of time. Solving for the closest hospital to the ambulance's location using an impedance of "travel time" would provide this information.

How to use the sample

Click near any of the hospitals and a route will be displayed from that clicked location to the nearest hospital.

How it works

  1. Create a ClosestFacilityTask using an Url from an online service.
  2. Get a ClosestFacilityParameters from this task, using createDefaultParameters.
  3. Add a list of facilities to the task parameters, using ClosestFacilityParameters.setFacilities.
  4. Add an incident to the parameters, using ClosestFacilityParameters.setIncidents(.
  5. Get the ClosestFacilityResult from solving the task with parameters: , ClosestFacilityTask.solveClosestFacility.
  6. Get the ranked list of the indices of the closest facilities to the incident, ClosestFacilityResult.rankedFacilities.
  7. Get the index of the closest facility (e.g. the first index in the ranked list).
  8. Find the closest facility route, ClosestFacilityResult.route.
  9. Display the route on the MapView:
  • create a Graphic from the route geometry, route.routeGeometry.
  • add graphic to GraphicsOverlay which is attached to the mapview.

Relevant API

  • ClosestFacilityParameters
  • ClosestFacilityResult
  • ClosestFacilityRoute
  • ClosestFacilityTask
  • Facility
  • Graphic
  • GraphicsOverlay
  • Incident
  • MapView

Tags

incident, network analysis, route, search

Sample Code

ClosestFacility.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
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
254
255
256
257
258
259
260
// [WriteFile Name=FindClosestFacilityToAnIncidentInteractive, Category=Routing]
// [Legal]
// Copyright 2017 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.ArcGISRuntime 100.15

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    property bool busy: false
    property string message: ""
    property var facilities: []
    property var facilityParams: null

    // Map view UI presentation at top
    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISStreets
            }

            initialViewpoint: ViewpointCenter {
                Point {
                    x: -13041154
                    y: 3858170
                    spatialReference: SpatialReference { wkid: 3857 }
                }
                targetScale: 1e5
            }

            onLoadStatusChanged: {
                createFacilities();
                task.load();
            }
        }

        GraphicsOverlay {
            id: facilitiesOverlay

            SimpleRenderer {
                PictureMarkerSymbol {
                    url: "https://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png"
                    height: 30
                    width: 30
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3042129900625112E7
                    y: 3860127.9479775648
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3042129900625112E7
                    y: 3860127.9479775648
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3042193400557665E7
                    y: 3862448.873041752
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3046882875518233E7
                    y: 3862704.9896770366
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3040539754780494E7
                    y: 3862924.5938606677
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3042571225655518E7
                    y: 3858981.773018156
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3039784633928463E7
                    y: 3856692.5980474586
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }

            Graphic {
                geometry: Point {
                    x: -1.3049023883956768E7
                    y: 3861993.789732541
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }
        }

        GraphicsOverlay {
            id: resultsOverlay
        }

        onMouseClicked: {
            if (busy === true)
                return;

            if (facilityParams === null)
                return;

            resultsOverlay.graphics.clear();

            const incidentGraphic = ArcGISRuntimeEnvironment.createObject("Graphic", {
                                                                              geometry: mouse.mapPoint,
                                                                              symbol: incidentSymbol
                                                                          });
            resultsOverlay.graphics.append(incidentGraphic);

            solveRoute(mouse.mapPoint);
        }
    }

    SimpleMarkerSymbol {
        id: incidentSymbol
        style: Enums.SimpleMarkerSymbolStyleCross
        color: "black"
        size: 20
    }

    SimpleLineSymbol {
        id: routeSymbol
        style: Enums.SimpleLineSymbolStyleSolid
        color: "blue"
        width: 2.0
    }

    ClosestFacilityTask {
        id: task
        url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ClosestFacility"

        onLoadStatusChanged: {
            if (loadStatus !== Enums.LoadStatusLoaded)
                return;

            setupRouting();
        }

        onCreateDefaultParametersStatusChanged: {
            if (createDefaultParametersStatus !== Enums.TaskStatusCompleted)
                return;

            busy = false;
            facilityParams = createDefaultParametersResult;
            facilityParams.setFacilities(facilities);
        }

        onSolveClosestFacilityStatusChanged: {
            if (solveClosestFacilityStatus !== Enums.TaskStatusCompleted)
                return;

            busy = false;

            if (solveClosestFacilityResult === null || solveClosestFacilityResult.error)
                message = "Incident not within San Diego Area!";

            const rankedList = solveClosestFacilityResult.rankedFacilityIndexes(0);
            const closestFacilityIdx = rankedList[0];

            const incidentIndex = 0; // 0 since there is just 1 incident at a time
            const route = solveClosestFacilityResult.route(closestFacilityIdx, incidentIndex);

            const routeGraphic = ArcGISRuntimeEnvironment.createObject("Graphic", {
                                                                           geometry: route.routeGeometry,
                                                                           symbol: routeSymbol
                                                                       });
            resultsOverlay.graphics.append(routeGraphic);
        }

        onErrorChanged: message = error.message;
    }

    BusyIndicator {
        anchors.centerIn: parent
        running: busy
    }

    Dialog {
        modal: true
        x: Math.round(parent.width - width) / 2
        y: Math.round(parent.height - height) / 2
        standardButtons: Dialog.Ok
        title: "Route Error"
        visible: text.length > 0
        property alias text : textLabel.text
        Text {
            id: textLabel
            text: message
        }
    }

    function createFacilities() {
        facilitiesOverlay.graphics.forEach(graphic => facilities.push(ArcGISRuntimeEnvironment.createObject("Facility", {
                                                                                                                geometry: graphic.geometry
                                                                                                            })));
    }


    function setupRouting() {
        busy = true;
        message = "";
        task.createDefaultParameters();
    }

    function solveRoute(incidentPoint) {
        const incident = ArcGISRuntimeEnvironment.createObject("Incident", {geometry: incidentPoint});
        facilityParams.setIncidents( [ incident ] );

        busy = true;
        message = "";
        task.solveClosestFacility(facilityParams);
    }
}

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