GEOG 863
GIS Mashups for Geospatial Professionals

6.2.4 Selecting Features


Simply getting a count of the features meeting certain criteria is sometimes sufficient, but it’s often necessary to work with the features themselves. To do that, you use the queryFeatures() method. As with queryFeatureCount(), queryFeatures() returns a promise. The difference is that the queryFeatures() promise resolves to a FeatureSet object rather than a Number.

Have a look at this example which uses the same Query where clause as the previous ones and displays the counties meeting the criterion as graphics. Note the following important points:

  • A new empty GraphicsLayer object is created to hold the counties meeting the query criterion.
  • The Query has its returnGeometry property set to true. This is necessary to be able to map the results.
  • The queryFeatures() method is executed after the counties layer has finished loading (line 41).
  • Following on example code from the SDK page on promises, the FeatureSet that gets returned by the queryFeatures() method is passed along to a function called displayResults(). A key part of making this work is using the JavaScript keyword return to specify that the FeatureSet is the return value of the counties layer’s callback function.
  • The displayResults() function is defined such that the FeatureSet returned by queryFeatures() is stored in a variable called results.
  • Getting at the counties in the FeatureSet is done by reading its features property, which returns an array of Graphic objects.
  • The array of Graphic objects is immediately passed to a JavaScript array method called map. JavaScript developers use the map() method to do something to each item in an array and return the result as a new array. In this case, the anonymous function plugged into the map() method changes the symbol of each graphic to a yellow SimpleFillSymbol.
  • The new array of Graphic objects is added to the GraphicsLayer (in the resultsLayer variable).

So far, where and returnGeometry are the only Query properties we’ve looked at. However, there are several others that are important in certain contexts. Here is a brief description of just a few of these properties:

  • num – the maximum number of features to return; often used together with the start property to implement paging of results
  • orderByFields – an array of fields to use for sorting the results
  • outFields – an array of fields to include in the results; limiting this array to only what you need can improve performance

There are actually a few more Query properties that I think are worth discussing, but I left them out of this list because they’re considered in greater depth in the next section on spatial queries.