GEOG 863
GIS Mashups for Geospatial Professionals

6.2.5 Spatial Queries


If you’re an ArcGIS Desktop user, the sort of query we’ve dealt with so far has been analogous to the kind you’d build using the Select By Attributes dialog. Now let’s look at how you’d go about performing a query like one you’d build using the Select By Location dialog.

To implement a spatial query, the main properties of concern are geometry and spatialRelationship. The geometry should be set to some point, line or polygon that you’d like to check against the features in the layer you’re applying the query to. The spatialRelationship should be set to a string defining the sort of check to run, such as "intersects", "overlaps", "touches", etc.

For example, let’s say you were writing an app in which the user was given the ability to draw a shape on the map and you want to identify the land parcels that intersect that shape. The basic steps for carrying out this sort of operation would be to:

  1. Get a reference to the land parcel layer.
  2. Create a Query, setting its geometry to the user-defined shape and its spatialRelationship to "intersects".
  3. Invoke the queryFeatures() method on the parcel layer.
  4. Write code that handles the FeatureSet returned by queryFeatures().

Have a look at this example, which essentially carries out the Jen & Barry’s site selection queries (minus the ones having to do with the recreation areas and highways).

Again, here is a list of the main points to take away from this script:

  • A series of variables holding the selection criteria are defined at the top of the script, making it easier to modify those values if desired.
  • Those values are incorporated into a couple of where clause variables using string concatenation.
  • After the cities and counties layers have loaded, the county-level query is executed and the resulting FeatureSet is passed along to a function called findGoodCities().
  • The findGoodCities() function is defined to store that FeatureSet in a variable called goodCounties.
  • In findGoodCities(), a Query is created to be used against the cities layer.
  • The expression goodCounties.features returns the array of Graphics that met the county-level criteria.
  • The JavaScript forEach method is used to loop through the array of county Graphics. On each pass through the loop, the geometry property of the cities Query is set to the geometry of the county in that iteration of the loop.
  • The cities Query is then applied to the cities layer, with the FeatureSet from that query being passed to the callback function called displayResults().
  • The displayResults() function is similar to how it looked in the previous example, creating a yellow graphic for the cities identified by the query. One difference is that embedded within the map() function is a console.log statement that prints the name of the city currently being processed. The expression graphic.attributes.NAME returns the value from the NAME field for the current city. Other field values can be retrieved in this way also, provided they are included in the outFields list when defining the Query. (Be sure to open your browser’s developer console to see the results listed.)
  • Note that cityQuery was used here to evaluate both attribute and spatial criteria at the same time.
  • We’ll see how to write content like the cities list to a more user-friendly place in the next lesson when we look at user interface development.