Find closest facility to multiple incidents (service)

View inQMLC++
View on GitHub
Sample viewer app

Find routes from several locations to the respective closest facility.

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 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

  1. Create a ClosestFacilityTask using a URL from an online service.
  2. Get the default set of ClosestFacilityParameters from the task: closestFacilityTask.createDefaultParameters().
  3. Create ServiceFeatureTables for both facilities incidents.
  4. Add all facilities to the task parameters: closestFacilityParameters.setFacilitiesWithFeatureTable(facilitiesFeatureTable, parameters).
  5. Add all incidents to the task parameters: closestFacilityParameters.setIncidentsWithFeatureTable(incidentsFeatureTable, parameters).
  6. Get ClosestFacilityResult by solving the task with the provided parameters: closestFacilityTask.solveClosestFacility(closestFacilityParameters).
  7. Find the closest facility for each incident by iterating over the list of Incidents.
  8. 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.qmlFindClosestFacilityToMultipleIncidentsService.cppFindClosestFacilityToMultipleIncidentsService.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
// [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 2.6
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import Esri.Samples 1.0


Item {

    // add a mapView component
    MapView {
        id: view
        anchors.fill: parent

        // background for buttons
        Rectangle {
            anchors {
                margins: 5
                left: parent.left
                top: parent.top
            }
            width: childrenRect.width
            height: childrenRect.height
            color: "#000000"
            opacity: .70
            radius: 5

            // catch mouse signals from propagating to parent
            MouseArea {
                anchors.fill: parent
                onClicked: mouse.accepted = true
                onWheel: wheel.accepted = true
            }

            // column layout for solve and reset buttons
            ColumnLayout {
                Button {
                    id: solveButton
                    text: qsTr("Solve Routes")
                    Layout.margins: 2
                    Layout.fillWidth: true
                    enabled: closestFacilityModel.solveButton
                    onClicked: closestFacilityModel.solveRoute();
                }

                Button {
                    id: resetButton
                    text: qsTr("Reset")
                    Layout.margins: 2
                    Layout.fillWidth: true
                    enabled: closestFacilityModel.resetButton
                    onClicked: closestFacilityModel.resetGO();
                }
            }
        }
    }

    BusyIndicator {
        anchors.centerIn: parent
        running: closestFacilityModel.busy
    }

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

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