Display OGC API collection

View on GitHub
Sample viewer app

Display an OGC API feature collection and query features while navigating the map view.

Image of display OGC API collection

Use case

When panning the map view, it may be necessary to query the OGC API feature table for additional features within the new visible extent.

How to use the sample

Pan the map and observe how new features are loaded from the OGC API feature service.

How it works

  1. Create an OgcFeatureCollectionTable using a URL to an OGC API feature service and a collection ID.
  2. Set the feature table's FeatureRequestMode property to FeatureRequestMode.MANUAL_CACHE.
  3. Call OgcFeatureCollectionTable.loadAsync().
  4. Create a FeatureLayer using the feature collection table and add it to the map view.
  5. Every time the map view navigation completes:
    1. Create QueryParameters.
    2. Set the parameter's Geometry to the current extent of the map view.
    3. Set the parameter's SpatialRelationship property to SpatialRelationship.INTERSECTS.
    4. Set the MaxFeatures property to 5000 (some services have a low default value for maximum features).
    5. Call OgcFeatureCollectionTable.populateFromServiceAsync() using the query parameters from the previous steps.

Relevant API

  • OgcFeatureCollectionTable
  • QueryParameters

About the data

The Daraa, Syria test data is OpenStreetMap data converted to the Topographic Data Store schema of NGA.

Additional information

See the OGC API website for more information on the OGC API family of standards.

Tags

feature, feature layer, feature table, OGC, OGC API, service, table, web

Sample Code

MainActivity.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
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
163
164
165
166
167
168
169
170
171
172
/* Copyright 2021 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.displayogcapicollection

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.data.OgcFeatureCollectionTable
import com.esri.arcgisruntime.data.QueryParameters
import com.esri.arcgisruntime.data.ServiceFeatureTable
import com.esri.arcgisruntime.geometry.Envelope
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.loadable.LoadStatus
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.displayogcapicollection.databinding.ActivityMainBinding
import com.esri.arcgisruntime.symbology.SimpleLineSymbol
import com.esri.arcgisruntime.symbology.SimpleRenderer

class MainActivity : AppCompatActivity() {

    // define strings for the service URL and collection id
    // note that the service defines the collection id which can be accessed
    // via OgcFeatureCollectionInfo.getCollectionId()
    private val serviceUrl = "https://demo.ldproxy.net/daraa"
    private val collectionId = "TransportationGroundCrv"

    // create an OGC feature collection table from the service url and collection id
    // keep loadable in scope to avoid garbage collection
    private var ogcFeatureCollectionTable: OgcFeatureCollectionTable =
        OgcFeatureCollectionTable(serviceUrl, collectionId)

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    override fun onCreate(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)

        // create a map with the BasemapStyle topographic
        val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)

        // set the map to be displayed in the layout's MapView
        mapView.map = map

        // set the feature request mode to manual (only manual is currently supported).
        // in this mode, the table must be manually populated - panning and zooming won't request features automatically
        ogcFeatureCollectionTable.featureRequestMode =
            ServiceFeatureTable.FeatureRequestMode.MANUAL_CACHE

        // load the table
        ogcFeatureCollectionTable.loadAsync()

        // ensure the feature collection table has loaded successfully before creating a feature layer from it to display on the map
        ogcFeatureCollectionTable.addDoneLoadingListener {

            if (ogcFeatureCollectionTable.loadStatus == LoadStatus.LOADED) {

                // create a feature layer and set a renderer to it to visualize the OGC API features
                val featureLayer = FeatureLayer(ogcFeatureCollectionTable)
                val simpleRenderer =
                    SimpleRenderer(SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 3F))
                featureLayer.renderer = simpleRenderer

                // add the layer to the map
                map.operationalLayers.add(featureLayer)

                // zoom to a small area within the dataset by default
                val datasetExtent = ogcFeatureCollectionTable.extent
                if (datasetExtent != null && !datasetExtent.isEmpty) {
                    mapView.setViewpointGeometryAsync(
                        Envelope(
                            datasetExtent.center,
                            datasetExtent.width / 3,
                            datasetExtent.height / 3
                        )
                    )
                }
            } else {
                // show an alert if there is a loading failure
                Log.e(
                    ogcFeatureCollectionTable.loadError.message,
                    ogcFeatureCollectionTable.loadError.additionalMessage
                )
                Toast.makeText(
                    applicationContext,
                    "Failed to load OGC Feature Collection Table",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        // once the map view navigation has completed, query the OGC API feature table for
        // additional features within the new visible extent
        mapView.addNavigationChangedListener {
            if (!it.isNavigating) {

                // get the current extent
                val currentExtent = mapView.visibleArea.extent

                // create a query based on the current visible extent
                val visibleExtentQuery = QueryParameters().apply {
                    geometry = currentExtent
                    spatialRelationship =
                        QueryParameters.SpatialRelationship.INTERSECTS
                    // set a limit of 5000 on the number of returned features per request, the default on some services
                    // could be as low as 10
                    maxFeatures = 5000
                }
                visibleExtentQuery.maxFeatures = 5000

                try {
                    // populate the table with the query, leaving existing table entries intact
                    // setting the outfields parameter to null requests all fields
                    ogcFeatureCollectionTable.populateFromServiceAsync(
                        visibleExtentQuery,
                        false,
                        null
                    )
                } catch (e: Exception) {
                    Toast.makeText(
                        applicationContext,
                        "Error populating from service: " + e.message,
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

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