Identify layers

View inC++QMLView on GitHubSample viewer app

Identify features in all layers in a map.

screenshot

Use case

IdentifyLayers allows users to tap on a map, returning features at that location across multiple layers. Because some layer types have sublayers, the sample recursively counts results for sublayers within each layer.

How to use the sample

Tap to identify features. An alert will show all layers with features under the cursor, as well as a layer count.

How it works

  1. The tapped position is passed to mapView.identifyLayersWithMaxResults
  2. For each IdentifyLayerResult in the results, features are counted.
    • Note: there is one identify result per layer with matching features; if the feature count is 0, that means a sublayer contains the matching features.

Relevant API

  • IdentifyLayerResult
  • IdentifyLayerResult.layerContent.name
  • IdentifyLayerResult.sublayerResults
  • MapView.identifyLayers

Additional information

The GeoView supports two methods of identify: identifyLayer(), which identifies features within a specific layer and identifyLayers(), which identifies features for all layers in the current view.

Tags

identify, recursion, recursive, sublayers

Sample Code

IdentifyLayers.qml
Use dark colors for code blocksCopy
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
// [WriteFile Name=IdentifyLayers, Category=Maps]
// [Legal]
// Copyright 2017 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.6
import QtQuick.Controls 2.2
import Esri.ArcGISRuntime 100.15

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    property string msgText: ""

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISTopographic
            }

            // add a map image layer and hide 2 of the sublayers
            ArcGISMapImageLayer {
                url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer"
                onLoadStatusChanged: {
                    if (error) {
                        console.log("error:", error.message, error.additionalMessage)
                        return;
                    }

                    if (loadStatus !== Enums.LoadStatusLoaded)
                        return;

                    mapImageSublayers.get(1).visible = false;
                    mapImageSublayers.get(2).visible = false;
                }
            }

            // add a feature layer
            FeatureLayer {
                ServiceFeatureTable {
                    url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"
                }
            }

            // set an initial viewpoint
            ViewpointCenter {
                targetScale: 68015210

                Point {
                    x: -10977012.785807
                    y: 4514257.550369
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }
        }

        // identify layers on mouse click
        onMouseClicked: {
            const screenX = mouse.x;
            const screenY = mouse.y;
            const tolerance = 12;
            const returnPopups = false;
            const maxResults = 10;
            mapView.identifyLayersWithMaxResults(screenX, screenY, tolerance, returnPopups, maxResults);
        }

        // handle the identify results
        onIdentifyLayersStatusChanged: {
            if (identifyLayersStatus !== Enums.TaskStatusCompleted)
                return;

            // reset the message string
            msgText = "";

            // loop through the results
            const results = mapView.identifyLayersResults;
            for (let i = 0; i < results.length; i++) {
                const result = results[i];
                const count = geoElementsCountFromResult(result);
                const layerName = result.layerContent.name;
                msgText += "%1 : %2".arg(layerName).arg(count);
                // add new line character if not the final element in array
                if (i !== results.length)
                    msgText += "\n";
            }

            // show the message box
            if (msgText.length > 0)
                msgDialog.open();
        }

        // handle any errors on the map view
        onErrorChanged: {
            if (error)
                console.log("error:", error.message, error.additionalMessage)
        }
    }

    function geoElementsCountFromResult(identifyLayerResult) {
        // create temp array
        const tempResults = [identifyLayerResult];

        // use Depth First Search approach to handle recursion
        let count = 0;
        let index = 0;

        while (index < tempResults.length) {
            //get the result object from the array
            const identifyResult = tempResults[index];

            // update count with geoElements from the result
            count += identifyResult.geoElements.length;

            // check if the result has any sublayer results
            // if yes then add those result objects in the tempResults
            // array after the current result
            if (identifyResult.sublayerResults.length > 0) {
                tempResults.push(identifyResult.sublayerResults[index]);
            }

            // update the count and repeat
            index += 1;
        }

        return count;
    }

    Dialog {
        id: msgDialog
        modal: true
        x: Math.round(parent.width - width) / 2
        y: Math.round(parent.height - height) / 2
        standardButtons: Dialog.Ok
        property alias text : textLabel.text
        Text {
            id: textLabel
            text: msgText
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.