Query Data with the ArcGIS Server API for Flex

Posted on May 10, 2010. Filed under: AGIS Server API for Flex, ArcGIS Server, GeoSpatial Training Services |

The next session of our Programming the ArcGIS Server API for Flex class begins June 7th.  There are still seats available.

ArcGIS Server can perform a number of different tasks.  In this article we will introduce the Query Task and you’ll see how it can be used with the ArcGIS Server API for Flex to query data in a map service.  A Query Task can be an attribute, spatial, or combination query that can be performed against the data layers in a map service.  Some examples would perhaps be illustrative at this point.  An attribute query might search for all land parcels with a valuation of greater than $100,000.  A spatial query could be used to find all land parcels that intersect a 100 year floodplain, and a combination query might search for all land parcels with a valuation of greater than $100,000 and whose geometry intersects the 100 year floodplain.

A Query object is used as input to a QueryTask and is defined by properties including geometry, where, and text.  The geometry property  is used to input a geometry that will be used in a spatial query and will be a point, line, or polygon geometry.  The where property is used to define an attribute query while the text property is used to perform a where clause containing a ‘like’ modifier.  The Query object can also contain a number of optional properties including the ability to define the fields that will be returned as a result of the query, the output spatial reference for the return geometry, and the actual geometry of the features that meet the query conditions.

Once you’ve defined the input properties in a Query object you can then use QueryTask to execute the query against a layer. You must provide a pointer to the layer that will be queried.  This pointer should be an integer based value.  Notice in the code example on this slide that we are pointing to the 6th layer in the ESRI_CENSUS_USA map service.  Although the integer shows a value of 5 it is actually the sixth layer since it is a zero based array.  In other words, the first layer has an integer value of 0.  Execute returns a FeatureSet object that contains the results of the query and these features are processed through a callback function which is specified in the execute( ) method.

As I mentioned earlier, the results of a query are stored in a FeatureSet object which is simply an array of Graphics which you can then plot on your map if you wish.  Using this object you can set field aliases for the returned fields as well as a display field name.
Here we provide a demonstration of using the query object in the ArcGIS Server API for Flex as well as the code that was used in the demonstration.  The code for this demonstration is provided by the ESRI Samples site.
Attribute queries can be specified using one of two properties on the Query class.  The ‘text’ property searches the display field of your layer in a literal fashion.  It functions as a ‘like’ operator.  The ‘where’ property is used to define a traditional where clause for your query.
Spatial queries use the geometry property to apply a spatial filter to the query.  The geometry used as input will need to have been determined before the query is executed.  In addition, a spatialRelationship property is also used to apply a spatial relationship to the input geometry with SPATIAL_REL_INTERSECTS being the default if not specified.  As you can see from the figure below there are many spatialRelationship tests that you can apply to filter your data based on a spatial relationship.
You can restrict the data returned by a query in a number of ways.  The fields, spatial reference, and geometry can all be restricted.  For performance reasons you should try to limit the fields returned by the query through the use of the outFields property.  This is simply a set of field names provided within an array.  To return all fields you can use an asterisk as a wildcard.  As I mentioned, you should attempt to limit the fields returned to only those that you will use as it will increase the performance of your application.  Depending upon your application you may or may not want to return the geometry of each feature that matched the query.  Using the returnGeometry property you can specify a true or false value to indicate whether or not you wish to return the geometry for each feature.  If you don’t need to plot the geometry on your application by all means do not return the geometry as it will increase the performance of your application.  You can also specify the spatial reference for any returned geometry through the outSpatialReference property.
Since queries return data sets you will often use the results of the queries in data binding.  For example, you may want to populate the contents of a grid with the information returned from a query.  Flex data binding provides an easy way of doing this without a lot of coding.  Data binding is a Flex feature, not an ArcGIS Server API for Flex feature, but you can easily put it to use in your applications.  Essentially, data binding is a way of passing information around in your application in a sort of publish-subscribe pattern.  Data is published in one way or another such as the result of a QueryTask, and then one or more components in your application can monitor for this data and consume it when it becomes available.

In the figure below you’ll see a depiction of how data binding can be used with queries in Flex.  Here, the FeatureSet returned by our QueryTask is bound to a Flex grid component.  The contents of the grid will automatically be populated with the contents of the FeatureSet without the need for a lot of coding.  As you can see from the code snippet, data binding is accomplished through the use of curly braces that surround the data to be bound to the component.  Data binding in Flex is specified with these curly braces.

As I mentioned, curly braces are used to define the binding between a data source and the component that it will be bound to.  In the example code below we are binding the results of a QueryTask to a Flex DataGrid.
About these ads

Make a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.

Join 47 other followers

%d bloggers like this: