Query (Map Service/Layer)
- URL:https:// <layer-url>/query
- Required Capability:Data
- Version Introduced:9.3
Description
Prior to 10.0, the query operation could only be performed on layers. From 10.0 onward, the query operation can be performed on tables and layers. Querying annotation is supported at the layer level, however querying annotation at the sublayer level is not supported.
Note that all parameters related to geometry will be ignored when querying tables.
The result of this operation is a feature set. This feature set contains feature objects including the values for the fields requested by the user. For layers, if you request geometry information, the geometry of each feature is also returned in the feature set. For tables, the feature set does not include geometries.
New in 10.7.1
- Map Services now support the protocol buffer (pbf) format. This format is supported on map services from ArcGIS Pro. The supportedQueryFormats layer property will list pbf if it is available on the layer.
New in 10.7
- Support for amf output format was removed.
New in 10.6.1
- Supports the following new parameters.
- historicMoment to query from a given moment in an archive enabled layer.
- Supports returning 'number of unique values', instead of a list of unique value, off a field when values for both returnCountOnly and returnDistinctValues are true.
- Map services now support quantizationParameters.
New in 10.5
- Supports the following new parameters.
- datumTransformations to provide a desired datum transformation to be applied while features get projected.
- rangeValues to set values to ranges.
- parameterValues to set value to parameterized filters.
- Supports passing in sql expressions in outStatistics. Check supportsSqlExpression on resources to find out whether the layer/table allows sql expressions.
New at 10.4
- Supports GeoJSON as a response format.
- Supports passing in expressions in orderByFields and groupByFieldsForStatistics. When useStandardizedQueries is true, you can pass in expressions that conform to StandardizedQueries specifications. Otherwise, any expressions that are supported by the underlying database can be passed in.
New at 10.3.1
- The exceededTransferLimit property is now included in the JSON response when paging through a query result with the resultOffset and resultRecordCount parameters. When exceededTransferLimit is true, it indicates there are more query results and you can continue to page through the results. When exceededTransferLimit is false, it indicates that you have reached the end of the query results.
When not using the resultOffset and resultRecordCount parameters, the exceededTransferLimit property may also be included in the query results. In this case, the property will be true only if the number of records exceeds the maximum number configured by the server administrator.
In some cases when using the resultOffset and resultRecordCount parameters, the exceededTransferLimit property may be included in the query results even though the value specified in the resultRecordCount has not been exceeded. This is due to internal spatial index filtering of the query results. For this reason you should always rely on the exceededTransferLimit property to determine if you should page through results rather than relying on the number of results returned from each page. In some extreme cases zero results can be returned but the exceededTransferLimit property will be returned. In these cases you should continue paging though your results until exceededTransferLimit is no longer returned.
New at 10.3
- Supports pagination in a query layer. UseresultOffset and resultRecordCount parameters to page through a query result.
- Note that when you pass in one of these two parameters and orderByFields is left empty, the map service uses the object-id field to sort the result. For a query layer with a pseudocolumn as the object-id field (for example, FID), you must provide orderByFields or else the query will fail.
- query now supports true curves in an input geometry parameter.
- query now returns true curves in output geometries when the returnTrueCurves parameter is set to true.
- Learn more about JSON Curve Objects in Geometry Objects.
New at 10.2
- The where parameter value must conform to the standardized queries, if the layer/table resource advertises useStandardizedQueries is true. Learn more about standardized queries.
- outStatistics now supports the gdbVersion parameter.
New at 10.1 SP1
- Support for a new parameter named returnDistinctValues that accepts a Boolean value was added. When true, the query result would contain distinct values based on the fields specified in the outFields parameter.
- outStatistics now supports the geometry parameter.
New at 10.1
- Support for orderByFields, outStatistics, and groupByFieldsForStatistics was added for both layers and tables.
- Support for returnZ and returnM was added for layers. Default value for returnZ and returnM is false.
- When output format f is kmz, the result would always contain a z-value irrespective of the returnZ property value. If the feature geometry does not support z, a default value of 0 would be returned for z.
- Support for the gdbVersion parameter was added. Use this parameter to specify the geodatabase version to query.
- The geometryPrecision parameter was introduced. This option can be used to specify the number of decimal places in the response geometries returned by the query operation.
- JSON response contains an optional property exceededTransferLimit. This property will be true only if the number of records exceeds the maximum number configured by the server administrator.
New at 10.0 SP1
- At 10.0 SP1, support for returning the count (number of features/records) that would be returned by a query was added for both layers and tables.
New at 10.0
- Support for querying layers and tables based on time was added at 10.0. For time-aware layers, users can use the time parameter to specify the time instant or the time extent to query.
- A new returnIdsOnly parameter was introduced. If set to false (default), the response will be a feature set. If true, the response will be an array of object IDs.
- Note that while there is a limit on the number of features included in the feature set response, there is no limit on 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.
- Support for query based on objectIds was added.
- Support for f=amf was added.
- Support for generalizing geometries returned by the query operation was added.
- The
query operation returns an additional fields array (JSON and AMF only) that contains array of field information for returned values. This array contains only fields specified in the outFields parameter. See Layer/Table for details on fields. Note:
The domains member is not included in field information objects returned with the response.
- The fieldAliases member is deprecated.
You can provide arguments to the query operation as query parameters defined in the parameters table below.
Request Parameters
Parameter |
Details | ||||||||
---|---|---|---|---|---|---|---|---|---|
f |
The response format. The default response format is HTML. Protocol buffer (pbf) format is only supported when the supportedQueryFormat property on the layer includes pbf.
Note: f=kmz would always return z-values. Values: html | json | geojson | kmz | pbf (default, when returnIdsOnly=false and returnCountOnly=false) Values: html | json (when outStatistics is specified) Values: html | json | geojson | pbf (when either returnIdsOnly=true or returnCountOnly=true is specified) |
||||||||
text |
A literal search text. If the layer has a display field associated with it, the server searches for this text in this field. This parameter is shorthand for a WHERE clause of where <displayField> like '%<text>%'. The text is case sensitive. This parameter is ignored if the WHERE parameter is specified. Example: text=Los |
||||||||
geometry |
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, for envelopes and points, you can specify the geometry with a simpler comma-separated syntax. Syntax:
|
||||||||
geometryType |
The type of geometry specified by the geometry parameter. The geometry type can be an envelope, point, line, or polygon. The default geometry type is an envelope. Values: esriGeometryPoint | esriGeometryMultipoint | esriGeometryPolyline | esriGeometryPolygon | esriGeometryEnvelope |
||||||||
inSR |
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 |
The spatial relationship to be applied on 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 | esriSpatialRelRelation |
||||||||
relationParam |
The spatial relate function that can be applied while performing the query operation. An example for this spatial relate function is "FFFTTT***." For more information on this spatial relate function, refer to the documentation for the spatial relate function. |
||||||||
where |
A WHERE clause for the query filter. Any legal SQL WHERE clause operating on the fields in the layer is allowed. Example: where=POP2000 > 350000 When standardized queries are enabled, where = CHAR_LENGTH(cntry_name) > 18. |
||||||||
objectIds |
The object IDs of this layer or table to be queried. Note: There might be a drop in performance if the layer/table data source resides in an enterprise geodatabase and more than 1,000 objectIds are specified. Syntax: objectIds=<objectId1>, <objectId2> Example: objectIds=37, 462 |
||||||||
time |
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. |
||||||||
distance | 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 esriSRUnit_Meter, 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 | The unit for calculating the buffer distance. This parameter only applies if supportsQueryWithDistance is true. Values: esriSRUnit_Meter | esriSRUnit_StatuteMile | esriSRUnit_Foot | esriSRUnit_Kilometer | esriSRUnit_NauticalMile | esriSRUnit_USNauticalMile | ||||||||
outFields |
The list of fields to be included in the returned result set. This list is a comma-delimited list of field names. If you specify the shape field in the list of return fields, it is ignored. To request geometry, set returnGeometry to true. 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=AREANAME,ST,POP2000 Example (wildcard usage): outFields=* |
||||||||
returnGeometry |
If true, the result set includes the geometry associated with each result. The default is true. Values: true | false Note: This parameter cannot be used with returnDistinctValues. |
||||||||
maxAllowableOffset |
This option was added at 10.0. 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 the 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 |
This option was added at 10.1. 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 |
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. when using outSR with pbf, the pbf format will use coordinate quantization for layer queries. When an output spatial reference is not provided for a query operation, the Map Service derives coordinate quantization parameters from the layer's spatial reference. If the precision in the layer's spatial references is inadequate for the client application's use, it should pass in a spatial reference with suitable precision as the output spatial reference. If the layer's source spatial reference has the desired precision and it is suitable for the client's use, the client can use the source layer's spatial reference as the output spatial reference. |
||||||||
returnIdsOnly |
If true, the response only includes an array of object IDs. Otherwise the response is a feature set. The default is false. Note that while there is a limit on the number of features included in the feature set response, there is no limit on 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. Values: false | true |
||||||||
returnCountOnly |
This option was added at 10.0 SP1. 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. Values: false | true |
||||||||
returnExtentOnly | This option was added at 10.4. 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 supportsReturningQueryExtent property of the layer is true. Values: true | false | ||||||||
orderByFields |
This option was added at 10.1. One or more field names or expressions that the features/records need to be ordered by. Use ASC or DESC for ascending or descending order, respectively.
Note:
Syntax: orderByFields=expression1 <ORDER>, expression2 <ORDER>, expression3 <ORDER> Example: orderByFields=STATE_NAME ASC, RACE DESC, GENDER Example: orderByFields=POPULATION / SHAPE_AREA |
||||||||
outStatistics |
This option was added at 10.1. The definitions for one or more field-based statistics to be calculated.
Note:
Values: An array of statistic definitions. A statistic definition specifies the type of statistic, the field on which it is to be calculated, and the resulting output field name. Syntax:
Example:
|
||||||||
groupByFieldsForStatistics |
This option was added at 10.1. One or more field names using the values that need to be grouped for calculating the statistics.
Note:
Syntax: groupByFieldsForStatistics=expression1, expression2 Example: groupByFieldsForStatistics=STATE_NAME, GENDER Example: groupByFieldsForStatistics=extract(month from incident_date) to group by month when StandardizedQueries is enabled. |
||||||||
returnZ |
This option was added at 10.1. If true, the z-values will be included in the results if the features have z-values. Otherwise z-values are not returned. The default is false.
Note: This parameter only applies if returnGeometry is true. |
||||||||
returnM |
This option was added at 10.1. If true, m-values will be included in the results if the features have m-values. Otherwise, m-values are not returned. The default is false.
Note: This parameter only applies if returnGeometry is true. |
||||||||
gdbVersion |
This option was added at 10.1. GeoDatabase version to query. This parameter applies only if the hasVersionedData property of the service and the isDataVersioned property of the layers queried are true. If this is not specified, the query will apply to the published map's version. Syntax: gdbVersion=<geodatabase version> Example: gdbVersion=sde.USER1 |
||||||||
returnDistinctValues |
This option was added at 10.1 SP1. If true, returns distinct values based on the fields specified in outFields. This parameter applies only if the supportsAdvancedQueries property of the layer is true. Syntax: returnDistinctValues=<true | false> Example: returnDistinctValues=true Note: This parameter cannot be used when returnGeometry is true. |
||||||||
returnTrueCurves |
This option was added at 10.3. If true, returns true curves in output geometries; otherwise, curves are converted to densified polylines or polygons. Syntax: returnTrueCurves=<true | false> Example: returnTrueCurves=true |
||||||||
resultOffset |
This option was added at 10.3. This option can be used for fetching query results by skipping the specified number of records and starts from the next record (for example, resultOffset + 1th). The Default is 0. This parameter only applies if supportsPagination is true. You can use this option to fetch records that are beyond maxRecordCount. For example, if maxRecordCount is 1,000, you can get the next 100 records by setting resultOffset=1000 and resultRecordCount = 100; query results can return the results in the range of 1,001 to 1,100. |
||||||||
resultRecordCount |
This option was added at 10.3. This option can be used for fetching query results up to the resultRecordCount specified. When resultOffset is specified but this parameter is not, the map service defaults it to maxRecordCount. The maximum value for this parameter is the value of the layer's maxRecordCount property. This parameter only applies if supportsPagination is true. Example: resultRecordCount=10 to fetch up to 10 records |
||||||||
datumTransformation | This option was added at 10.5 Use this parameter to apply a datum transformation while projecting geometries in the results when outSR is different than the layer's spatial reference. Note: while specifying transformation, you need to think about which datum transformation best applicable to project the layer (not the map service) to the outSR. sourceSpatialReference property in the layer resource reports which spatial reference features are stored in the source dataset. For a list of valid datum transformation ID values and well-known text strings, see Geographic transformations. Syntax: datumTransformation=wkid1. Examples: datumTransformation=1623 to apply a transformation. Syntax: datumTransformation={<dt1>}. Examples: datumTransformation={"geoTransforms":[{"wkid":108889,"transformForward":true},{"wkid":1622,"transformForward":false}]} to apply a composite transformation. For more information on datum transformation, please see transformation parameter in Project operation. | ||||||||
rangeValues | This option was added at 10.5 Allows you to filter features from the layer that are within the specified range instant or extent. Note: Check rangeInfos at the layer resources for the available ranges. Syntax:
Example:
| ||||||||
quantizationParameters | This option was added at 10.6.1 Used to project the geometry onto a virtual grid, likely representing pixels on the screen. Note:
Examples Example 1:
Example 2:
Example 3:
Example 4:
| ||||||||
parameterValues | This option was added at 10.5 Allows you to filter the features layers by specifying value(s) to an array of pre-authored parameterized filters for those layers. When value is not specified for any parameter in a request, the default value, that is assigned during authoring time, gets used instead. When a parameterInfo allows multiple values, you must pass them in an array. Note: Check parameterInfos at the layer resources for the available parameterized filters, their default values and expected data type. Syntax:
Example:
| ||||||||
historicMoment | This option was added at 10.6.1. The historic moment to query. This parameter applies only if the layer is archiving enabled and the supportsQueryWithHistoricMoment property is set to true. This property is provided in the layer resource. If historicMoment is not specified, the query will apply to the current features. Syntax: historicMoment=<Epoch time in milliseconds> Example: historicMoment=1199145600000 |
Example usage
Example 1: Query using the text parameter on the states layer of the ESRI_StateCityHighway_USA on sampleserver1:
Example 2: Query using a WHERE statement on the same layer. The output is JSON format:
Example 3: Query strings are case sensitive. In this example, UPPER is used to make the query case insensitive:
Example 4: Querying the same states layer using geometry (envelope):
Example 5: Querying the states layer by both geometry (envelope) and a WHERE statement:
Example 6: Querying the states layer by a WHERE statement, specifying a list of fields to return, and requesting no geometry in the results:
Example 7: Querying the states layer by text parameter and requesting the geometry with the well-known ID of 102113 (Web Mercator):
Example 8: Query a table using a WHERE clause and return object IDs only:
Example 9: Using groupByFieldsForStatistics and outStatistics:
Example 10: Page through a query result using resultOffset and resultRecordCount. Requesting to skip first 5 records and return next 10 counties in California order by population:
https://mymachine/arcgis/rest/services/USA/MapServer/3/query?where=STATE_NAME='California'&outFields=Name,Population&returnGeometry=false&resultOffset=5&resultRecordCount=10&orderByFields=Population&f=pjson
JSON Response syntax
When returnIdsOnly=false and returnCountOnly=false
{
"displayFieldName" : "<displayFieldName>",
//fieldAliases deprecated at 10
"fieldAliases" : {
"<fieldName1>" : "<fieldAlias1>",
"<fieldName2>" : "<fieldAlias2>"
},
"fields" : [
{"name" : "<fieldName1>", "type" : "<fieldType1>", "alias" : "<fieldAlias1>", "length" : "<length1>"},
{"name" : "<fieldName2>", "type" : "<fieldType2>", "alias" : "<fieldAlias2>", "length" : "<length2>"}
],
"geometryType" : "<geometryType>", //for layers only
"spatialReference" : <spatialReference>, //for layers only
"hasZ" : <true|false>, //added in 10.1
"hasM" : <true|false>, //added in 10.1
"features" : [ //features may include geometry for layers only
<feature1>, <feature2>
]
}
JSON Response syntax
When returnIdsOnly=true
{
"objectIdFieldName" : "<objectIdFieldName>",
"objectIds" : [ <objectId1>, <objectId2> ]
}
JSON Response syntax
When returnCountOnly=true
{
"count" : <count>
}
JSON Response syntax
When groupByFieldsForStatistics and outStatistics are specified
{
"displayFieldName": "",
"fieldAliases": {
"alias1": "fieldAlias1",
"alias2": "fieldAlias2"
},
"fields":
[
{
"name": "fieldName1",
"type": "fieldType1",
"alias": "fieldAlias1",
"length": fieldLength1
},
{
"name": "fieldName2",
"type": "fieldType2",
"alias": "fieldAlias2",
"length": fieldLength2
}
],
"features": [<feature1>, <feature2>] //Feature object without geometry
}
JSON Response example
When returnIdsOnly=false and returnCountOnly=false
{"displayFieldName" : "AREANAME",
"fieldAliases" : { "ST" : "ST", "POP2000" : "Population - 2000", "AREANAME" : "City Name"
},
"fields" : [{ "name" : "ST", "alias" : "ST", "type" : "esriFieldTypeString", "length" : 2},
{
"name" : "POP2000", "alias" : "Population - 2000", "type" : "esriFieldTypeInteger"},
{ "name" : "AREANAME", "alias" : "City Name", "type" : "esriFieldTypeString", "length" : 255}],
"geometryType" : "esriGeometryPoint","spatialReference" : {"wkid" : 4326},"features" : [ { "attributes" : { "ST" : "CA", "POP2000" : 3694820, "AREANAME" : "Los Angeles" }, "geometry" : { "x" : -118.37, "y" : 34.086 } }, { "attributes" : { "ST" : "CA", "POP2000" : 461522, "AREANAME" : "Long Beach" }, "geometry" : { "x" : -118.15, "y" : 33.80 } }]}
JSON Response example
When returnIdsOnly=true
{
"objectIdFieldName":"objectid",
"objectIds":[1,2,3,4,5,7]
}
JSON Response example
When returnCountOnly=true
{
"count":48
}
JSON Response example
When groupByFieldsForStatistics and outStatistics are specified
{
"displayFieldName": "",
"fieldAliases": {
"sub_region": "SUB_REGION",
"Population_2007": "Population_2007",
"Average_Family_Size": "Average_Family_Size"
},
"fields": [
{
"name": "sub_region",
"type": "esriFieldTypeString",
"alias": "SUB_REGION",
"length": 20
},
{
"name": "Population_2007",
"type": "esriFieldTypeDouble",
"alias": "Population_2007"
},
{
"name": "Average_Family_Size",
"type": "esriFieldTypeDouble",
"alias": "Average_Family_Size"
}
],
"features": [
{
"attributes": {
"sub_region": "Pacific",
"Population_2007": 49731702,
"Average_Family_Size": 3.2439999999999998
}
},
{
"attributes": {
"sub_region": "Mountain",
"Population_2007": 21492235,
"Average_Family_Size": 3.165
}
},
{
"attributes": {
"sub_region": "New England",
"Population_2007": 14515009,
"Average_Family_Size": 3.0249999999999999
}
},
{
"attributes": {
"sub_region": "West North Central",
"Population_2007": 20384497,
"Average_Family_Size": 3.044285714285714
}
},
{
"attributes": {
"sub_region": "East North Central",
"Population_2007": 47176974,
"Average_Family_Size": 3.0940000000000003
}
},
{
"attributes": {
"sub_region": "Middle Atlantic",
"Population_2007": 41116339,
"Average_Family_Size": 3.1566666666666663
}
},
{
"attributes": {
"sub_region": "South Atlantic",
"Population_2007": 58943344,
"Average_Family_Size": 3.0333333333333332
}
},
{
"attributes": {
"sub_region": "East South Central",
"Population_2007": 18077309,
"Average_Family_Size": 3.0275000000000003
}
},
{
"attributes": {
"sub_region": "West South Central",
"Population_2007": 34910821,
"Average_Family_Size": 3.1124999999999998
}
}
]
}