|
| 1 | +/* |
| 2 | + * Copyright 2018 Esri. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.samples.analysis.analyze_hotspots; |
| 18 | + |
| 19 | +import java.sql.Timestamp; |
| 20 | +import java.text.SimpleDateFormat; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | + |
| 23 | +import javafx.application.Application; |
| 24 | +import javafx.geometry.Insets; |
| 25 | +import javafx.geometry.Pos; |
| 26 | +import javafx.scene.Scene; |
| 27 | +import javafx.scene.control.Alert; |
| 28 | +import javafx.scene.control.Alert.AlertType; |
| 29 | +import javafx.scene.control.Button; |
| 30 | +import javafx.scene.control.DatePicker; |
| 31 | +import javafx.scene.control.ProgressIndicator; |
| 32 | +import javafx.scene.layout.StackPane; |
| 33 | +import javafx.scene.layout.VBox; |
| 34 | +import javafx.stage.Stage; |
| 35 | + |
| 36 | +import com.esri.arcgisruntime.concurrent.Job; |
| 37 | +import com.esri.arcgisruntime.concurrent.ListenableFuture; |
| 38 | +import com.esri.arcgisruntime.geometry.Point; |
| 39 | +import com.esri.arcgisruntime.geometry.SpatialReference; |
| 40 | +import com.esri.arcgisruntime.layers.ArcGISMapImageLayer; |
| 41 | +import com.esri.arcgisruntime.loadable.LoadStatus; |
| 42 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 43 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 44 | +import com.esri.arcgisruntime.mapping.Viewpoint; |
| 45 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 46 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingJob; |
| 47 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingParameters; |
| 48 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingResult; |
| 49 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingString; |
| 50 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingTask; |
| 51 | + |
| 52 | +public class AnalyzeHotspotsSample extends Application { |
| 53 | + |
| 54 | + private MapView mapView; |
| 55 | + private GeoprocessingJob geoprocessingJob; |
| 56 | + |
| 57 | + @Override |
| 58 | + public void start(Stage stage) throws Exception { |
| 59 | + |
| 60 | + try { |
| 61 | + // create stack pane and application scene |
| 62 | + StackPane stackPane = new StackPane(); |
| 63 | + Scene scene = new Scene(stackPane); |
| 64 | + |
| 65 | + // set title, size, and add scene to stage |
| 66 | + stage.setTitle("Analyze Hotspots Sample"); |
| 67 | + stage.setWidth(800); |
| 68 | + stage.setHeight(700); |
| 69 | + stage.setScene(scene); |
| 70 | + stage.show(); |
| 71 | + |
| 72 | + // create a view with a map and topographic basemap |
| 73 | + ArcGISMap map = new ArcGISMap(Basemap.createTopographic()); |
| 74 | + map.setInitialViewpoint(new Viewpoint(new Point(-13671170, 5693633, SpatialReference.create(3857)), 57779)); |
| 75 | + mapView = new MapView(); |
| 76 | + mapView.setMap(map); |
| 77 | + |
| 78 | + // show progress indicator when geoprocessing task is loading or geoprocessing job is running |
| 79 | + ProgressIndicator progress = new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS); |
| 80 | + progress.setMaxWidth(30); |
| 81 | + |
| 82 | + // create date pickers to choose the date range, initialize with the min & max dates |
| 83 | + SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); |
| 84 | + Timestamp minDate = Timestamp.valueOf("1998-01-01 00:00:00"); |
| 85 | + Timestamp maxDate = Timestamp.valueOf("1998-05-31 00:00:00"); |
| 86 | + DatePicker begDatePicker = new DatePicker(minDate.toLocalDateTime().toLocalDate()); |
| 87 | + begDatePicker.setPromptText("Beg"); |
| 88 | + DatePicker endDatePicker = new DatePicker(maxDate.toLocalDateTime().toLocalDate()); |
| 89 | + endDatePicker.setPromptText("End"); |
| 90 | + |
| 91 | + // create a button to create and start the geoprocessing job, disable until geoprocessing task is loaded |
| 92 | + Button analyzeButton = new Button("Analyze Hotspots"); |
| 93 | + analyzeButton.setDisable(true); |
| 94 | + |
| 95 | + VBox controlsVBox = new VBox(6); |
| 96 | + controlsVBox.setMaxSize(180, 250); |
| 97 | + controlsVBox.getChildren().addAll(begDatePicker, endDatePicker, analyzeButton); |
| 98 | + |
| 99 | + // and the mapView, controls, and progress indicator to the stack pane |
| 100 | + stackPane.getChildren().addAll(mapView, controlsVBox, progress); |
| 101 | + StackPane.setAlignment(progress, Pos.CENTER); |
| 102 | + StackPane.setAlignment(controlsVBox, Pos.TOP_LEFT); |
| 103 | + StackPane.setMargin(controlsVBox, new Insets(10, 0, 0, 10)); |
| 104 | + |
| 105 | + // create the geoprocessing task with the service URL and load it |
| 106 | + GeoprocessingTask geoprocessingTask = new GeoprocessingTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/GPServer/911%20Calls%20Hotspot"); |
| 107 | + geoprocessingTask.loadAsync(); |
| 108 | + |
| 109 | + geoprocessingTask.addDoneLoadingListener(() -> { |
| 110 | + if (geoprocessingTask.getLoadStatus() == LoadStatus.LOADED) { |
| 111 | + // hide the progress when the geoprocessing task is done loading and enable the button |
| 112 | + progress.setVisible(false); |
| 113 | + analyzeButton.setDisable(false); |
| 114 | + |
| 115 | + analyzeButton.setOnAction(e -> { |
| 116 | + // clear any previous results, and show the progress indicator until the job is complete |
| 117 | + map.getOperationalLayers().clear(); |
| 118 | + progress.setVisible(true); |
| 119 | + |
| 120 | + // get the task's default geoprocessing parameters |
| 121 | + ListenableFuture<GeoprocessingParameters> defaultParameters = geoprocessingTask.createDefaultParametersAsync(); |
| 122 | + defaultParameters.addDoneListener(() -> { |
| 123 | + try { |
| 124 | + GeoprocessingParameters parameters = defaultParameters.get(); |
| 125 | + |
| 126 | + // get timestamps from the datepickers |
| 127 | + Timestamp beg = Timestamp.valueOf(begDatePicker.getValue().atStartOfDay()); |
| 128 | + Timestamp end = Timestamp.valueOf(endDatePicker.getValue().atStartOfDay()); |
| 129 | + |
| 130 | + // check that the timestamps are in the valid range for the data |
| 131 | + if (beg.getTime() >= minDate.getTime() && beg.getTime() <= maxDate.getTime() && end.getTime() >= |
| 132 | + minDate.getTime() && end.getTime() <= maxDate.getTime()) { |
| 133 | + |
| 134 | + // add a date range query to the geoprocessing parameters |
| 135 | + String begTimestamp = dateFormat.format(beg); |
| 136 | + String endTimestamp = dateFormat.format(end); |
| 137 | + String query = "(\"DATE\" > date '" + begTimestamp + "' AND \"Date\" < date '" + endTimestamp + "')"; |
| 138 | + parameters.getInputs().put("Query", new GeoprocessingString(query)); |
| 139 | + |
| 140 | + // create a geoprocessing job from the task with the parameters |
| 141 | + geoprocessingJob = geoprocessingTask.createJob(parameters); |
| 142 | + |
| 143 | + // start the job and wait for the result |
| 144 | + geoprocessingJob.start(); |
| 145 | + geoprocessingJob.addJobDoneListener(() -> { |
| 146 | + if (geoprocessingJob.getStatus() == Job.Status.SUCCEEDED) { |
| 147 | + // get the map image layer from the job's result |
| 148 | + GeoprocessingResult geoprocessingResult = geoprocessingJob.getResult(); |
| 149 | + ArcGISMapImageLayer hotSpotLayer = geoprocessingResult.getMapImageLayer(); |
| 150 | + // make the layer semi-transparent to reference the basemap streets underneath |
| 151 | + hotSpotLayer.setOpacity(0.7f); |
| 152 | + // add the layer to the map and zoom to it when loaded |
| 153 | + map.getOperationalLayers().add(hotSpotLayer); |
| 154 | + hotSpotLayer.loadAsync(); |
| 155 | + hotSpotLayer.addDoneLoadingListener(() -> { |
| 156 | + if (hotSpotLayer.getLoadStatus() == LoadStatus.LOADED) { |
| 157 | + mapView.setViewpointGeometryAsync(hotSpotLayer.getFullExtent()); |
| 158 | + } else { |
| 159 | + new Alert(AlertType.ERROR, "Result layer failed to load: " + hotSpotLayer.getLoadError() |
| 160 | + .getMessage()).show(); |
| 161 | + } |
| 162 | + }); |
| 163 | + } else { |
| 164 | + new Alert(AlertType.ERROR, "Geoprocessing job failed: " + geoprocessingJob.getError().getMessage()).show(); |
| 165 | + } |
| 166 | + // hide the progress when the job is complete |
| 167 | + progress.setVisible(false); |
| 168 | + // cancel the job if it's still going and set it to null to re-enable the mouse click listener |
| 169 | + if (geoprocessingJob != null) { |
| 170 | + geoprocessingJob.cancel(); |
| 171 | + geoprocessingJob = null; |
| 172 | + } |
| 173 | + }); |
| 174 | + } else { |
| 175 | + new Alert(AlertType.ERROR, "Time range must be within " + minDate.toString() + " and " + maxDate |
| 176 | + .toString()).show(); |
| 177 | + progress.setVisible(false); |
| 178 | + } |
| 179 | + } catch (InterruptedException | ExecutionException ex) { |
| 180 | + new Alert(AlertType.ERROR, "Error creating default geoprocessing parameters").show(); |
| 181 | + progress.setVisible(false); |
| 182 | + } |
| 183 | + }); |
| 184 | + }); |
| 185 | + } else { |
| 186 | + new Alert(AlertType.ERROR, "Failed to load geoprocessing task").show(); |
| 187 | + progress.setVisible(false); |
| 188 | + } |
| 189 | + }); |
| 190 | + } catch (Exception e) { |
| 191 | + // on any error, display the stack trace. |
| 192 | + e.printStackTrace(); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Stops and releases all resources used in application. |
| 198 | + */ |
| 199 | + @Override |
| 200 | + public void stop() { |
| 201 | + |
| 202 | + if (mapView != null) { |
| 203 | + mapView.dispose(); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Opens and runs application. |
| 209 | + * |
| 210 | + * @param args arguments passed to this application |
| 211 | + */ |
| 212 | + public static void main(String[] args) { |
| 213 | + |
| 214 | + Application.launch(args); |
| 215 | + } |
| 216 | +} |
0 commit comments