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 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 on the raster or press on the raster, hold, and move around the raster to identify it and see the raster cell attributes information displayed in a callout.
How it works
- Connect to signals emmited by a
mouseClicked
,mousePressedAndHeld
, andmouseMoved
on theMapView
. - On tapped or pressed, held, and dragged:
- Call
identifyLayer(...)
passing in the raster layer, screen point, tolerance, whether to return popups only, and maximum number of results per layer. - Connect to the
identifyLayerStatusChanged
. If theidentifyLayerStatus
isEnums.TaskStatusCompleted
then use theidentifyLayerResult
to get the result of the identify and then get theGeoElement
from the layer result and get anyRasterCell
s from them. - Create a callout at the calculated map point and populate the callout content with text from the
RasterCell
attributes. - Show the callout.
- Call
Relevant API
- GeoView.identifyLayer(...)
- IdentifyLayerResult
- RasterCell
- RasterCell.attributes
- RasterLayer
Offline data
To set up the sample's offline data, see the Use offline data in the samples section of the Qt Samples repository overview.
Link | Local Location |
---|---|
South Africa data | <userhome> /ArcGIS/Runtime/Data/raster/SA_EVI_8Day_03May20 |
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.
Tags
band, cell, cell value, continuous, discrete, identify, pixel, pixel value, raster
Sample Code
// [WriteFile Name=IdentifyRasterCell, Category=Layers]
// [Legal]
// 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.
// [Legal]
import QtQuick 2.15
import QtQuick.Controls 2.15
import Esri.ArcGISRuntime 100.15
import Esri.ArcGISExtras 1.1
import Esri.ArcGISRuntime.Toolkit 100.15
Rectangle {
id: rootRectangle
clip: true
width: 800
height: 600
readonly property url dataPath: System.userHomePath + "/ArcGIS/Runtime/Data/raster/SA_EVI_8Day_03May20/"
property Point clickedPoint: null
property string calloutText: ""
property bool pressedMouse: false
MapView {
id: mapView
anchors.fill: parent
Component.onCompleted: {
// Set the focus on MapView to initially enable keyboard navigation
forceActiveFocus();
}
Callout {
id: callout
calloutData: parent.calloutData
implicitWidth: 300
contentItem: Label {
id: componentText
text: calloutText
wrapMode: Text.WordWrap
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
}
}
Map {
Basemap {
initStyle: Enums.BasemapStyleArcGISOceans
}
RasterLayer {
id: rasterLayer
Raster {
path: dataPath + "SA_EVI_8Day_03May20.tif"
}
onErrorChanged: {
console.warn(loadError.message);
}
onLoadStatusChanged: {
if (loadStatus !== Enums.LoadStatusLoaded)
return;
mapView.setViewpointGeometry(fullExtent);
}
}
}
onMouseClicked: {
clickedPoint = screenToLocation(mouse.x, mouse.y);
if (identifyLayerStatus !== Enums.TaskStatusInProgress) {
identifyLayer(rasterLayer, mouse.x, mouse.y, 10, false, 1);
}
}
onMousePressedAndHeld: {
pressedMouse = true;
clickedPoint = screenToLocation(mouse.x, mouse.y);
if (identifyLayerStatus !== Enums.TaskStatusInProgress) {
identifyLayer(rasterLayer, mouse.x, mouse.y, 10, false, 1);
}
}
onMouseReleased: pressedMouse = false;
onMousePositionChanged: {
if (pressedMouse) {
clickedPoint = screenToLocation(mouse.x, mouse.y);
if (identifyLayerStatus !== Enums.TaskStatusInProgress) {
identifyLayer(rasterLayer, mouse.x, mouse.y, 10, false, 1);
}
}
}
onIdentifyLayerStatusChanged: {
if (identifyLayerStatus !== Enums.TaskStatusCompleted)
return;
for (let i = 0; i < identifyLayerResult.geoElements.length; i++) {
calloutText = "";
let geoElement = identifyLayerResult.geoElements[i];
const attributes = geoElement.attributes;
const attributeNames = attributes.attributeNames;
for (let j = 0; j < attributeNames.length; j++) {
calloutText = calloutText + attributeNames[j] + ": " + attributes.attributeValue(attributeNames[j]) + "\n";
}
const xPoint = geoElement.geometry.extent.xMin;
const yPoint = geoElement.geometry.extent.yMin;
calloutText = calloutText + "X: " + xPoint.toFixed(2) + " Y: " + yPoint.toFixed(2);
callout.calloutData.location = clickedPoint;
callout.showCallout();
}
}
}
}