Query feature table

View on GitHubSample viewer app

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

Image of query feature table

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 the search icon on the device keypad or press the enter key, 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 whereClause().
  3. Perform the query using queryFeatures(queryParameters) on the service feature table.
  4. When complete, the query will return a FeatureQueryResult which can be iterated over to get the matching features.

Relevant API

  • FeatureLayer
  • FeatureQueryResult
  • QueryParameters
  • ServiceFeatureTable

About the data

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

Additional information

This sample uses the GeoView-Compose Toolkit module to be able to implement a composable MapView.

Tags

geoview-compose, query, search, toolkit

Sample Code

MapViewModel.ktMapViewModel.ktMainActivity.ktMainScreen.ktSearchBar.kt
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
/* Copyright 2023 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.arcgismaps.sample.queryfeaturetable.components

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import com.arcgismaps.Color
import com.arcgismaps.data.FeatureQueryResult
import com.arcgismaps.data.QueryParameters
import com.arcgismaps.data.ServiceFeatureTable
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.layers.FeatureLayer
import com.arcgismaps.mapping.symbology.SimpleFillSymbol
import com.arcgismaps.mapping.symbology.SimpleFillSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleLineSymbol
import com.arcgismaps.mapping.symbology.SimpleLineSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleRenderer
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.queryfeaturetable.R
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.util.Locale

class MapViewModel(
    application: Application,
    private val sampleCoroutineScope: CoroutineScope
) : AndroidViewModel(application) {

    // create a ViewModel to handle dialog interactions
    val messageDialogVM: MessageDialogViewModel = MessageDialogViewModel()

    // create a service feature table and a feature layer from it
    private val serviceFeatureTable: ServiceFeatureTable = ServiceFeatureTable(
        application.getString(R.string.us_daytime_population_url)
    )

    // create the feature layer using the service feature table
    private val featureLayer: FeatureLayer by lazy {
        FeatureLayer.createWithFeatureTable(serviceFeatureTable)
    }

    // map used to display the feature layer
    val map = ArcGISMap(BasemapStyle.ArcGISTopographic)

    private var usaViewpoint = Viewpoint(
        center = Point(-11e6, 5e6, SpatialReference.webMercator()),
        scale = 1e8
    )

    // create a MapViewProxy to handle MapView operations
    var mapViewProxy by mutableStateOf(MapViewProxy())

    init {
        // use symbols to show U.S. states with a black outline and yellow fill
        val lineSymbol = SimpleLineSymbol(
            style = SimpleLineSymbolStyle.Solid,
            color = Color.black,
            width = 1.0f
        )
        val fillSymbol = SimpleFillSymbol(
            style = SimpleFillSymbolStyle.Solid,
            color = Color.fromRgba(255, 255, 0, 255),
            outline = lineSymbol
        )

        // set featurelayer properties
        featureLayer.apply {
            // set renderer for the feature layer
            renderer = SimpleRenderer(fillSymbol)
            opacity = 0.8f
            maxScale = 10000.0
        }
        // add the feature layer to the map's operational layers
        map.apply {
            initialViewpoint = usaViewpoint
            operationalLayers.add(featureLayer)
        }
    }

    /**
     * Search for a U.S. state using [searchQuery] in the feature table, and if found add it
     * to the featureLayer, zoom to it, and select it.
     */
    fun searchForState(searchQuery: String) {
        // clear any previous selections
        featureLayer.clearSelection()
        // create a query for the state that was entered
        val queryParameters = QueryParameters().apply {
            // make search case insensitive
            whereClause = ("upper(STATE_NAME) LIKE '%" + searchQuery.uppercase(Locale.US) + "%'")
        }

        sampleCoroutineScope.launch {
            // call select features
            val featureQueryResult = serviceFeatureTable.queryFeatures(queryParameters).getOrElse {
                messageDialogVM.showMessageDialog(it.message.toString(), it.cause.toString())
            } as FeatureQueryResult

            val feature = featureQueryResult.firstOrNull()
            if (feature != null) {
                // select the feature
                featureLayer.selectFeature(feature)
                // get the extent of the first feature in the result to zoom to
                val envelope = feature.geometry?.extent
                    ?: return@launch messageDialogVM.showMessageDialog("Error retrieving geometry extent")
                // update the map's viewpoint to the feature's geometry
                mapViewProxy.setViewpointGeometry(envelope)
            } else {
                messageDialogVM.showMessageDialog("No states found with name: $searchQuery")
            }
        }
    }
}

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