Requirements: ArcGIS Online hosted feature service(s) or ArcGIS Server service(s) 10.1 or greater.
Query is used to define parameters to filter a layer's features, either by its attributes or its geometry. Once a Query object is created, you can set its properties to conform to the filtering requirements of the application. Once a Query object is created and its properties are set, it is then ready to be passed into an executable function in QueryTask (e.g. QueryTask.execute(Query)
). The returned result is often a FeatureSet.
For example, you may want to view features in your map based on attribute values specified by the user (e.g. User inputs a state name to zoom to the state). To do this, create a query object and use the where
property to query an attribute using SQL:
require(["esri/tasks/QueryTask", "esri/tasks/support/Query"], function(QueryTask, Query){
var queryStatesTask = new QueryTask({
url: "..." // URL of a feature layer representing U.S. states
});
var query = new Query();
query.where = "STATE_NAME = 'California'";
queryStatesTask.execute(query).then(function(result){
// Do something with the resulting FeatureSet (zoom to it, highlight features, get other attributes, etc)
});
});
Querying by geometry/location is also possible with Query. Instead of using where
, use geometry
. For example, let's say you want to build an application that highlights all the cities in a Feature Layer that are within 100 miles of a click on the view. You would set the geometry
to the view click and the distance to 100
:
require(["esri/tasks/QueryTask", "esri/tasks/support/Query"], function(QueryTask, Query){
var queryCitiesTask = new QueryTask({
url: "..." // URL of a feature layer representing U.S. cities
});
var query = new Query();
query.geometry = mapPoint; // mapPoint obtained from view-click event.
query.distance = 100;
query.units = "miles";
query.spatialRelationship = "intersects"; // All features that intersect 100mi buffer
queryCitiesTask.execute(query).then(function(result){
// Returns all U.S. cities as a FeatureSet within 100 miles of view-click
});
});
Constructors
new Query(properties)
properties Object See the properties for a list of all the properties that may be passed into the constructor. |
Property Overview
Name | Type | Summary | |
---|---|---|---|
String | The name of the class. more details | more details | |
Number | Buffer distance for input geometries. more details | more details | |
Geometry | The geometry to apply to the spatial filter. more details | more details | |
Number | Specify the number of decimal places for the geometries returned by the query operation. more details | more details | |
String[] | One or more field names that will be used to group the statistics. more details | more details | |
Number | The maximum allowable offset used for generalizing geometries returned by the query operation. more details | more details | |
String | Parameter dictates how the geometry of a multipatch feature will be returned. more details | more details | |
Number | Number of features to retrieve. more details | more details | |
Number[] | A comma delimited list of ObjectIds for the features in the layer/table being queried. more details | more details | |
String[] | One or more field names that will be used to order the query results. more details | more details | |
String[] | Attribute fields to include in the FeatureSet. more details | more details | |
SpatialReference | The spatial reference for the returned geometry. more details | more details | |
StatisticDefinition[] | The definitions for one or more field-based statistics to be calculated. more details | more details | |
Symbol | Specify the pixel level to be identified on the X and Y axis. more details | more details | |
Object | Used to project the geometry onto a virtual grid, likely representing pixels on the screen. more details | more details | |
String | The string describes the spatial relationship to be tested when the spatial relationship is | more details | |
Boolean | If | more details | |
Boolean | If | more details | |
Boolean | Set this property to | more details | |
String | The spatial relationship to be applied to the input geometry while performing the query. more details | more details | |
Number | Zero-based index indicating where to begin retrieving features. more details | more details | |
String | Shorthand for a where clause using "like". more details | more details | |
String | The unit for calculating the buffer distance. more details | more details | |
String | A where clause for the query. more details | more details |
Property Details
declaredClassStringreadonly
The name of the class. The declared class name is formatted as
esri.folder.className
.distanceNumber
Buffer distance for input geometries. The distance unit is specified by units property. Query results will include features within the distance specified of the geometry of the query. This parameter only applies if supportsQueryWithDistance is
true
.geometryGeometry
The geometry to apply to the spatial filter. The spatial relationship as specified by
spatialRelationship
is applied to this geometry while performing the query. The valid geometry types are Extent, Point, Multipoint, Polyline, or Polygon.geometryPrecisionNumber
Specify the number of decimal places for the geometries returned by the query operation.
groupByFieldsForStatisticsString[]
One or more field names that will be used to group the statistics. This is only valid when
outStatistics
has been defined. Requires ArcGIS Server service version 10.1 or greater.maxAllowableOffsetNumber
The maximum allowable offset used for generalizing geometries returned by the query operation. The offset is in the units of
spatialReference
. IfspatialReference
is not defined the spatial reference of the map is used.multipatchOptionString
Parameter dictates how the geometry of a multipatch feature will be returned. Currently, the only supported value is
xyFootprint
and the xy footprint of each multipatch geometry will be returned in the result.Example:var queryTask = new QueryTask( ... ); var query = new Query(); query.objectIds = [22]; query.multipatchOption = "xyFootprint"; query.outFields = ["*"]; query.returnGeometry = true; queryTask.execute(query);
numNumber
Number of features to retrieve. Should be used in conjunction with start property. Use this to implement paging and retrieve "pages" of results when querying. If not provided, but an instance of Query has a start property, num defaults to 10.
Default Value: 10objectIdsNumber[]
A comma delimited list of ObjectIds for the features in the layer/table being queried.
orderByFieldsString[]
One or more field names that will be used to order the query results. Specfiy
ASC
(ascending) orDESC
(descending) after the field name to control the order. The default order isASC
.orderByFields
is only supported on MapImageLayers and tables wheresupportsAdvancedQueries = true
. Requires ArcGIS Server service version 10.1 or greater.Example:query.orderByFields = ["STATE_NAME DESC"];
outFieldsString[]
Attribute fields to include in the FeatureSet. Fields must exist in the map layer. You must list actual field names rather than the alias names. You are, however, able to use the alias names when you display the results. You can set field alias names in the map document.
When specifying the output fields, you should limit the fields to only those you expect to use in the query or the results. The fewer fields you include, the faster the response will be.
Each query must have access to the Shape and ObjectId fields for a layer. However, your list of fields does not need to include these two fields.
Example:query.outFields = ["NAME", "STATE_ABBR", "POP04"];
outSpatialReferenceSpatialReference
The spatial reference for the returned geometry. If not specified, the geometry is returned in the spatial reference of the map.
outStatisticsStatisticDefinition[]
The definitions for one or more field-based statistics to be calculated.
outStatistics
is only supported on layers/tables wheresupportsStatistics = true
. IfoutStatistics
is specified the only other query parameters that will be used aregroupByFieldsForStatistics
,orderByFields
,text
andwhere
. Requires ArcGIS Server service version 10.1 or greater.Example:require([ "esri/tasks/support/Query", "esri/tasks/support/StatisticDefinition", ... ], function(Query, StatisticDefinition, ... ) { var query = new Query(); var statisticDefinition = new StatisticDefinition(); statisticDefinition.statisticType = "sum"; statisticDefinition.onStatisticField = "POP2000"; statisticDefinition.outStatisticFieldName = "TotalPop"; query.outStatistics = [statisticDefinition]; ... });
pixelSizeSymbol
Specify the pixel level to be identified on the X and Y axis. Defaults to the base resolution of the dataset if not specified. Applicable only to Image Service layers.
quantizationParametersObject
Used to project the geometry onto a virtual grid, likely representing pixels on the screen. Only works with ArcGIS Online hosted services.
Properties:optionalextent ExtentAn extent defining the quantization grid bounds. Its SpatialReference matches the input geometry spatial reference if one is specified for the query. Otherwise, the extent will be in the layer's spatial reference.
optionalmode StringGeometry coordinates are optimized for viewing and displaying of data. Known Values: view
optionaloriginPosition StringThe integer's coordinates will be returned relative to the origin position defined by this property value. Default is
upper-left
. Known Values: upper-left | lower-leftoptionaltolerance NumberThe size of one pixel in the units of outSpatialReference. This number is used to convert coordinates to integers by building a grid with a resolution matching the tolerance. Each coordinate is then snapped to one pixel on the grid. Consecutive coordinates snapped to the same pixel are removed for reducing the overall response size. The units of tolerance will match the units of outSpatialReference. If outSpatialReference is not specified, then tolerance is assumed to be in the units of the spatial reference of the layer. If tolerance is not specified, the maxAllowableOffset is used. If tolerance and maxAllowableOffset are not specified, a grid of 10,000 * 10,000 grid is used by default.
Example:var query = new Query(); query.quantizationParameters = { mode: "view", originPosition: "upper-left", tolerance: 4820, extent: layer.fullExtent };
relationParamString
The string describes the spatial relationship to be tested when the spatial relationship is
relation
. The Relational functions for ST_Geometry topic has additional details.Example:var query = new Query(); query.relationParam = "FFFTTT***";
returnDistinctValuesBoolean
If
true
then the query returns distinct values based on the fields specified inoutFields
. This parameter applies only ifsupportsAdvancedQueries
property of the layer istrue
. Requires ArcGIS Server 10.1 Service Pack 1 or later.Default Value: falsereturnGeometryBoolean
If
true
, each feature in the FeatureSet includes the geometry. Set tofalse
(default) if you do not plan to include highlighted features on a map since the geometry takes up a significant portion of the response.Default Value: falsereturnZBoolean
Set this property to
true
to include the z-values of each feature in the response FeatureSet. The returnGeometry property must also be set totrue
for this property to have an effect.Default Value: falsespatialRelationshipString
The spatial relationship to be applied to the input geometry while performing the query. The valid values are listed in the table below:
Value Description intersects Part of a feature from feature class 1 intersects a feature from feature class 2. contains Part or all of a feature from feature class 1 is contained within a feature from feature class 2. crosses The feature from feature class 1 crosses a feature from feature class 2. envelope-intersects The envelope of feature class 1 intersects with the envelope of feature class 2. index-intersects The envelope of the query feature class intersects the index entry for the target feature class. overlaps Features from feature class 1 overlap features in feature class 2. touches The feature from feature class 1 touches the border of a feature from feature class 2. within The feature from feature class 1 is completely enclosed by the feature from feature class 2. relation Allows specification of any relationship defined using the Shape Comparison Language. Default Value: intersectsExample:require([ "esri/tasks/support/Query", ... ], function(Query, ... ) { var query = new Query(); query.spatialRelationship = "contains"; ... });
startNumber
Zero-based index indicating where to begin retrieving features. Should be used in conjunction with num property. Use this to implement paging and retrieve "pages" of results when querying. Features are sorted ascending by object ID by default.
textString
Shorthand for a where clause using "like". The field used is the display field defined in the map document. You can determine what the display field is for a layer in the Services Directory.
Example:query.text = stateName;
unitsString
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 spatial reference. This parameter only applies if supportsQueryWithDistance is
true
.Known Values: feet | miles | nautical-miles | us-nautical-miles | meters | kilometers
whereString
A where clause for the query. Any legal SQL where clause operating on the fields in the layer is allowed. Be sure to have the correct sequence of single and double quotes when writing the where clause in JavaScript.
Examples:query.where = "NAME = '" + stateName + "'";
query.where = "POP04 > " + population;
Method Overview
Name | Return Type | Summary | |
---|---|---|---|
Object | Converts an instance of this class to its ArcGIS Portal JSON representation. more details | more details |
Method Details
toJSON(){Object}
Converts an instance of this class to its ArcGIS Portal JSON representation. See the Using fromJSON() topic in the Guide for more information.
Returns:Type Description Object The ArcGIS Portal JSON representation of an instance of this class.