Overview

You will learn: how to build SQL and spatial expressions to get layer data.

One of the most powerful features of the ArcGIS platform is the ability to query and select data from hosted feature layers. Queries support SQL expressions but they can also include geometry and spatial relationship parameters. This means you can select records with a where clause, a geometry (point, line or polygon) and a spatial condition such as esriSpatialRelIntersects or esriSpatialRelContains. Queries return features with attribute and geometry information, and the results can be formatted as JSON, GeoJSON, or HTML. You can create queries and expressions interactively with the Map Viewer, the layer item page, at the definition of a layer view, or manually by accessing the ArcGIS REST layer endpoint directly. Visit the documentation to learn more about the REST API for layer queries and how to format SQL expressions.

In this lab you will learn how to execute queries against hosted feature layers with the ArcGIS REST API. We are just scratching the surface of what's possible, but this will be very helpful when you need to query data with the ArcGIS APIs.

Steps

Explore the layer data with ArcGIS Online

Before querying the layer, check to see what fields and values are available. The easiest way to do this is on the item details page.

  1. Go to the Trailheads layer item in ArcGIS Online.

  2. Click Data and familarize yourself with the data.

    • Identify the following fields: "FID", "TRL_NAME", "PARK_NAME" and "ZIP_CODE"
    • Note that some of the records have the string "Backbone" in the TRL_NAME field.
  3. Click Visualization and click on some of the trail features. Try to identify some trailheads with the name "Backbone" in them.

View the layer metadata with a URL

All hosted layers have a default REST endpoint that you can access with a GET or POST request to view the metadata and the layer capabilities. You can view the endpoint with a custom application, or with a browser (as seen below).

  1. In the layer item page, click Overview.

  2. Under Layers > Trailheads click Service URL. This is the default endpoint for the layer and it should bring up the metadata page for the layer.

https://services3.arcgis.com/GVgbJbqm8hXASVYi/ArcGIS/rest/services/Trailheads/FeatureServer/0

  1. Take a few minutes to explore the metadata. Fill in the values for the following attributes:
Attribute Value
Name                                     
Type
Geometry Type
Max Record Count
Use Standardized Queries
Fields: FID (type)
Fields: TRL_NAME (type)
Fields: TRL_ID (type)
Fields: PARK_NAME (type)
Fields: ZIP_CODE (type)
Supported Operations

Query for all attributes

Now let's use the query page to build an expression to return all of the records and field values.

  1. At the bottom of the metadata page, click Query.

  2. In the query page, set the following parameters to build a query expression to return all records and fields, but not the geometry:

Field Value
Where: FID > 0
Out Fields: *
Return Geometry: False
  1. At the bottom of the page click Query (GET) to run the query. The trimmed URL should look like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query? where=FID>0&outFields=*&returnGeometry=false

  1. Scroll down the page to view the results.

Query for all geometries

Now lets use the query page to build a different expression to return all of the geomteries but no attribute data.

  1. In the query page, set the query parameters as follows:
Field Value
Where: FID > 0
Out Fields: (leave this empty)
Return Geometry: True
Format: GEOJSON
  1. At the bottom, click Query (GET) to run the query. The trimmed URL should look like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query? where=FID>0&returnGeometry=true&f=pgeojson

  1. Scroll down the page to view the results.

Query for attributes and geometries

ArcGIS supports standard SQL expressions and spatial parameters. To learn more about the supported expressions, visit the SQL reference for query expressions.

  1. In the query page, reset the query parameters and build an expression to return just the records with Backbone:
Field Value
Where: TRL_NAME = 'Backbone Trail'
Out Fields: *
Return Geometry: True
Format: HTML
  1. At the bottom, click Query (GET) to run the query. The URL request should look like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/ query?where=TRL_NAME='Backbone Trail'&returnGeometry=true&outFields=*

  1. Scroll down your page to see the results. Note that both attributes and geometry information are returned.

Congratulations, you're done!

Challenge

Advanced spatial query

Take a few minutes to review the ArcGIS REST API documentation for Query (FeatureService/Layer) and identify the different geometry parameters that can be passed in. Let's build a query to find features within a certain distance of a location. This is also knowns as a "buffer search" or "search by distance".

  1. Go back to the query page.

  2. Reset the parameters and build an expression to return just the trailheads within 2 miles of Malibu (-118.7798,34.0259):

Field Value
Where:
Input Geometry: -118.7798,34.0259 Coordinates in longitude and latitude
Geometry Type: Point esriGeometryPoint
Input Spatial Reference: 4326 Geographic Coordinate System
Distance: 2 Distance to search (buffer distance)
Units: Miles Units applied to distance parameter above
Out Fields: *
Return Geometry: True
Format: HTML
  1. Click Query (Get) to run the query. The URL and results should look like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/ query?geometry=-118.7798,34.0259&geometryType=esriGeometryPoint&inSR=4326&distance=2 &units=esriSRUnit_StatuteMile&outFields=*&returnGeometry=true

  1. Try changing the distance parameter to 3, 4, or 5 miles and run the query again.

  2. Find the longitude and latitude of other cities on the coast and run the query again.