Find the service area within a network from a given point.
Use case
A service area shows locations that can be reached from a facility based off a certain impedance, such as travel time or distance. Barriers can increase impedance by either adding to the time it takes to pass through the barrier or by altogether preventing passage.
You might calculate the region around a hospital in which ambulances can service in 30 min or less.
How to use the sample
In order to find any service areas at least one facility needs to be added to the map view. Add facilities to the map by clicking the "Add Facilities" button and then clicking anywhere on the map. To add barriers, click the "Add Barriers" button, and click multiple locations on map. Clicking any other button will stop the barrier from drawing. Click on "Add Barriers" again to draw a new barrier. To show the service areas around facilities that were added, click "Show Service Areas" button. The "Reset" button clears all graphics and resets the service area task.
How it works
Create a new ServiceAreaTask from a network service.
Create default ServiceAreaParameters from the service area task.
Set the parameters to return polygons (true) to return all service areas.
Add a ServiceAreaFacility to the parameters.
Get the ServiceAreaResult by solving the service area task using the parameters.
Get any ServiceAreaPolygons that were returned, serviceAreaResult.getResultPolygons(facilityIndex).
Display the service area polygons as graphics in a GraphicsOverlay on the MapView.
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
/*
* 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.
*/package com.esri.samples.service_area_task;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.ToggleButton;
import javafx.scene.input.MouseButton;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
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.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.tasks.networkanalysis.PolylineBarrier;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaFacility;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaParameters;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaPolygon;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaPolygonDetail;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaResult;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaTask;
publicclassServiceAreaTaskController{
@FXMLprivate MapView mapView;
@FXMLprivate ToggleButton btnAddFacility;
@FXMLprivate ToggleButton btnAddBarrier;
@FXMLprivate ProgressIndicator progressIndicator;
// all location were service areas will be foundprivate List<ServiceAreaFacility> serviceAreaFacilities;
// task to find service area around a facilityprivate ServiceAreaTask serviceAreaTask;
// used for solving task aboveprivate ServiceAreaParameters serviceAreaParameters;
// for displaying service area facilities to the mapviewprivate GraphicsOverlay facilityOverlay;
// for displaying service areas to the mapviewprivate GraphicsOverlay serviceAreasOverlay;
// for displaying barriers to mapviewprivate GraphicsOverlay barrierOverlay;
// used to make barriersprivate PolylineBuilder barrierBuilder;
// fills service areas with a color when displayed to mapviewprivate List<SimpleFillSymbol> fillSymbols;
// used for placing geometry on mapviewprivatefinal SpatialReference spatialReference = SpatialReferences.getWebMercator();
@FXMLpublicvoidinitialize(){
// authentication with an API key or named user is required to access basemaps and other location services String yourAPIKey = System.getProperty("apiKey");
ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);
// create a map with the streets basemap style and set it to the map view ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
mapView.setMap(map);
// set a viewpoint on the map view mapView.setViewpoint(new Viewpoint(32.73, -117.14, 60000));
// create service area task from urlfinal String SanDiegoRegion = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ServiceArea";
serviceAreaTask = new ServiceAreaTask(SanDiegoRegion);
serviceAreaTask.loadAsync();
// create default parameters from task ListenableFuture<ServiceAreaParameters> parameters = serviceAreaTask.createDefaultParametersAsync();
parameters.addDoneListener(() -> {
try {
serviceAreaParameters = parameters.get();
serviceAreaParameters.setPolygonDetail(ServiceAreaPolygonDetail.HIGH);
serviceAreaParameters.setReturnPolygons(true);
// adding another service area of 2 minutes// default parameters have a default service area of 5 minutes serviceAreaParameters.getDefaultImpedanceCutoffs().addAll(Collections.singletonList(2.0));
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
});
// for displaying graphics to mapview serviceAreasOverlay = new GraphicsOverlay();
facilityOverlay = new GraphicsOverlay();
barrierOverlay = new GraphicsOverlay();
mapView.getGraphicsOverlays().addAll(Arrays.asList(serviceAreasOverlay, barrierOverlay, facilityOverlay));
barrierBuilder = new PolylineBuilder(spatialReference);
serviceAreaFacilities = new ArrayList<>();
SimpleLineSymbol outline = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF000000, 3.0f);
fillSymbols = new ArrayList<>();
fillSymbols.add(new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0x66FF0000, outline));
fillSymbols.add(new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0x66FFA500, outline));
// icon used to display facilities to mapview String facilityUrl = "http://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png";
PictureMarkerSymbol facilitySymbol = new PictureMarkerSymbol(facilityUrl);
facilitySymbol.setHeight(30);
facilitySymbol.setWidth(30);
// creates facilities and barriers at user's clicked location mapView.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY && e.isStillSincePress()) {
// create a point from where the user clicked Point2D point = new Point2D(e.getX(), e.getY());
Point mapPoint = mapView.screenToLocation(point);
if (btnAddFacility.isSelected()) {
// create facility from point and display to mapview Point servicePoint = new Point(mapPoint.getX(), mapPoint.getY(), spatialReference);
serviceAreaFacilities.add(new ServiceAreaFacility(servicePoint));
facilityOverlay.getGraphics().add(new Graphic(servicePoint, facilitySymbol));
} elseif (btnAddBarrier.isSelected()) {
// create barrier and display to mapview barrierBuilder.addPoint(new Point(mapPoint.getX(), mapPoint.getY(), spatialReference));
barrierOverlay.getGraphics().add(barrierOverlay.getGraphics().size(), new Graphic(barrierBuilder.toGeometry(), outline));
}
}
});
}
/**
* Starts creating a new barrier if barrier button is selected.
*/@FXMLprivatevoidcreateBarrier(){
if (btnAddBarrier.isSelected()) {
barrierBuilder = new PolylineBuilder(spatialReference);
}
}
/**
* Clears all graphics from mapview and clears all facilities and barriers from service area parameters.
*/@FXMLprivatevoidclearRouteAndGraphics(){
serviceAreaParameters.clearFacilities();
serviceAreaParameters.clearPolylineBarriers();
serviceAreaFacilities.clear();
facilityOverlay.getGraphics().clear();
serviceAreasOverlay.getGraphics().clear();
barrierOverlay.getGraphics().clear();
}
/**
* Solves Service Areas Task using the facilities and barriers that were added to the mapview.
* <p>
* All service areas that are return will be displayed to the mapview.
*/@FXMLprivatevoidshowServiceAreas(){
// need at least one facility for the task to workif (serviceAreaFacilities.size() > 0) {
progressIndicator.setVisible(true);
//turn barrier button off and add any barriers to service area parameters btnAddBarrier.setSelected(false);
List<PolylineBarrier> polylineBarriers = new ArrayList<>();
barrierOverlay.getGraphics()
.forEach(barrier -> polylineBarriers.add(new PolylineBarrier((Polyline) barrier.getGeometry())));
serviceAreaParameters.setPolylineBarriers(polylineBarriers);
serviceAreasOverlay.getGraphics().clear();
serviceAreaParameters.setFacilities(serviceAreaFacilities);
// find service areas around facility using parameters that were set ListenableFuture<ServiceAreaResult> result = serviceAreaTask.solveServiceAreaAsync(serviceAreaParameters);
result.addDoneListener(() -> {
try {
// display all service areas that were found to mapview List<Graphic> graphics = serviceAreasOverlay.getGraphics();
ServiceAreaResult serviceAreaResult = result.get();
for (int i = 0; i < serviceAreaFacilities.size(); i++) {
List<ServiceAreaPolygon> polygons = serviceAreaResult.getResultPolygons(i);
// could be more than one service areafor (int j = 0; j < polygons.size(); j++) {
graphics.add(new Graphic(polygons.get(j).getGeometry(), fillSymbols.get(j % 2)));
}
}
} catch (ExecutionException | InterruptedException e) {
if (e.getMessage().contains("Unable to complete operation")) {
showErrorMessage("Facility not within San Diego area!");
} else {
e.printStackTrace();
}
}
progressIndicator.setVisible(false);
});
} else {
showErrorMessage("Must have at least 1 Facility!");
}
}
/**
* Shows error message to user if something went wrong with solving task.
*
* @param message error message to show.
*/privatevoidshowErrorMessage(String message){
Alert dialog = new Alert(AlertType.WARNING);
dialog.setHeaderText(null);
dialog.setTitle("Error");
dialog.setContentText(message);
dialog.showAndWait();
}
/**
* Stops and releases all resources used in application.
*/voidterminate(){
if (mapView != null) {
mapView.dispose();
}
}
}