Query map image sublayer

View on GitHubSample viewer app

Find features in a sublayer based on attributes and location.

Image of query map image sublayer

Use case

Sublayers of an ArcGISMapImageLayer may expose a ServiceFeatureTable through a table property. This allows you to perform the same queries available when working with a table from a FeatureLayer: attribute query, spatial query, statistics query, query for related features, etc. An image layer with a sublayer of counties can be queried by population to only show those above a minimum population.

How to use the sample

Specify a minimum population in the input field (values under 1810000 will produce a selection in all layers) and tap the query button to query the sublayers in the current view extent. After a short time, the results for each sublayer will appear as graphics.

How it works

  1. Create an ArcGISMapImageLayer object using the URL of an image service.
  2. After loading the layer, get the sublayer you want to query with (ArcGISMapImageSublayer) layer.getSubLayers().get(index).
  3. Load the sublayer, and then get its ServiceFeatureTable with sublayer.getTable().
  4. Create a QueryParameters object. You can use queryParameters.setWhereClause(sqlQueryString) to query against a table attribute and/or set queryParameters.setGeometry(extent) to limit the results to an area of the map.
  5. Call sublayerTable.queryFeaturesAsync(queryParameters) to get a FeatureQueryResult with features matching the query. The result is an iterable of features.

About the data

The ArcGISMapImageLayer in the map uses the "USA" map service as its data source. This service is hosted by ArcGIS Online, and is composed of four sublayers: "states", "counties", "cities", and "highways". Since the cities, counties, and states tables all have a POP2000 field, they can all execute a query against that attribute and a map extent.

Relevant API

  • ArcGISMapImageLayer
  • ArcGISMapImageSublayer
  • QueryParameters
  • ServiceFeatureTable

Tags

search and query

Sample Code

MainActivity.java
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 2018 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.querymapimagesublayer;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.data.QueryParameters;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.layers.ArcGISMapImageSublayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.Symbol;

import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

  private MapView mMapView;
  private Button mQueryButton;
  private EditText mQueryInputBox;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // inflate views from layout
    mMapView = findViewById(R.id.mapView);
    mQueryButton = findViewById(R.id.queryButton);
    mQueryInputBox = findViewById(R.id.queryInputBox);

    // create a map with a streets vector basemap
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);

    // set the map to be displayed in this view
    mMapView.setMap(map);
    Point initialLocation = new Point(-13171939.239529, 3923971.284048, SpatialReferences.getWebMercator());
    mMapView.setViewpoint(new Viewpoint(initialLocation, 9500000));

    // create and add a map image layer to the map
    ArcGISMapImageLayer imageLayer = new ArcGISMapImageLayer(getString(R.string.usa_map));
    map.getOperationalLayers().add(imageLayer);

    // create a graphics overlay to show the results in
    GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
    mMapView.getGraphicsOverlays().add(graphicsOverlay);

    // create symbols for showing the results of each sublayer
    SimpleMarkerSymbol citySymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 16);
    SimpleLineSymbol stateSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0000FF, 6);
    SimpleLineSymbol countyLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF00FFFF, 2);
    SimpleFillSymbol countySymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, 0xFF00FFFF,
        countyLineSymbol);

    mQueryInputBox.setText(Integer.toString(1800000));
    mQueryButton.setEnabled(false);

    // wait until the layer is loaded before enabling the query button
    imageLayer.addDoneLoadingListener(() -> {
      mQueryButton.setEnabled(true);

      // get and load each sublayer to query
      ArcGISMapImageSublayer citiesSublayer = (ArcGISMapImageSublayer) imageLayer.getSublayers().get(0);
      ArcGISMapImageSublayer statesSublayer = (ArcGISMapImageSublayer) imageLayer.getSublayers().get(2);
      ArcGISMapImageSublayer countiesSublayer = (ArcGISMapImageSublayer) imageLayer.getSublayers().get(3);
      citiesSublayer.loadAsync();
      statesSublayer.loadAsync();
      countiesSublayer.loadAsync();

      // query the sublayers when the button is clicked
      mQueryButton.setOnClickListener(v -> {

        // clear previous results
        graphicsOverlay.getGraphics().clear();

        // create query parameters filtering based on population and the map view's current viewpoint
        QueryParameters populationQuery = new QueryParameters();
        populationQuery.setWhereClause("POP2000 > " + mQueryInputBox.getText());
        populationQuery
                .setGeometry(mMapView.getCurrentViewpoint(Viewpoint.Type.BOUNDING_GEOMETRY).getTargetGeometry());

        QueryAndDisplayGraphics(citiesSublayer, citySymbol, populationQuery, graphicsOverlay);
        QueryAndDisplayGraphics(statesSublayer, stateSymbol, populationQuery, graphicsOverlay);
        QueryAndDisplayGraphics(countiesSublayer, countySymbol, populationQuery, graphicsOverlay);

      });
    });
  }

  /**
   * Queries the sublayer's feature table with the query parameters and displays the result features as graphics
   *
   * @param sublayer        - type of sublayer to query from
   * @param sublayerSymbol  - symbol to display on map
   * @param query           - filters based on the population and the current view point
   * @param graphicsOverlay - manages the graphics that will be added to the map view
   */
  private static void QueryAndDisplayGraphics(ArcGISMapImageSublayer sublayer, Symbol sublayerSymbol, QueryParameters query,
      GraphicsOverlay graphicsOverlay) {
    if (sublayer.getLoadStatus() == LoadStatus.LOADED) {
      ServiceFeatureTable sublayerTable = sublayer.getTable();
      ListenableFuture<FeatureQueryResult> sublayerQuery = sublayerTable.queryFeaturesAsync(query);
      sublayerQuery.addDoneListener(() -> {
        try {
          FeatureQueryResult result = sublayerQuery.get();
          for (Feature feature : result) {
            Graphic sublayerGraphic = new Graphic(feature.getGeometry(), sublayerSymbol);
            graphicsOverlay.getGraphics().add(sublayerGraphic);
          }
        } catch (InterruptedException | ExecutionException e) {
          Log.e(MainActivity.class.getSimpleName(), e.toString());
        }
      });
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    mMapView.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mMapView.dispose();
  }
}

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