Query

A query provides the ability to return a subset of features from a dataset based on any combination of attribute, spatial, and temporal (time) criteria.

  • Attribute criteria are defined with a standard SQL expression based on the available attribute fields.
  • Spatial criteria use a geometry and a spatial relationship (within, contains, intersect, and so on).
  • A temporal filter can be defined using a single date or time, or a range.

You can also perform queries to return related features, a feature count, an extent containing all features meeting your criteria, or statistical information about a dataset.

How query works

Query criteria is defined using a query parameters object. This is where you specify the attribute, spatial, and/or temporal inputs. Most ArcGIS Runtime queries take query parameters as an input to define the query criteria as well as some preferences for the results. When the query is executed against a specific dataset (feature table), results are returned as a collection of features.

A query does not require that each type of criteria be defined. Query criteria are only evaluated if explicitly defined (missing temporal criteria, for example, means not to filter the results according to time).

Relevant classes and members in the API ref

Query parameters

Query parameters define the query criteria using:

  • An SQL expression for attribute criteria
  • Geometry and a spatial relationship for spatial criteria
  • A date/time or a range of dates/times for temporal criteria

Some spatial relationships you can define for the query include:

  • Intersects: part of a feature is contained in the geometry.
  • Touches: a feature touches the border of the geometry.
  • Crosses: a feature crosses the geometry.
  • Within: a feature is completely enclosed by the geometry.
  • Contains: part or all of a feature is contained within the geometry.

The query parameters can be used in a standard query to return features, or in queries that return a feature count or extent. You can also use the query parameters to make a selection in the map showing the features that match the criteria.

Specialized query parameters are used for queries that return statistics or related features. In addition to query criteria, these query parameters define things like the type of statistics to return or the relationships to evaluate.

This example uses spatial criteria to find features inside a polygon. Instead of executing a query on the FeatureTable, however, the query parameters are simply passed to the FeatureLayer to display features that meet the criteria as a new selection.

                                                                                                                                                                                                       
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// --- Edit ---

// check features can be added, based on edit capabilities
if (arcGISFeatureTable.canAdd()) {
  // create the attributes for the feature
  Map<String, Object> attributes = new HashMap<String, Object>();
  attributes.put("type", 1); // Coded Values: [1: Manatee] etc...
  attributes.put("confirmed", 1); // Coded Values: [0: No] , [1: Yes]
  attributes.put("comments", "Definitely a manatee");


  // Create a new feature from the attributes and an existing point geometry, and then add the feature
  Feature addedFeature = arcGISFeatureTable.createFeature(attributes, mapPoint);
  final ListenableFuture<Void> addFeatureFuture = arcGISFeatureTable.addFeatureAsync(addedFeature);
  addFeatureFuture.addDoneListener(() -> {


    // if dealing with ServiceFeatureTable, apply edits after making updates; if editing locally, then edits can
    // be synchronized at some point using the SyncGeodatabaseTask.
    if (arcGISFeatureTable instanceof ServiceFeatureTable) {
      ServiceFeatureTable serviceFeatureTable = (ServiceFeatureTable) arcGISFeatureTable;
      // apply the edits
      final ListenableFuture<List<FeatureEditResult>> applyEditsFuture = serviceFeatureTable.applyEditsAsync();
      applyEditsFuture.addDoneListener(() -> {
        try {
          final List<FeatureEditResult> featureEditResults = applyEditsFuture.get();
          // if required, can check the edits applied in this operation
          System.out.println("Number of edits: " + featureEditResults.size());
        } catch (InterruptedException | ExecutionException e) {
          // executionException may contain an ArcGISRuntimeException with edit error information.
          if (e.getCause() instanceof ArcGISRuntimeException) {
            ArcGISRuntimeException agsEx = (ArcGISRuntimeException) e.getCause();
            System.out.println("Error Code: " + agsEx.getErrorCode());
            System.out.println("Error Message: " + agsEx.getMessage());
          } else {
            // ... deal with exception here
          }
        }
      });
    }
  });
}



// get selected features from the layer for this ArcGISFeatureTable
final FeatureLayer featureLayer = arcGISFeatureTable.getFeatureLayer();
final ListenableFuture<FeatureQueryResult> selected = featureLayer.getSelectedFeaturesAsync();
FeatureQueryResult features = null;
try {
  features = selected.get();
} catch (ExecutionException | InterruptedException e) {
  // ... deal with exception
}


// check there is at least one selected feature
if (!features.iterator().hasNext()) {
  //.., deal with no features being selected
}


// get the first selected feature and load it
final ArcGISFeature feature = (ArcGISFeature) features.iterator().next();
feature.loadAsync();
feature.addDoneLoadingListener(() -> {
  // now feature is loaded we can update it; change attribute and geometry (here the point geometry is moved North)


  feature.getAttributes().put("confirmed", 1);
  Point currentLoc = (Point) feature.getGeometry();
  Point updatedLoc = new Point(currentLoc.getX(), currentLoc.getY() + 50000, mapView.getSpatialReference());
  feature.setGeometry(updatedLoc);


  // update the feature in the table
  arcGISFeatureTable.updateFeatureAsync(feature).addDoneListener(() -> {
    // if dealing with ServiceFeatureTable, apply edits after making updates; if editing locally, then edits can
    // be synchronized at some point using the SyncGeodatabaseTask.
    if (arcGISFeatureTable instanceof ServiceFeatureTable) {
      ServiceFeatureTable serviceFeatureTable = (ServiceFeatureTable) arcGISFeatureTable;


      // can call getUpdatedFeaturesCountAsync to verify number of updates to be applied before calling applyEditsAsync


      final ListenableFuture<List<FeatureEditResult>> listenerResult = serviceFeatureTable.applyEditsAsync();
      listenerResult.addDoneListener(() -> {
        try {
          List<FeatureEditResult> featureEditResults = listenerResult.get();
          // ... if required, can check the edits applied in this operation by using returned FeatureEditResult
        } catch (ExecutionException | InterruptedException e) {
          // ... deal with exception
        }
      });
      // if required, can check the edits applied in this operation by using returned FeatureEditResult
      // ... check updated results
    }
  });
});



// get selected features from the layer for this ArcGISFeatureTable
final FeatureLayer featureLayer = arcGISFeatureTable.getFeatureLayer();
final ListenableFuture<FeatureQueryResult> selected = featureLayer.getSelectedFeaturesAsync();
FeatureQueryResult features = null;
try {
  features = selected.get();
} catch (ExecutionException | InterruptedException e) {
  // ... deal with exception
}


// check atleast a feature was selected
if (!features.iterator().hasNext()) {
  // ... deal with no features being selected
}


// delete the selected features
arcGISFeatureTable.deleteFeaturesAsync(features).addDoneListener(() -> {
  //if dealing with ServiceFeatureTable, apply edits after making updates; if editing locally, then edits can
  // be synchronized at some point using the SyncGeodatabaseTask.
  if (arcGISFeatureTable instanceof ServiceFeatureTable) {
    ServiceFeatureTable serviceFeatureTable = (ServiceFeatureTable) arcGISFeatureTable;


    // can call getDeletedFeaturesCountAsync() to verify number of deletes to be applied before calling applyEditsAsync


    final ListenableFuture<List<FeatureEditResult>> listenableResult = serviceFeatureTable.applyEditsAsync();
    listenableResult.addDoneListener(() -> {
      try {
        List<FeatureEditResult> featureEditResults = listenableResult.get();
        // ... if required, can check the edits applied in this operation by using returned FeatureEditResult
      } catch (ExecutionException | InterruptedException e) {
        // ... deal with exception
      }
    });
    // if required, can check the edits applied in this operation by using returned FeatureEditResult
  }
});


// --- Query ---


// create a buffer from the point
final Polygon searchGeometry = GeometryEngine.buffer(mapPoint, 5000);

// create a query to find which features are contained by the query geometry
QueryParameters queryParams = new QueryParameters();
queryParams.setGeometry(searchGeometry);
queryParams.setSpatialRelationship(QueryParameters.SpatialRelationship.CONTAINS);

// select based on the query
final ListenableFuture<FeatureQueryResult> selectFuture =
    arcGisFeatureTable.getFeatureLayer().selectFeaturesAsync(queryParams, FeatureLayer.SelectionMode.NEW);
// if required, can listen to the future to perform an action when features are selected



// TODO:



// TODO:



// TODO:



//specifying the layer to identify, where to identify, tolerance around point, to return pop-ups only, and
// maximum results
final ListenableFuture<IdentifyLayerResult> identifyFuture =
  mapView.identifyLayerAsync(layer, screenPoint, 20, false, 25);

// add a listener to the future
identifyFuture.addDoneListener(() -> {
  try {
    // get the identify results from the future - returns when the operation is complete
    IdentifyLayerResult identifyLayerResult = identifyFuture.get();

    // a reference to the feature layer can be used, for example, to select identified features
    if (identifyLayerResult.getLayerContent() instanceof FeatureLayer) {
      FeatureLayer featureLayer = (FeatureLayer) identifyLayerResult.getLayerContent();
      // select all features that were identified
      List<Feature> features =
        identifyLayerResult.getElements().stream().map(f -> (Feature) f).collect(Collectors.toList());
      featureLayer.selectFeatures(features);
    }

  } catch (InterruptedException | ExecutionException ex) {
    // ... deal with exceptions thrown from the async identify operation
  }
});

Query results

Query results typically provide a collection of features. You can iterate the result features to display them on the map, read their attributes, and so on. A query for statistics returns a collection of records that describe the requested statistics for features in the dataset. Queries for feature count or extent return a number and an envelope respectively.

Geometry for the query results can be returned in a specified spatial reference by specifying the output spatial reference in the query parameters. If a spatial reference is not specified, results will be returned in the spatial reference of the dataset. Most often, you will need the result features in the same spatial reference as your app's map.

You can also set a maximum number of features to return in the result. This is useful in situations where you might only need a subset of features that meet your criteria. It may also improve performance by limiting the amount of information returned with the result.

Identify

Identify is like a shortcut for a spatial query. It allows you to quickly answer the question: what is here? It gives users a quick way to explore and learn about the map or scene content by tapping or clicking. Information returned from an identify operation can be shown in pop-ups or other UI components in your app. Unlike a query, you can't provide attribute or time criteria to filter results. You can, however, return all geoelements (from all layers) at the specified location.

                                                                                                                                                                                                       
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// --- Edit ---

// check features can be added, based on edit capabilities
if (arcGISFeatureTable.canAdd()) {
  // create the attributes for the feature
  Map<String, Object> attributes = new HashMap<String, Object>();
  attributes.put("type", 1); // Coded Values: [1: Manatee] etc...
  attributes.put("confirmed", 1); // Coded Values: [0: No] , [1: Yes]
  attributes.put("comments", "Definitely a manatee");


  // Create a new feature from the attributes and an existing point geometry, and then add the feature
  Feature addedFeature = arcGISFeatureTable.createFeature(attributes, mapPoint);
  final ListenableFuture<Void> addFeatureFuture = arcGISFeatureTable.addFeatureAsync(addedFeature);
  addFeatureFuture.addDoneListener(() -> {


    // if dealing with ServiceFeatureTable, apply edits after making updates; if editing locally, then edits can
    // be synchronized at some point using the SyncGeodatabaseTask.
    if (arcGISFeatureTable instanceof ServiceFeatureTable) {
      ServiceFeatureTable serviceFeatureTable = (ServiceFeatureTable) arcGISFeatureTable;
      // apply the edits
      final ListenableFuture<List<FeatureEditResult>> applyEditsFuture = serviceFeatureTable.applyEditsAsync();
      applyEditsFuture.addDoneListener(() -> {
        try {
          final List<FeatureEditResult> featureEditResults = applyEditsFuture.get();
          // if required, can check the edits applied in this operation
          System.out.println("Number of edits: " + featureEditResults.size());
        } catch (InterruptedException | ExecutionException e) {
          // executionException may contain an ArcGISRuntimeException with edit error information.
          if (e.getCause() instanceof ArcGISRuntimeException) {
            ArcGISRuntimeException agsEx = (ArcGISRuntimeException) e.getCause();
            System.out.println("Error Code: " + agsEx.getErrorCode());
            System.out.println("Error Message: " + agsEx.getMessage());
          } else {
            // ... deal with exception here
          }
        }
      });
    }
  });
}



// get selected features from the layer for this ArcGISFeatureTable
final FeatureLayer featureLayer = arcGISFeatureTable.getFeatureLayer();
final ListenableFuture<FeatureQueryResult> selected = featureLayer.getSelectedFeaturesAsync();
FeatureQueryResult features = null;
try {
  features = selected.get();
} catch (ExecutionException | InterruptedException e) {
  // ... deal with exception
}


// check there is at least one selected feature
if (!features.iterator().hasNext()) {
  //.., deal with no features being selected
}


// get the first selected feature and load it
final ArcGISFeature feature = (ArcGISFeature) features.iterator().next();
feature.loadAsync();
feature.addDoneLoadingListener(() -> {
  // now feature is loaded we can update it; change attribute and geometry (here the point geometry is moved North)


  feature.getAttributes().put("confirmed", 1);
  Point currentLoc = (Point) feature.getGeometry();
  Point updatedLoc = new Point(currentLoc.getX(), currentLoc.getY() + 50000, mapView.getSpatialReference());
  feature.setGeometry(updatedLoc);


  // update the feature in the table
  arcGISFeatureTable.updateFeatureAsync(feature).addDoneListener(() -> {
    // if dealing with ServiceFeatureTable, apply edits after making updates; if editing locally, then edits can
    // be synchronized at some point using the SyncGeodatabaseTask.
    if (arcGISFeatureTable instanceof ServiceFeatureTable) {
      ServiceFeatureTable serviceFeatureTable = (ServiceFeatureTable) arcGISFeatureTable;


      // can call getUpdatedFeaturesCountAsync to verify number of updates to be applied before calling applyEditsAsync


      final ListenableFuture<List<FeatureEditResult>> listenerResult = serviceFeatureTable.applyEditsAsync();
      listenerResult.addDoneListener(() -> {
        try {
          List<FeatureEditResult> featureEditResults = listenerResult.get();
          // ... if required, can check the edits applied in this operation by using returned FeatureEditResult
        } catch (ExecutionException | InterruptedException e) {
          // ... deal with exception
        }
      });
      // if required, can check the edits applied in this operation by using returned FeatureEditResult
      // ... check updated results
    }
  });
});



// get selected features from the layer for this ArcGISFeatureTable
final FeatureLayer featureLayer = arcGISFeatureTable.getFeatureLayer();
final ListenableFuture<FeatureQueryResult> selected = featureLayer.getSelectedFeaturesAsync();
FeatureQueryResult features = null;
try {
  features = selected.get();
} catch (ExecutionException | InterruptedException e) {
  // ... deal with exception
}


// check atleast a feature was selected
if (!features.iterator().hasNext()) {
  // ... deal with no features being selected
}


// delete the selected features
arcGISFeatureTable.deleteFeaturesAsync(features).addDoneListener(() -> {
  //if dealing with ServiceFeatureTable, apply edits after making updates; if editing locally, then edits can
  // be synchronized at some point using the SyncGeodatabaseTask.
  if (arcGISFeatureTable instanceof ServiceFeatureTable) {
    ServiceFeatureTable serviceFeatureTable = (ServiceFeatureTable) arcGISFeatureTable;


    // can call getDeletedFeaturesCountAsync() to verify number of deletes to be applied before calling applyEditsAsync


    final ListenableFuture<List<FeatureEditResult>> listenableResult = serviceFeatureTable.applyEditsAsync();
    listenableResult.addDoneListener(() -> {
      try {
        List<FeatureEditResult> featureEditResults = listenableResult.get();
        // ... if required, can check the edits applied in this operation by using returned FeatureEditResult
      } catch (ExecutionException | InterruptedException e) {
        // ... deal with exception
      }
    });
    // if required, can check the edits applied in this operation by using returned FeatureEditResult
  }
});


// --- Query ---


// create a buffer from the point
final Polygon searchGeometry = GeometryEngine.buffer(mapPoint, 5000);

// create a query to find which features are contained by the query geometry
QueryParameters queryParams = new QueryParameters();
queryParams.setGeometry(searchGeometry);
queryParams.setSpatialRelationship(QueryParameters.SpatialRelationship.CONTAINS);

// select based on the query
final ListenableFuture<FeatureQueryResult> selectFuture =
    arcGisFeatureTable.getFeatureLayer().selectFeaturesAsync(queryParams, FeatureLayer.SelectionMode.NEW);
// if required, can listen to the future to perform an action when features are selected



// TODO:



// TODO:



// TODO:



//specifying the layer to identify, where to identify, tolerance around point, to return pop-ups only, and
// maximum results
final ListenableFuture<IdentifyLayerResult> identifyFuture =
  mapView.identifyLayerAsync(layer, screenPoint, 20, false, 25);

// add a listener to the future
identifyFuture.addDoneListener(() -> {
  try {
    // get the identify results from the future - returns when the operation is complete
    IdentifyLayerResult identifyLayerResult = identifyFuture.get();

    // a reference to the feature layer can be used, for example, to select identified features
    if (identifyLayerResult.getLayerContent() instanceof FeatureLayer) {
      FeatureLayer featureLayer = (FeatureLayer) identifyLayerResult.getLayerContent();
      // select all features that were identified
      List<Feature> features =
        identifyLayerResult.getElements().stream().map(f -> (Feature) f).collect(Collectors.toList());
      featureLayer.selectFeatures(features);
    }

  } catch (InterruptedException | ExecutionException ex) {
    // ... deal with exceptions thrown from the async identify operation
  }
});

Samples

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