Get the cell value of a local raster and display the result in a callout.
Use case
You may want to identify a raster layer to get its exact cell value in the case the approximate value conveyed by its symbology is not sufficient. The information available for the raster cell depends on the type of raster layer being identified. For example, a 3-band satellite or aerial image might provide 8-bit RGB values, whereas a digital elevation model (DEM) would provide floating point z values. By identifying a raster cell of a DEM, you can retrieve the precise elevation of a location.
How to use the sample
Move the mouse pointer over an area of the raster to identify the raster cell at that location. The raster cell attribute information will display in a callout. You can click the primary mouse button to lock the callout in place, and click again to release the callout and resume identifying on-the-fly.
How it works
Add a listener to the MapView to capture mouse clicks.
On click:
Dismiss the Callout, if one is showing.
Call identifyLayerAsync(...) passing in the raster layer, screen point, tolerance, whether to return popups only, and maximum number of results.
Await the result of the identify and then get the GeoElement from the layer result.
Create a callout at the calculated map point and populate the callout content with text from the RasterCell attributes.
Show the callout.
Relevant API
IdentifyLayerResult
RasterCell
RasterLayer
About the data
The data shown is an NDVI classification derived from MODIS imagery between 27 Apr 2020 and 4 May 2020. It comes from the NASA Worldview application. In a normalized difference vegetation index, or NDVI, values range between -1 and +1 with the positive end of the spectrum showing green vegetation.
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
/*
* Copyright 2020 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.identify_raster_cell;
import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.RasterLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.GeoElement;
import com.esri.arcgisruntime.mapping.view.Callout;
import com.esri.arcgisruntime.mapping.view.IdentifyLayerResult;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.raster.Raster;
import com.esri.arcgisruntime.raster.RasterCell;
publicclassIdentifyRasterCellSampleextendsApplication{
privateboolean calloutLocked = false;
private Callout callout;
private MapView mapView;
private RasterLayer rasterLayer;
@Overridepublicvoidstart(Stage stage){
try {
// create stack pane and application scene StackPane stackPane = new StackPane();
Scene scene = new Scene(stackPane);
// set title, size, and add scene to stage stage.setTitle("Identify Raster Cell Sample");
stage.setWidth(800);
stage.setHeight(700);
stage.setScene(scene);
stage.show();
// 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 oceans basemap style ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_OCEANS);
// create a map view and set the map to it mapView = new MapView();
mapView.setMap(map);
// create a raster from a local raster file Raster raster = new Raster(new File(System.getProperty("data.dir"), "./samples-data/SA_EVI_8Day_03May20/SA_EVI_8Day_03May20.tif").getAbsolutePath());
// create a raster layer rasterLayer = new RasterLayer(raster);
// add the raster layer to the map's operational layers map.getOperationalLayers().add(rasterLayer);
// set viewpoint on the raster rasterLayer.addDoneLoadingListener(() -> {
if (map.getLoadStatus() == LoadStatus.LOADED) {
mapView.setViewpointGeometryAsync(rasterLayer.getFullExtent(), 50);
} else {
Alert alert = new Alert(Alert.AlertType.ERROR, "Raster Layer Failed to Load!");
alert.show();
}
});
// get a handle on the callout callout = mapView.getCallout();
callout.setTitle("Raster cell attributes:");
// make the callout transparent to mouse interactions, so that we keep identifying raster cells behind it callout.setMouseTransparent(true);
// start identifying on-the-fly if the mouse enters the map view, and the callout is not locked mapView.setOnMouseEntered(mouseEvent -> {
if (!calloutLocked) {
mapView.setOnMouseMoved(this::identifyRasterCell);
}
});
// stop identifying on-the-fly when the mouse leaves the map view mapView.setOnMouseExited(null);
// on click, either lock the callout in place if raster cells are identified on-the-fly,// or release the callout again and start identifying on-the-fly mapView.setOnMouseClicked(mouseEvent -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY && mouseEvent.isStillSincePress()) {
if (!calloutLocked) {
// stop identifying on the fly mapView.setOnMouseMoved(null);
// lock the callout in place calloutLocked = true;
} else {
// dismiss the callout callout.dismiss();
// unlock the callout calloutLocked = false;
// show a new callout at the clicked location identifyRasterCell(mouseEvent);
// start identifying on-the-fly mapView.setOnMouseMoved(this::identifyRasterCell);
}
}
});
// add the map view to stack pane stackPane.getChildren().addAll(mapView);
} catch (Exception e) {
// on any error, display the stack trace e.printStackTrace();
}
}
/**
* Identifies the raster cell at the mouse event's location and displays a callout at that location.
*
* @param mouseEvent the mouse event used to identify the raster cell and show the callout
*/privatevoididentifyRasterCell(MouseEvent mouseEvent){
// get the map point where the user clicked Point2D point = new Point2D(mouseEvent.getX(), mouseEvent.getY());
Point mapPoint = mapView.screenToLocation(point);
// identify the layers at the clicked location ListenableFuture<IdentifyLayerResult> identifyLayerResultFuture
= mapView.identifyLayerAsync(rasterLayer, point, 10, false, 1);
identifyLayerResultFuture.addDoneListener(() -> {
try {
// get the result of the query IdentifyLayerResult identifyLayerResult = identifyLayerResultFuture.get();
// get the read only list of geo-elements (they contain RasterCells) List<GeoElement> geoElements = identifyLayerResult.getElements();
// create a StringBuilder to display information to the user StringBuilder stringBuilder = new StringBuilder();
// loop through each RasterCellfor (GeoElement geoElement : geoElements) {
if (geoElement instanceof RasterCell) {
RasterCell rasterCell = (RasterCell) geoElement;
// loop through the attributes (key/value pairs) rasterCell.getAttributes().forEach((key, value) ->
// add the key-value pair to the string builder stringBuilder.append(key).append(": ").append(value).append("\n")
);
// get and format the X and Y values for the celldouble x = rasterCell.getGeometry().getExtent().getXMin();
double y = rasterCell.getGeometry().getExtent().getYMin();
String string = "X: " + Math.round(x) + " Y: " + Math.round(y);
// add the X & Y coordinates where the user clicked raster cell to the string builder stringBuilder.append(string);
// define a callout based on the string builder callout.setDetail(stringBuilder.toString());
callout.showCalloutAt(mapPoint);
}
}
} catch (InterruptedException | ExecutionException e) {
new Alert(Alert.AlertType.ERROR, "Error identifying layer").show();
}
});
}
/**
* Stops and releases all resources used in application.
*/@Overridepublicvoidstop(){
if (mapView != null) {
mapView.dispose();
}
}
/**
* Opens and runs application.
*
* @param args arguments passed to this application
*/publicstaticvoidmain(String[] args){
Application.launch(args);
}
}