Analyze hotspots

View inJavaKotlinView on GitHubSample viewer app

Use a geoprocessing service and a set of features to identify statistically significant hot spots and cold spots.

Image of analyze hotspots

Use case

This tool identifies statistically significant spatial clusters of high values (hot spots) and low values (cold spots). For example, a hotspot analysis based on the frequency of 911 calls within a set region.

How to use the sample

Select a date range (between 1998-01-01 and 1998-05-31) from the dialog and tap on Analyze. The results will be shown on the map upon successful completion of the geoprocessing job.

How it works

  1. Create a GeoprocessingTask with the URL set to the endpoint of a geoprocessing service.
  2. Create a query string with the date range as an input of GeoprocessingParameters.
  3. Use the GeoprocessingTask to create a GeoprocessingJob with the GeoprocessingParameters instance.
  4. Start the GeoprocessingJob and wait for it to complete and return a GeoprocessingResult.
  5. Get the resulting ArcGISMapImageLayer using geoprocessingResult.getMapImageLayer().
  6. Add the layer to the map's operational layers.

Relevant API

  • GeoprocessingJob
  • GeoprocessingParameters
  • GeoprocessingResult
  • GeoprocessingTask

Tags

analysis, density, geoprocessing, hot spots, hotspots

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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* 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.arcgisruntime.sample.analyzehotspots;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ExecutionException;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.Job;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingJob;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingParameters;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingResult;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingString;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingTask;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

// enum to flag whether the date picker calendar shown should be for the 'from' or 'to' date
enum InputCalendar {
  From,
  To
}

public class MainActivity extends AppCompatActivity {

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

  private MapView mMapView;
  private GeoprocessingTask mGeoprocessingTask;
  private GeoprocessingJob mGeoprocessingJob;

  private EditText fromDateText;
  private EditText toDateText;

  private SimpleDateFormat mSimpleDateFormatter;
  private Date mMinDate;
  private Date mMaxDate;

  private boolean mIsCanceled;

  @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);

    // create a simple date formatter to parse strings to date
    mSimpleDateFormatter = new SimpleDateFormat(getString(R.string.date_format), Locale.US);

    // inflate MapView from layout
    mMapView = findViewById(R.id.mapView);

    // create a map with the Basemap Style topographic
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);

    // set the map to the map view
    mMapView.setMap(map);

    // center for initial viewpoint
    Point center = new Point(-13671170, 5693633, SpatialReference.create(3857));

    // set initial viewpoint
    mMapView.setViewpoint(new Viewpoint(center, 57779));

    // initialize geoprocessing task with the url of the service
    mGeoprocessingTask = new GeoprocessingTask(getString(R.string.hotspot_911_calls));
    mGeoprocessingTask.loadAsync();

    FloatingActionButton calendarFAB = findViewById(R.id.calendarButton);

    calendarFAB.setOnClickListener(v -> showDateRangeDialog());

    calendarFAB.performClick();
  }

  /**
   * Creates the date range dialog. Includes listeners to handle click events, which call showCalendar(...) or
   * analyzeHotspots(...).
   */
  private void showDateRangeDialog() {
    // create custom dialog
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_alert_dialog);
    dialog.setCancelable(true);

    try {
      // set default date range for the data set
      mMinDate = mSimpleDateFormatter.parse(getString(R.string.min_date));
      mMaxDate = mSimpleDateFormatter.parse(getString(R.string.max_date));
    } catch (ParseException e) {
      Log.e(TAG, "Error in date format: " + e.getMessage());
    }

    fromDateText = dialog.findViewById(R.id.fromDateText);
    toDateText = dialog.findViewById(R.id.toDateText);

    fromDateText.setOnClickListener(v -> showCalendar(InputCalendar.From));
    toDateText.setOnClickListener(v -> showCalendar(InputCalendar.To));

    Button analyzeButton = dialog.findViewById(R.id.analyzeButton);
    // if button is clicked, close the custom dialog
    analyzeButton.setOnClickListener(v -> {
      analyzeHotspots(fromDateText.getText().toString(), toDateText.getText().toString());
      dialog.dismiss();
    });

    dialog.show();
  }

  /**
   * Shows a date picker dialog and writes the date chosen to the correct editable text.
   *
   * @param inputCalendar enum which specifies which editable text the chosen date should be written to
   */
  private void showCalendar(final InputCalendar inputCalendar) {
    // create a date set listener
    DatePickerDialog.OnDateSetListener onDateSetListener = (view, year, month, dayOfMonth) -> {
      // build the correct date format for the query
      StringBuilder date = new StringBuilder()
          .append(year)
          .append("-")
          .append(month + 1)
          .append("-")
          .append(dayOfMonth);
      // set the date to correct text view
      if (inputCalendar == InputCalendar.From) {
        fromDateText.setText(date);
        try {
          // limit the min date to after from date
          mMinDate = mSimpleDateFormatter.parse(date.toString());
        } catch (ParseException e) {
          e.printStackTrace();
        }
      } else if (inputCalendar == InputCalendar.To) {
        toDateText.setText(date);
        try {
          // limit the maximum date to before the to date
          mMaxDate = mSimpleDateFormatter.parse(date.toString());
        } catch (ParseException e) {
          e.printStackTrace();
        }
      }
    };

    // define the date picker dialog
    Calendar calendar = Calendar.getInstance();
    DatePickerDialog datePickerDialog = new DatePickerDialog(this, onDateSetListener,
        calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    datePickerDialog.getDatePicker().setMinDate(mMinDate.getTime());
    datePickerDialog.getDatePicker().setMaxDate(mMaxDate.getTime());
    if (inputCalendar == InputCalendar.From) {
      // start from calendar from min date
      datePickerDialog.updateDate(1998, 0, 1);
    }
    datePickerDialog.show();
  }

  /**
   * Runs the geoprocessing job, updating progress while loading. On job done, loads the resulting
   * ArcGISMapImageLayer to the map and resets the Viewpoint of the MapView.
   *
   * @param from string which holds a date
   * @param to   string which holds a date
   */
  private void analyzeHotspots(final String from, final String to) {
    // cancel previous job request
    if (mGeoprocessingJob != null) {
      mGeoprocessingJob.cancelAsync();
    }

    // a map image layer is generated as a result. Remove any layer previously added to the map
    mMapView.getMap().getOperationalLayers().clear();

    // set canceled flag to false
    mIsCanceled = false;

    // parameters
    final ListenableFuture<GeoprocessingParameters> paramsFuture = mGeoprocessingTask.createDefaultParametersAsync();
    paramsFuture.addDoneListener(() -> {
      try {
        GeoprocessingParameters geoprocessingParameters = paramsFuture.get();
        geoprocessingParameters.setProcessSpatialReference(mMapView.getSpatialReference());
        geoprocessingParameters.setOutputSpatialReference(mMapView.getSpatialReference());

        StringBuilder queryString = new StringBuilder("(\"DATE\" > date '")
            .append(from)
            .append(" 00:00:00' AND \"DATE\" < date '")
            .append(to)
            .append(" 00:00:00')");

        geoprocessingParameters.getInputs().put("Query", new GeoprocessingString(queryString.toString()));

        Log.i(TAG, "Query: " + queryString);

        // create job
        mGeoprocessingJob = mGeoprocessingTask.createJob(geoprocessingParameters);

        // start job
        mGeoprocessingJob.start();

        // create a dialog to show progress of the geoprocessing job
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Running geoprocessing job");
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.setCancelable(false);
        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", (dialog, which) -> {
          dialog.dismiss();
          // set canceled flag to true
          mIsCanceled = true;
          mGeoprocessingJob.cancelAsync();
        });
        progressDialog.show();

        // update progress
        mGeoprocessingJob.addProgressChangedListener(() -> progressDialog.setProgress(mGeoprocessingJob.getProgress()));

        mGeoprocessingJob.addJobDoneListener(() -> {
          progressDialog.dismiss();
          if (mGeoprocessingJob.getStatus() == Job.Status.SUCCEEDED) {
            Log.i(TAG, "Job succeeded.");

            GeoprocessingResult geoprocessingResult = mGeoprocessingJob.getResult();
            final ArcGISMapImageLayer hotspotMapImageLayer = geoprocessingResult.getMapImageLayer();

            // add the new layer to the map
            mMapView.getMap().getOperationalLayers().add(hotspotMapImageLayer);

            hotspotMapImageLayer.addDoneLoadingListener(() -> {
              if (hotspotMapImageLayer.getLoadStatus() == LoadStatus.LOADED) {
                // set the map viewpoint to the MapImageLayer, once loaded
                mMapView.setViewpointGeometryAsync(hotspotMapImageLayer.getFullExtent());
              } else {
                Toast.makeText(this, "Error loading hot spot image layer: " + hotspotMapImageLayer.getLoadError().getCause().getMessage(),
                    Toast.LENGTH_SHORT).show();
                Log.e(TAG, "Error loading hot spot image layer: " + hotspotMapImageLayer.getLoadError().getCause().getMessage());
              }
            });
          } else if (mIsCanceled) {
            Toast.makeText(this, "Job canceled.", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "Job cancelled.");
          } else {
            Log.e(TAG, "Job did not succeed!");
            Toast.makeText(this, "Job did not succeed!", Toast.LENGTH_LONG).show();
          }
        });
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    });
  }

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

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

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

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