|
| 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.viewshed_geoprocessing; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | + |
| 24 | +import javafx.application.Application; |
| 25 | +import javafx.geometry.Point2D; |
| 26 | +import javafx.geometry.Pos; |
| 27 | +import javafx.scene.Scene; |
| 28 | +import javafx.scene.control.Alert; |
| 29 | +import javafx.scene.control.Alert.AlertType; |
| 30 | +import javafx.scene.control.ProgressIndicator; |
| 31 | +import javafx.scene.input.MouseButton; |
| 32 | +import javafx.scene.layout.StackPane; |
| 33 | +import javafx.scene.paint.Color; |
| 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.data.Feature; |
| 39 | +import com.esri.arcgisruntime.data.FeatureCollectionTable; |
| 40 | +import com.esri.arcgisruntime.data.FeatureSet; |
| 41 | +import com.esri.arcgisruntime.data.Field; |
| 42 | +import com.esri.arcgisruntime.geometry.GeometryType; |
| 43 | +import com.esri.arcgisruntime.geometry.Point; |
| 44 | +import com.esri.arcgisruntime.loadable.LoadStatus; |
| 45 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 46 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 47 | +import com.esri.arcgisruntime.mapping.view.Graphic; |
| 48 | +import com.esri.arcgisruntime.mapping.view.GraphicsOverlay; |
| 49 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 50 | +import com.esri.arcgisruntime.symbology.ColorUtil; |
| 51 | +import com.esri.arcgisruntime.symbology.FillSymbol; |
| 52 | +import com.esri.arcgisruntime.symbology.SimpleFillSymbol; |
| 53 | +import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol; |
| 54 | +import com.esri.arcgisruntime.symbology.SimpleRenderer; |
| 55 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingFeatures; |
| 56 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingJob; |
| 57 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingParameters; |
| 58 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingResult; |
| 59 | +import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingTask; |
| 60 | + |
| 61 | +public class ViewshedGeoprocessingSample extends Application { |
| 62 | + |
| 63 | + private MapView mapView; |
| 64 | + private GeoprocessingJob geoprocessingJob; |
| 65 | + |
| 66 | + @Override |
| 67 | + public void start(Stage stage) throws Exception { |
| 68 | + |
| 69 | + try { |
| 70 | + // create stack pane and application scene |
| 71 | + StackPane stackPane = new StackPane(); |
| 72 | + Scene scene = new Scene(stackPane); |
| 73 | + |
| 74 | + // set title, size, and add scene to stage |
| 75 | + stage.setTitle("Viewshed Geoprocessing Sample"); |
| 76 | + stage.setWidth(800); |
| 77 | + stage.setHeight(700); |
| 78 | + stage.setScene(scene); |
| 79 | + stage.show(); |
| 80 | + |
| 81 | + // create a view with a map and topographic basemap |
| 82 | + ArcGISMap map = new ArcGISMap(Basemap.Type.TOPOGRAPHIC, 45.3790902612337, 6.84905317262762, 12); |
| 83 | + mapView = new MapView(); |
| 84 | + mapView.setMap(map); |
| 85 | + |
| 86 | + // create an input graphics overlay to show red point markers where the user clicks |
| 87 | + SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 10); |
| 88 | + SimpleRenderer renderer = new SimpleRenderer(pointSymbol); |
| 89 | + GraphicsOverlay inputGraphicsOverlay = new GraphicsOverlay(); |
| 90 | + inputGraphicsOverlay.setRenderer(renderer); |
| 91 | + |
| 92 | + // create an output graphics overlay to show the viewsheds as orange areas |
| 93 | + int fillColor = ColorUtil.colorToArgb(Color.rgb(226, 119, 40, 0.5)); |
| 94 | + FillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, fillColor, null); |
| 95 | + GraphicsOverlay outputGraphicsOverlay = new GraphicsOverlay(); |
| 96 | + outputGraphicsOverlay.setRenderer(new SimpleRenderer(fillSymbol)); |
| 97 | + |
| 98 | + // add the graphics overlays to the map view |
| 99 | + mapView.getGraphicsOverlays().addAll(Arrays.asList(inputGraphicsOverlay, outputGraphicsOverlay)); |
| 100 | + |
| 101 | + // show progress indicator when geoprocessing task is loading or geoprocessing job is running |
| 102 | + ProgressIndicator progress = new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS); |
| 103 | + progress.setMaxWidth(30); |
| 104 | + |
| 105 | + // create the geoprocessing task with the service URL and load it |
| 106 | + GeoprocessingTask geoprocessingTask = new GeoprocessingTask("https://sampleserver6.arcgisonline" + |
| 107 | + ".com/arcgis/rest/services/Elevation/ESRI_Elevation_World/GPServer/Viewshed"); |
| 108 | + geoprocessingTask.loadAsync(); |
| 109 | + |
| 110 | + geoprocessingTask.addDoneLoadingListener(() -> { |
| 111 | + if (geoprocessingTask.getLoadStatus() == LoadStatus.LOADED) { |
| 112 | + // hide the progress when the geoprocessing task is done loading and enable the click event |
| 113 | + progress.setVisible(false); |
| 114 | + |
| 115 | + mapView.setOnMouseClicked(e -> { |
| 116 | + // check that the primary mouse button was clicked and any previous geoprocessing job has been canceled |
| 117 | + if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY && geoprocessingJob == null) { |
| 118 | + |
| 119 | + // show a graphic in the input graphics overlay at the clicked location |
| 120 | + Point2D point2D = new Point2D(e.getX(), e.getY()); |
| 121 | + Point point = mapView.screenToLocation(point2D); |
| 122 | + Graphic inputGraphic = new Graphic(point); |
| 123 | + inputGraphicsOverlay.getGraphics().add(inputGraphic); |
| 124 | + |
| 125 | + progress.setVisible(true); |
| 126 | + // get the default geoprocessing parameters |
| 127 | + ListenableFuture<GeoprocessingParameters> defaultParameters = geoprocessingTask.createDefaultParametersAsync(); |
| 128 | + defaultParameters.addDoneListener(() -> { |
| 129 | + try { |
| 130 | + GeoprocessingParameters parameters = defaultParameters.get(); |
| 131 | + |
| 132 | + // create required viewshed fields |
| 133 | + List<Field> fields = Collections.singletonList(Field.createString("observer", "", 8)); |
| 134 | + |
| 135 | + // create a feature collection table (used as a parameter to the geoprocessing job) |
| 136 | + final FeatureCollectionTable featureCollectionTable = new FeatureCollectionTable(fields, |
| 137 | + GeometryType.POINT, point.getSpatialReference()); |
| 138 | + featureCollectionTable.loadAsync(); |
| 139 | + |
| 140 | + featureCollectionTable.addDoneLoadingListener(() -> { |
| 141 | + // create a new feature with the geometry of the clicked location and add it to the table |
| 142 | + Feature newFeature = featureCollectionTable.createFeature(); |
| 143 | + newFeature.setGeometry(point); |
| 144 | + |
| 145 | + featureCollectionTable.addFeatureAsync(newFeature).addDoneListener(() -> { |
| 146 | + // set the required parameters for viewshed |
| 147 | + parameters.setProcessSpatialReference(featureCollectionTable.getSpatialReference()); |
| 148 | + parameters.setOutputSpatialReference(featureCollectionTable.getSpatialReference()); |
| 149 | + parameters.getInputs().put("Input_Observation_Point", new GeoprocessingFeatures(featureCollectionTable)); |
| 150 | + |
| 151 | + // create a geoprocessing job from the task with the parameters |
| 152 | + geoprocessingJob = geoprocessingTask.createJob(parameters); |
| 153 | + |
| 154 | + // start the job and wait for the result |
| 155 | + geoprocessingJob.start(); |
| 156 | + geoprocessingJob.addJobDoneListener(() -> { |
| 157 | + if (geoprocessingJob.getStatus() == Job.Status.SUCCEEDED) { |
| 158 | + // get the viewshed from the job's result |
| 159 | + GeoprocessingResult geoprocessingResult = geoprocessingJob.getResult(); |
| 160 | + GeoprocessingFeatures resultFeatures = (GeoprocessingFeatures) geoprocessingResult.getOutputs().get("Viewshed_Result"); |
| 161 | + |
| 162 | + // loop through the result features to get the viewshed geometries |
| 163 | + FeatureSet featureSet = resultFeatures.getFeatures(); |
| 164 | + for (Feature feature : featureSet) { |
| 165 | + // add the viewshed geometry as a graphic to the output graphics overlay |
| 166 | + Graphic graphic = new Graphic(feature.getGeometry()); |
| 167 | + outputGraphicsOverlay.getGraphics().add(graphic); |
| 168 | + } |
| 169 | + } else { |
| 170 | + // remove the input and show an error if the job fails |
| 171 | + inputGraphicsOverlay.getGraphics().remove(inputGraphic); |
| 172 | + new Alert(AlertType.ERROR, "Geoprocessing job failed. Try again.").show(); |
| 173 | + } |
| 174 | + // hide the progress when the job is complete |
| 175 | + progress.setVisible(false); |
| 176 | + // cancel the job if it's still going and set it to null to re-enable the mouse click listener |
| 177 | + if (geoprocessingJob != null) { |
| 178 | + geoprocessingJob.cancel(); |
| 179 | + geoprocessingJob = null; |
| 180 | + } |
| 181 | + }); |
| 182 | + }); |
| 183 | + }); |
| 184 | + } catch (InterruptedException | ExecutionException ex) { |
| 185 | + new Alert(AlertType.ERROR, "Error creating default geoprocessing parameters").show(); |
| 186 | + } |
| 187 | + }); |
| 188 | + } |
| 189 | + }); |
| 190 | + } else { |
| 191 | + new Alert(AlertType.ERROR, "Failed to load geoprocessing task").show(); |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + // and the mapView and progress indicator to the stack pane |
| 196 | + stackPane.getChildren().addAll(mapView, progress); |
| 197 | + StackPane.setAlignment(progress, Pos.CENTER); |
| 198 | + } catch (Exception e) { |
| 199 | + // on any error, display the stack trace. |
| 200 | + e.printStackTrace(); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Stops and releases all resources used in application. |
| 206 | + */ |
| 207 | + @Override |
| 208 | + public void stop() { |
| 209 | + |
| 210 | + if (mapView != null) { |
| 211 | + mapView.dispose(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Opens and runs application. |
| 217 | + * |
| 218 | + * @param args arguments passed to this application |
| 219 | + */ |
| 220 | + public static void main(String[] args) { |
| 221 | + |
| 222 | + Application.launch(args); |
| 223 | + } |
| 224 | +} |
0 commit comments