Skip To Content ArcGIS for Developers Sign In Dashboard

Query Top Features (Feature Service/Layer)

  • URL:https://<featurelayer-url>/queryTopFeatures
  • Required Capability:Query
  • Version Introduced:10.7

Description

The queryTopFeatures operation is performed on a feature service layer resource. This operation returns a feature set based on the top features by order within a group. For example, when querying counties in the United States, you want to return the top five counties by population in each state. To do this, you can use queryTopFeaures to group by state name, order by desc on the population and return the first five rows from each group (state).

The topFilter parameter is used to set the group by, order by, and count criteria used in generating the result. The operation also has many of the same parameters (for example, where and geometry) as the layer query operation. However, unlike the layer query operation, queryTopFeatures does not support parameters such as outStatistics and its related parameters or returnDistinctValues. Consult the advancedQueryCapabilities layer property for more details. For example, queryTopFeatures will support pagination if "supportsPaginationOnAggregatedQueries" is true.

If the feature service supports the queryTopFeatures operation, it will include "supportsTopFeaturesQuery": true, in the advancedQueryCapabilities layer property.

Note:

In ArcGIS Enterprise, there is no link from the REST services directory layer page to the queryTopFeatures html page. For layers with "supportsTopFeaturesQuery": true, you can access the queryTopFeatures html page using the following format: https://<featurelayer-url>/queryTopFeatures.

Request parameters

ParameterDetails
f

Description: The response format. The default response format is html.

Values: html | json | pbf | geojson

Note:
The values supported are defined by the "supprotedQueryFormats" layer property.

where

Description: A WHERE clause for the query filter.

SQL '92 WHERE clause syntax on the fields in the layer is supported for most data sources. Some data sources have restrictions

For more information, see Query (Feature Service/Layer)

objectIds

Description: The object IDs of the layer or table to be queried.

Syntax: objectIds=<objectId1,objectId2>

Example: objectIds=42,93

time

Description: The time instant or the time extent to query.

Time instant

Syntax: time=<timeInstant>

Example: time=1199145600000 (1 Jan 2008 00:00:00 GMT)

Time extent

Syntax: time=<startTime>, <endTime>

Example: time=1199145600000, 1230768000000 (1 Jan 2008 00:00:00 GMT to 1 Jan 2009 00:00:00 GMT)

A null value specified for start time or end time will represent infinity for start or end time, respectively. Example: time=null, 1230768000000

topFilter

Description: The JSON parameters that define the aggregation of the data. groupByFields define the field or fields used to aggregate your data. topCount defines the number of features returned from the top features query and is a numeric value. orderByFields defines the order in which the top features will be returned. orderByFields can be specified in either ascending (asc) or descending (desc) order, ascending being the default.

Syntax: {"groupByFields": <"field1, field2, ..., <fieldN">, "topCount": <topCount>, "orderByFields":<"field1">}

Example: {"groupByFields": "worker", "topCount": 1, "orderByFields": "employeeNumber"}

geometry

Description: The geometry to apply as the spatial filter. The structure of the geometry is the same as the structure of the JSON geometry objects returned by the ArcGIS REST API. In addition to the JSON structures, you can specify the geometry of envelopes and points with a simple comma-separated syntax.

Syntax:

  • JSON structures: geometryType=<geometryType>&geometry={geometry}
  • Envelope simple syntax: geometryType=esriGeometryEnvelope&geometry=<xmin>,<ymin>,<xmax>,<ymax>
  • Point simple syntax: geometryType=esriGeometryPoint&geometry=<x>,<y>

Examples:

  • geometryType=esriGeometryEnvelope&geometry={xmin: -104, ymin: 35.6, xmax: -94.32, ymax: 41}

  • geometryType=esriGeometryEnvelope&geometry=-104,35.6,-94.32,41

  • geometryType=esriGeometryPoint&geometry=-104,35.6

geometryType

Description: The type of geometry specified by the geometry parameter. The geometry type can be an envelope, a point, a line, or a polygon. The default geometry type is an envelope.

Values: esriGeometryPoint | esriGeometryMultipoint | esriGeometryPolyline | esriGeometryPolygon | esriGeometryEnvelope

inSR

Description: The spatial reference of the input geometry.

The spatial reference can be specified as either a well-known ID or as a spatial reference JSON object.

If the inSR is not specified, the geometry is assumed to be in the spatial reference of the map.

spatialRel

Description: The spatial relationship to be applied to the input geometry while performing the query. The supported spatial relationships include intersects, contains, envelope intersects, within, and so on. The default spatial relationship is intersects ( esriSpatialRelIntersects).

Values: esriSpatialRelIntersects | esriSpatialRelContains | esriSpatialRelCrosses | esriSpatialRelEnvelopeIntersects | esriSpatialRelIndexIntersects | esriSpatialRelOverlaps | esriSpatialRelTouches | esriSpatialRelWithin

distance

Description: The buffer distance for the input geometries. The distance unit is specified by units. For example, if the distance is 100, the query geometry is a point, units is set to meters, and all points within 100 meters of the point are returned.

Syntax: distance=<distance>

Example: distance=100

The geodesic buffer is created based on the datum of the output spatial reference if it exists. If there is no output spatial reference, the input geometry spatial reference is used. Otherwise, the native layer spatial reference is used to generate the geometry buffer used in the query.

This parameter only applies if supportsQueryWithDistance is true.

units

Description: The unit for calculating the buffer distance. If unit is not specified, the unit is derived from the geometry spatial reference. If the geometry spatial reference is not specified, the unit is derived from the feature service data spatial reference.

This parameter only applies if supportsQueryWithDistance is true.

Values: esriSRUnit_Meter | esriSRUnit_StatuteMile | esriSRUnit_Foot | esriSRUnit_Kilometer | esriSRUnit_NauticalMile | esriSRUnit_USNauticalMile

outFields

Description: The list of fields to be included in the returned result set. This list is a comma-delimited list of field names.

You can also specify the wildcard "*" as the value of this parameter. In this case, the query results include all the field values.

Example: outFields=NAME,ST,AREA

Example (wildcard usage): outFields=*

returnGeometry

Description: If true, the result includes the geometry associated with each feature returned. The default is true.

Values: true | false

maxAllowableOffset

Description: This option can be used to specify the maxAllowableOffset to be used for generalizing geometries returned by the query operation.

The maxAllowableOffset is in the units of outSR. If outSR is not specified, maxAllowableOffset is assumed to be in the unit of the spatial reference of the map.

Example: maxAllowableOffset=2

geometryPrecision

Description: This option can be used to specify the number of decimal places in the response geometries returned by the Query operation.

This applies to X and Y values only (not m or z-values).

Example: geometryPrecision=3

outSR

Description: The spatial reference of the returned geometry.

The spatial reference can be specified as either a well-known ID or as a spatial reference JSON object.

If outSR is not specified, the geometry is returned in the spatial reference of the map.

returnIdsOnly

Description: If true, the response only includes an array of object IDs. Otherwise, the response is a feature set. The default is false.

While there is a limit to the number of features included in the feature set response, there is no limit to the number of object IDs returned in the ID array response. Clients can exploit this to get all the query conforming object IDs by specifying returnIdsOnly=true and subsequently requesting feature sets for subsets of object IDs.

When objectIds are specified, setting this parameter to true is invalid.

Values: true | false

returnCountOnly

Description: If true, the response only includes the count(number of features/records) that would be returned by a query. Otherwise, the response is a feature set. The default is false. This option supersedes the returnIdsOnly parameter. If returnCountOnly = true, the response will return both the count and the extent.

Values: true | false

returnExtentOnly

Description: If true, the response only includes the extent of the features that would be returned by the query. If returnCountOnly=true, the response will return both the count and the extent. The default is false. This parameter applies only if the supportsReturningQueryExtentproperty of the layer is true.

Values: true | false

Note:

At 10.3, this option is only available for hosted feature services. At 10.3.1, this option is available for hosted and nonhosted feature services.

returnZ

Description: If true, Z values are included in the results if the features have Z values. Otherwise, Z values are not returned. The default is false.

This parameter only applies if returnGeometry=true, and the layer's hasZ property is true.

returnM

Description: If true, M values are included in the results if the features have M values. Otherwise, M values are not returned. The default is false.

This parameter only applies if returnGeometry=true, and the layer's hasM property is true .

resultType

Optional

Description: The resultTypeparameter can be used to control the number of features returned by the query operation.

Values: none | standard | tile

Note:

  • Support for the resultType parameter is advertised on the layer metadata. For example, {"supportQueryWithResultType": true/false}.
  • The maxTileRecordCount and maxStandardRecordCount are server managed (determined by the server). The feature service assigns the max record count relevant to the resultType query parameter.
    • If no resultType query parameter is used, the default maxRecordCount is always used. This can be the default server assigned maxRecordCount (1,000, 2,000) or an overwritten value provided by the service owner/admin.
    • The resultType=tile is used when the client is using a virtual tiling scheme when querying features. A virtual tiling scheme works similar to tiles in a tiled map service layer.
    • The resultType=standard is used with a nontiled query in which the client will send only one query for the full extent.
  • Server advertises max record counts with the layer metadata. For example, for points data, the following max record count is returned:
    • Ex: {"maxRecordCount" : 2000, "tileMaxRecordCount" : 8000, "standardMaxRecordCount" : 32000}.
  • The values of the max record counts might vary, based on the type of the data ( polygon, point, polyline, table).
  • Pagination queries also support resultType query parameter.
    • If the resultType is specified and the resultRecordCount is not specified with the resultOffset, the server will determine the maxRecordCount relevant to the resultType query parameter.
    • The client can supply the resultRecordCount parameter in the request. This cannot be greater than the standard/tile maxRecordCount value if resultType is used.
  • The layer metadata includes maxRecordCountFactor that can be configured from the admin API. The server maxRecordCountFactor for the tileMaxRecordCount and standardMaxRecordCount is used as a multiplier for the server base value.
    • All maxRecordCount values are adjusted with the maxRecordCountFactor.

Examples

Example 1: (When resultType=none)

The default maxRecordCount is always used. This can be the default server assigned ("maxRecordCount" : 2000) or an overwritten value by the service owner/admin.

https://services.myserver.com/OrgID/ArcGIS/rest/services/USAStatesRiversCapitals/FeatureServer/2/query?where=1=1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=
&units=esriSRUnit_Meter&outFields=*&returnGeometry=true&multipatchOption=&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false
&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=
&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&quantizationParameters=&sqlFormat=none&f=html&token=

Example 2: (When resultType=standard)

The standardMaxRecordCount is always used. Ex: ("standardMaxRecordCount" : 32000) for point and table layer.

https://services.myserver.com/OrgID/ArcGIS/rest/services/USAStatesRiversCapitals/FeatureServer/2/query?where=1=1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=standard&distance=
&units=esriSRUnit_Meter&outFields=*&returnGeometry=true&multipatchOption=&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false
&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=
&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&quantizationParameters=&sqlFormat=none&f=html&token=

Example 3: (When resultType=tile)

The tileMaxRecordCount is always used. Ex: ("tileMaxRecordCount" : 8000) for point layer.

https://services.myserver.com/OrgID/ArcGIS/rest/services/USAStatesRiversCapitals/FeatureServer/2/query?where=1=1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=tile&distance=
&units=esriSRUnit_Meter&outFields=*&returnGeometry=true&multipatchOption=&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false
&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=
&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&quantizationParameters=&sqlFormat=none&f=html&token=

Example usage

Example1: Querying the last two deliveries made by an employee.

https://services.myserver.com/OrgID/ArcGIS/services/Hosted/deliveries/FeatureServer/0/queryTopFeatures?where=&objectIds=&time=&topFilter=%7B%22groupByFields%22%3A+%27employeeName%27%2C+%22topCount%22%3A+2%2C+%22orderByFields%22%3A+%27deliveryNumber desc%27%7D&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&distance=&units=esriSRUnit_Foot&outFields=employeeName,deliveryNumber&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnZ=false&returnM=false&resultType=&f=pjson

JSON Response syntax

{
 "exceededTransferLimit": <true | false>,
 "features": [<feature1, feature2>],
 "fields" : [
      {"name" : "<fieldName1>", "type" : "<fieldType1>", "alias" : "<fieldAlias1>", "length" : "<length1>"},
      {"name" : "<fieldName2>", "alias" : "<fieldAlias2>", "type" : "<fieldType2>", "length" : "<length2>"}],
 "geometryType": "<geometryType>",
 "spatialReference": {<spatialReference>},
 "globalIdFieldName": "<fieldName>",
 "objectIdFieldName": "<fieldName>",
 "hasZ": <true | false>,
 "hasM": <true | false>
}

JSON Response example

{
 "exceededTransferLimit": false,
 "features": [
  {
   "attributes": {
   "employeeName": "Jason",
	  "deliveryNumber": 25
   },
    "geometry": {
     "x": -76.9505202472378,
     "y": 36.02168768903079
     }
   },
  {
   "attributes": {
   "employeeName": "Jason",
	  "deliveryNumber": 24
   },
   "geometry": {
    "x": -80.28022828451529,
    "y": 34.9904342763449
   }
  },
 ],
 "fields": [
  {
   "name": "employee",
   "alias": "employee",
   "type": "esriFieldTypeString",
   "length": 100
  },
    {
   "name": "deliveryNumber",
   "alias": "deliveryNumber",
   "type": "esriFieldTypeDouble",
   "length": 1000
  }
 ],
 "geometryType": "esriGeometryPoint",
 "spatialReference": {
  "wkid": 4326,
  "latestWkid": 4326
 },
 "globalIdFieldName": "globalid",
 "objectIdFieldName": "objectid",
 "hasZ": false,
 "hasM": false
}