Get the cell value of a local raster at the tapped location 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
Tap or double tap drag an area of the raster to identify it and see the raster cell attributes information displayed in a callout.
How it works
Create a DefaultMapViewOnTouchListener on the MapView.
On tap or double tap drag:
Call identifyLayerAsync(...) passing in the raster layer, screen point, tolerance, and maximum number of results per layer.
Add a done loading listener for the result of the identify and then get the GeoElement from the layer result and get any RasterCells from them.
Create a callout at the calculated map point and populate the callout content with text from the RasterCell attributes.
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
/*
* 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.arcgisruntime.sample.identifyrastercell
import android.graphics.Color
import android.graphics.Point
import android.os.Bundle
import android.view.MotionEvent
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.layers.RasterLayer
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.raster.Raster
import com.esri.arcgisruntime.raster.RasterCell
import com.esri.arcgisruntime.sample.identifyrastercell.databinding.ActivityMainBinding
classMainActivity : AppCompatActivity() {
privatevar rasterLayer: RasterLayer? = nullprivateval activityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
privateval mapView: MapView by lazy {
activityMainBinding.mapView
}
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activityMainBinding.root)
// authentication with an API key or named user is required to access basemaps and other// location services ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)
// load the raster fileval rasterFile =
Raster(getExternalFilesDir(null)?.path + "/SA_EVI_8Day_03May20.tif")
// create the layer rasterLayer = RasterLayer(rasterFile)
// define a new mapval rasterMap = ArcGISMap(BasemapStyle.ARCGIS_OCEANS).apply {
// add the raster layer operationalLayers.add(rasterLayer)
}
mapView.apply {
// add the map to the map view map = rasterMap
setViewpoint(Viewpoint(-33.9, 18.6, 1000000.0))
// set behavior for double touch drag and on single tap gestures onTouchListener = object : DefaultMapViewOnTouchListener(this@MainActivity, mapView) {
overridefunonDoubleTouchDrag(e: MotionEvent): Boolean {
// identify the pixel at the given screen point identifyPixel(Point(e.x.toInt(), e.y.toInt()))
returntrue }
overridefunonSingleTapConfirmed(e: MotionEvent): Boolean {
// identify the pixel at the given screen point identifyPixel(Point(e.x.toInt(), e.y.toInt()))
returntrue }
}
}
}
/**
* Identify the pixel at the given screen point and report raster cell attributes in a callout.
*
* @param screenPoint from motion event, for use in identify
*/privatefunidentifyPixel(screenPoint: Point) {
rasterLayer?.let { rasterLayer ->
// identify at the tapped screen pointval identifyResultFuture =
mapView.identifyLayerAsync(rasterLayer, screenPoint, 1.0, false, 10)
identifyResultFuture.addDoneListener {
// get the identify resultval identifyResult = identifyResultFuture.get()
// create a string builderval stringBuilder = StringBuilder()
// get the a list of geoelements as raster cells from the identify result identifyResult.elements.filterIsInstance<RasterCell>().forEach { cell ->
// get each attribute for the cell cell.attributes.forEach {
// add the key/value pair to the string builder stringBuilder.append(it.key + ": " + it.value)
stringBuilder.append("\n")
}
// format the X & Y coordinate values of the raster cell to a human readable stringval xyString =
"X: ${String.format("%.4f", cell.geometry.extent.xMin)} " + "\n" +
"Y: ${String.format("%.4f", cell.geometry.extent.yMin)}"// add the coordinate string to the string builder stringBuilder.append(xyString)
// create a textview for the calloutval calloutContent = TextView(applicationContext).apply {
setTextColor(Color.BLACK)
// format coordinates to 4 decimal places and display lat long read out text = stringBuilder.toString()
}
// display the callout in the map view mapView.callout.apply {
location = mapView.screenToLocation(screenPoint)
content = calloutContent
style.leaderLength = 64 }.show()
}
}
}
}
overridefunonPause() {
mapView.pause()
super.onPause()
}
overridefunonResume() {
super.onResume()
mapView.resume()
}
overridefunonDestroy() {
mapView.dispose()
super.onDestroy()
}
}