Feature layer query

View inQMLC++
View on GitHub
Sample viewer app

Find features in a feature table which match an SQL query.

screenshot

Use case

Query expressions can be used in ArcGIS to select a subset of features from a feature table. This is most useful in large or complicated data sets. A possible use case might be on a feature table marking the location of street furniture through a city. A user may wish to query by a TYPE column to return "benches". In this sample, we query a U.S. state by STATE_NAME from a feature table containing all U.S. states.

How to use the sample

Input the name of a U.S. state into the text field. When you click "search", a query is performed and the matching features are highlighted or an error is returned.

How it works

  1. Create a ServiceFeatureTable using the URL of a feature service.
  2. Create a QueryParameters with a where clause specified using setWhereClause().
  3. Perform the query using queryFeatures(query) on the service feature table.
  4. When complete, the query will return a FeatureQueryResult which can be iterated over to get the matching features.

About the data

This sample uses U.S. State polygon features from the USA 2016 Daytime Population feature service.

Relevant API

  • FeatureLayer
  • FeatureQueryResult
  • QueryParameters
  • ServiceFeatureTable

Tags

Search and Query

Sample Code

FeatureLayerQuery.qmlFeatureLayerQuery.cppFeatureLayerQuery.h
                                                                                       
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
// [WriteFile Name=FeatureLayerQuery, Category=Features]
// [Legal]
// Copyright 2016 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.Samples 1.0

FeatureLayerQuerySample {
    id: featureLayerQuerySample
    width: 800
    height: 600

    // add a mapView component
    MapView {
        id: mapView
        anchors.fill: parent
        objectName: "mapView"
    }

    Row {
        id: findRow

        anchors {
            top: parent.top
            bottom: mapView.top
            left: parent.left
            right: parent.right
            margins: 5
        }
        spacing: 5

        TextField {
            id: findText

            width: parent.width * 0.25
            placeholderText: "Enter a state name to select"
            inputMethodHints: Qt.ImhNoPredictiveText
            selectByMouse: true
            validator: RegExpValidator{ regExp: /^[a-zA-Z ]*$/ }
            Keys.onReturnPressed: {
                // Call C++ invokable function to run the query
                featureLayerQuerySample.runQuery(findText.text);
            }
        }

        Button {
            text: "Find and Select"
            enabled: featureLayerQuerySample.layerInitialized
            onClicked: {
                // Call C++ invokable function to run the query
                featureLayerQuerySample.runQuery(findText.text);
            }
        }
    }

    Dialog {
        id: errorMsgDialog
        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: "No state named " + findText.text.toUpperCase() + " exists."
        }
    }

    onQueryResultsCountChanged: {
        // Use the C++ property to determine if no features were returned
        if (featureLayerQuerySample.queryResultsCount === 0)
            errorMsgDialog.visible = true;
    }
}

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