Find closest facility to multiple incidents (service)

View on GitHubSample viewer app

Find routes from several locations to the respective closest facility.

Image of find closest facility to multiple incidents service

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

Tap the 'Solve Routes' button to solve and display the route from each incident (fire) to the nearest facility (fire station).

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.createDefaultParametersAsync().get().
  3. Build a list of all Facilities and Incidents:
  • Create a FeatureTable using ServiceFeatureTable(Uri).
  • Query the FeatureTable for all Features using queryFeaturesAsync(queryParameters).
  • Iterate over the result and add each Feature to the List, instantiating the feature as a Facility or Incident.
  1. Add a list of all facilities to the task parameters: closestFacilityParameters.setFacilities(facilitiesList).
  2. Add a list of all incidents to the task parameters: closestFacilityParameters.setIncidents(incidentsList).
  3. Get ClosestFacilityResult by solving the task with the provided parameters: closestFacilityTask.solveClosestFacilityAsync(closestFacilityParameters).
  4. Find the closest facility for each incident by iterating over the list of Incidents.
  5. Display the route as a Graphic using the closestFacilityRoute.getRouteGeometry().

Relevant API

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

Tags

incident, network analysis, route, search

Sample Code

MainActivity.java
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
/*
 *  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.
 */

package com.esri.arcgisruntime.sample.findclosestfacilitytomultipleincidentsservice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.data.QueryParameters;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import com.esri.arcgisruntime.tasks.networkanalysis.ClosestFacilityParameters;
import com.esri.arcgisruntime.tasks.networkanalysis.ClosestFacilityResult;
import com.esri.arcgisruntime.tasks.networkanalysis.ClosestFacilityRoute;
import com.esri.arcgisruntime.tasks.networkanalysis.ClosestFacilityTask;
import com.esri.arcgisruntime.tasks.networkanalysis.Facility;
import com.esri.arcgisruntime.tasks.networkanalysis.Incident;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private MapView mMapView;
  // objects that implement Loadable must be class fields to prevent being garbage collected before loading
  private ClosestFacilityTask mClosestFacilityTask;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // get a reference to the solve routes button and disable it
    Button solveRoutesButton = findViewById(R.id.solveRoutesButton);
    solveRoutesButton.setEnabled(false);

    // get a reference to the map view
    mMapView = findViewById(R.id.mapView);

    // create a ArcGISMap with a Basemap instance with an Imagery base layer
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS_RELIEF);
    mMapView.setMap(map);

    // create a graphics overlay and add it to the map
    GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
    mMapView.getGraphicsOverlays().add(graphicsOverlay);

    // create Symbols for displaying facilities
    PictureMarkerSymbol facilitySymbol = new PictureMarkerSymbol(getString(R.string.fire_station_symbol_url));
    facilitySymbol.setHeight(30);
    facilitySymbol.setWidth(30);
    PictureMarkerSymbol incidentSymbol = new PictureMarkerSymbol(getString(R.string.crime_symbol_url));
    incidentSymbol.setHeight(30);
    incidentSymbol.setWidth(30);

    // create a line symbol to mark the route
    SimpleLineSymbol simpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 5.0f);

    // create a closest facility task from a network analysis service
    mClosestFacilityTask = new ClosestFacilityTask(this,
        getString(R.string.san_diego_network_analysis_service_url));

    // create a table for facilities using the feature service
    FeatureTable facilitiesFeatureTable = new ServiceFeatureTable(getString(R.string.san_diego_facilities_service_url));
    // create a feature layer from the table, apply facilities icon
    FeatureLayer facilitiesFeatureLayer = new FeatureLayer(facilitiesFeatureTable);
    facilitiesFeatureLayer.setRenderer(new SimpleRenderer(facilitySymbol));
    // create a table for incidents using the feature service
    FeatureTable incidentsFeatureTable = new ServiceFeatureTable(getString(R.string.san_diego_incidents_service_url));
    // create a feature layer from the table, apply incident icon
    FeatureLayer incidentsFeatureLayer = new FeatureLayer(incidentsFeatureTable);
    incidentsFeatureLayer.setRenderer(new SimpleRenderer(incidentSymbol));

    // add the layers to the map
    map.getOperationalLayers().addAll(Arrays.asList(facilitiesFeatureLayer, incidentsFeatureLayer));

    // create the list to store the facilities
    List<Facility> facilities = new ArrayList<>();
    // create the list to store the incidents
    List<Incident> incidents = new ArrayList<>();

    // wait for the feature layers to load to retrieve the facilities and incidents
    facilitiesFeatureLayer.addDoneLoadingListener(() -> incidentsFeatureLayer.addDoneLoadingListener(() -> {
          if (facilitiesFeatureLayer.getLoadStatus() == LoadStatus.LOADED
              && incidentsFeatureLayer.getLoadStatus() == LoadStatus.LOADED) {
            // zoom to the extent of the combined feature layers
            Envelope fullFeatureLayerExtent = GeometryEngine
                .combineExtents(facilitiesFeatureLayer.getFullExtent(), incidentsFeatureLayer.getFullExtent());
            mMapView.setViewpointGeometryAsync(fullFeatureLayerExtent, 90);
            // create query parameters to select all features
            QueryParameters queryParameters = new QueryParameters();
            queryParameters.setWhereClause("1=1");
            // retrieve a list of all facilities
            ListenableFuture<FeatureQueryResult> result = facilitiesFeatureTable.queryFeaturesAsync(queryParameters);
            result.addDoneListener(() -> {
              try {
                FeatureQueryResult facilitiesResult = result.get();
                // add the found facilities to the list
                for (Feature facilityFeature : facilitiesResult) {
                  // since we know our feature layer only contains point features, we can cast them as Point in order to create a Facility
                  facilities.add(new Facility((Point) facilityFeature.getGeometry()));
                }
              } catch (InterruptedException | ExecutionException e) {
                String error = "Error retrieving list of facilities: " + e.getMessage();
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                Log.e(TAG, error);
              }
            });
            // retrieve a list of all incidents
            ListenableFuture<FeatureQueryResult> incidentsQueryResult = incidentsFeatureTable
                .queryFeaturesAsync(queryParameters);
            incidentsQueryResult.addDoneListener(() -> {
              try {
                FeatureQueryResult incidentsResult = incidentsQueryResult.get();
                // add the found incidents to the list
                for (Feature incidentFeature : incidentsResult) {
                  // since we know our feature layer only contains point features, we can cast them as Point in order to create an Incident
                  incidents.add(new Incident((Point) incidentFeature.getGeometry()));
                }
              } catch (InterruptedException | ExecutionException e) {
                String error = "Error retrieving list of incidents: " + e.getMessage();
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                Log.e(TAG, error);
              }
            });
            // enable the 'solve routes' button
            solveRoutesButton.setEnabled(true);
            // resolve button press
            solveRoutesButton.setOnClickListener(v -> {
              // disable the 'solve routes' button and show the progress indicator
              solveRoutesButton.setEnabled(false);
              // start the routing task
              mClosestFacilityTask.loadAsync();
              mClosestFacilityTask.addDoneLoadingListener(() -> {
                if (mClosestFacilityTask.getLoadStatus() == LoadStatus.LOADED) {
                  try {
                    // create default parameters for the task and add facilities and incidents to parameters
                    ClosestFacilityParameters closestFacilityParameters = mClosestFacilityTask
                        .createDefaultParametersAsync().get();
                    closestFacilityParameters.setFacilities(facilities);
                    closestFacilityParameters.setIncidents(incidents);
                    // solve closest facilities
                    try {
                      // use the task to solve for the closest facility
                      ListenableFuture<ClosestFacilityResult> closestFacilityTaskResult = mClosestFacilityTask
                          .solveClosestFacilityAsync(closestFacilityParameters);
                      closestFacilityTaskResult.addDoneListener(() -> {
                        try {
                          ClosestFacilityResult closestFacilityResult = closestFacilityTaskResult.get();
                          // find the closest facility for each incident
                          for (int i = 0; i < incidents.size(); i++) {
                            // get the index of the closest facility to incident
                            Integer closestFacilityIndex = closestFacilityResult.getRankedFacilityIndexes(i).get(0);
                            // get the route to the closest facility
                            ClosestFacilityRoute closestFacilityRoute = closestFacilityResult
                                .getRoute(closestFacilityIndex, i);
                            // display the route on the graphics overlay
                            graphicsOverlay.getGraphics()
                                .add(new Graphic(closestFacilityRoute.getRouteGeometry(), simpleLineSymbol));
                          }
                        } catch (ExecutionException | InterruptedException ex) {
                          String error = "Error getting the closest facility task result: " + ex.getMessage();
                          Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                          Log.e(TAG, error);
                        }
                      });
                    } catch (Exception ex) {
                      String error = "Error solving the closest facility task: " + ex.getMessage();
                      Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                      Log.e(TAG, error);
                    }
                  } catch (InterruptedException | ExecutionException ex) {
                    String error = "Error getting default route parameters: " + ex.getMessage();
                    Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                    Log.e(TAG, error);
                  }
                } else {
                  String error = "Error loading route task: " + mClosestFacilityTask.getLoadError().getMessage();
                  Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                  Log.e(TAG, error);
                }
              });
            });
          }
        })
    );
  }

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    mMapView.dispose();
    super.onDestroy();
  }
}

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