Overview

You will learn: how to query data from ArcGIS Feature Services.

ArcGIS Feature Services can access, edit, and store spatial data in feature layers. Feature layers define the spatial reference, geometry type (points, lines or polygons), attribute fields, and the permission settings for the data. If you want to create an application that can access feature layer data you can use the ArcGIS REST API and the query operation. All you need to do is pass in the SQL where clause, and the service returns the records. The records can contain attribute and/or geometry information. You can also create complex expressions that include a geometry and a spatial relationship. To learn more about the capabilities of feature services, please visit the documentation.

In this lab you will use the ArcGIS REST API to access a host feature layer to query features with a SQL and geometry expression.

Before you begin

Install Postman to execute HTTP requests. Go to this lab if you need an access token.

Steps

Create a request to access the feature service

  1. Open Postman and click [+] in the tab bar to create a new request.

  2. In the new tab, set the following:

    • HTTP Method: POST
    • Request URL: https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query

Add parameters to define a where clause and how data should be returned

  1. Click on Body and add the following Key/Value pairs:

    • f: json
    • where: 1=1
    • outSr: 4326
    • outFields: TRL_NAME,ELEV_FT,CITY_JUR,PARK_NAME,FID

Execute the request to get the records

  1. Click Send to run the request.

  2. In the response window, change the view to JSON and it should look something like this:

    {
        "objectIdFieldName": "FID",
        "globalIdFieldName": "",
        "geometryType": "esriGeometryPoint",
        "spatialReference": {
            "wkid": 4326,
            "latestWkid": 4326
        },
        "fields": [
            {
                "name": "TRL_NAME",
                "type": "esriFieldTypeString",
                "alias": "TRL_NAME",
                "sqlType": "sqlTypeNVarchar",
                "length": 256,
                "domain": null,
                "defaultValue": null
            },
            {
                "name": "ELEV_FT",
                "type": "esriFieldTypeInteger",
                "alias": "ELEV_FT",
                "sqlType": "sqlTypeInteger",
                "domain": null,
                "defaultValue": null
            },
            {
                "name": "CITY_JUR",
                "type": "esriFieldTypeString",
                "alias": "CITY_JUR",
                "sqlType": "sqlTypeNVarchar",
                "length": 256,
                "domain": null,
                "defaultValue": null
            },
            {
                "name": "PARK_NAME",
                "type": "esriFieldTypeString",
                "alias": "PARK_NAME",
                "sqlType": "sqlTypeNVarchar",
                "length": 256,
                "domain": null,
                "defaultValue": null
            },
            {
                "name": "FID",
                "type": "esriFieldTypeInteger",
                "alias": "FID",
                "sqlType": "sqlTypeInteger",
                "domain": null,
                "defaultValue": null
            }
        ],
        "features": [
            {
                "attributes": {
                    "TRL_NAME": "Rio Hondo River Trail - Garfield Ave S Connector",
                    "ELEV_FT": 115,
                    "CITY_JUR": "South Gate",
                    "PARK_NAME": null,
                    "FID": 1
                },
                "geometry": {
                    "x": -118.16810669,
                    "y": 33.93990426
                }
            },
            {
                "attributes": {
                    "TRL_NAME": "Rio Hondo River Trail - Garfield Ave N Connector",
                    "ELEV_FT": 116,
                    "CITY_JUR": "South Gate",
                    "PARK_NAME": null,
                    "FID": 2
                },
                "geometry": {
                    "x": -118.16794515,
                    "y": 33.940011459999994
                }
            },
    ...
    
  3. Go to the top of the response and identify the main properties:

    • geometryType - the type of geometry this layer contains. Usually esriGeometryPoint, esriGeometryPolyline or esriGeometryPolygon.
    • spatialReference - the spatial reference that is used for data.
    • fields - contains an array of information about the requested fields.
    • features - an array of features from the database. Each feature will have attributes corresponding to the requested fields and a geometry with the coordinates.
  4. Go back and run the query again wiht the following where parameters:

    • ELEV_FT > 1000
    • CITY_JUR LIKE '%Los Angeles%'
    • PARK_NAME = 'National Parks Service' AND ELEV_FT > 250 and ELEV_FT < 1000

Update the parameters to define a spatial search expression

  1. In the Body, set the where parameter to 1=1 and then add these additional parameters:

    • geometryType: esriGeometryPolygon
    • spatialRel: esriSpatialRelContains
    • inSR: 4326
    • geometry:
    {
      "rings":[
        [
          [
            -118.806005698014,
            34.0082167272637
          ],
          [
            -118.805403625418,
            34.0088062261768
          ],
          [
            -118.805336612985,
            34.0088742313928
          ],
          [
            -118.80527760234,
            34.0089332353118
          ],
          [
            -118.804621479867,
            34.0095862804933
          ],
          [
            -118.804539464192,
            34.0096672859409
          ],
          [
            -118.804475458569,
            34.0095832759002
          ],
          [
            -118.804427453026,
            34.0095452717131
          ],
          [
            -118.804372445159,
            34.0095132667997
          ],
          [
            -118.804313437191,
            34.0094872627812
          ],
          [
            -118.804250428977,
            34.0094682595708
          ],
          [
            -118.804184419796,
            34.0094562568147
          ],
          [
            -118.804117409304,
            34.0094512547092
          ],
          [
            -118.804050399883,
            34.0094542528035
          ],
          [
            -118.803984389881,
            34.0094642526439
          ],
          [
            -118.803920379505,
            34.0094822528968
          ],
          [
            -118.803903376591,
            34.0094882529577
          ],
          [
            -118.803807360309,
            34.0095392553488
          ],
          [
            -118.803691339865,
            34.0096342610323
          ],
          [
            -118.80300921284,
            34.0103053067502
          ],
          [
            -118.802997210554,
            34.0103113076722
          ],
          [
            -118.802661148925,
            34.0106413295809
          ],
          [
            -118.802146053001,
            34.0111473648239
          ],
          [
            -118.80180899052,
            34.0114723868865
          ],
          [
            -118.801741981689,
            34.0114483822984
          ],
          [
            -118.800505817147,
            34.0109042996862
          ],
          [
            -118.800396802917,
            34.0108592924818
          ],
          [
            -118.800391631307,
            34.0108571763704
          ],
          [
            -118.800473816207,
            34.0108092898469
          ],
          [
            -118.800585836225,
            34.0107312862738
          ],
          [
            -118.800737863697,
            34.010600277594
          ],
          [
            -118.800849884225,
            34.0104792691973
          ],
          [
            -118.800948905097,
            34.010319256411
          ],
          [
            -118.801080931947,
            34.010133241197
          ],
          [
            -118.801457010843,
            34.0095321939997
          ],
          [
            -118.801636051924,
            34.009137161775
          ],
          [
            -118.801761078855,
            34.0089011423248
          ],
          [
            -118.801867103188,
            34.0086871254696
          ],
          [
            -118.80189311203,
            34.0085651141859
          ],
          [
            -118.801873111242,
            34.0085051077619
          ],
          [
            -118.801787102567,
            34.0083730931971
          ],
          [
            -118.801688102599,
            34.0079780531162
          ],
          [
            -118.801662107784,
            34.0077530315621
          ],
          [
            -118.80166911362,
            34.0076150180462
          ],
          [
            -118.801755132905,
            34.0074330026092
          ],
          [
            -118.801840151227,
            34.0072789905495
          ],
          [
            -118.801867158915,
            34.0071969837689
          ],
          [
            -118.801873163018,
            34.0071049749076
          ],
          [
            -118.801873166573,
            34.0070153880467
          ],
          [
            -118.801903143273,
            34.0069053016283
          ],
          [
            -118.801972188966,
            34.0067899478818
          ],
          [
            -118.802065205832,
            34.0066959415752
          ],
          [
            -118.802203230004,
            34.0066039361115
          ],
          [
            -118.802395262088,
            34.0065219330455
          ],
          [
            -118.802479983217,
            34.0064996420266
          ],
          [
            -118.802840897416,
            34.0064901592053
          ],
          [
            -118.80301683795,
            34.0064889886298
          ],
          [
            -118.803114370608,
            34.0065049496029
          ],
          [
            -118.803143870988,
            34.0064881432884
          ],
          [
            -118.803322647798,
            34.0064869533916
          ],
          [
            -118.803728925156,
            34.0064157264629
          ],
          [
            -118.803756010706,
            34.0064249195121
          ],
          [
            -118.803953494046,
            34.0065179723314
          ],
          [
            -118.804012840537,
            34.0065563438362
          ],
          [
            -118.804302536936,
            34.0067810070728
          ],
          [
            -118.804480560211,
            34.0068730202092
          ],
          [
            -118.804535925293,
            34.0069145448854
          ],
          [
            -118.804665580652,
            34.0070420405061
          ],
          [
            -118.804767772788,
            34.0070984229226
          ],
          [
            -118.80485660408,
            34.0071760585343
          ],
          [
            -118.80501385524,
            34.0072912553222
          ],
          [
            -118.805377666427,
            34.0076111125846
          ],
          [
            -118.805436871525,
            34.0076489298369
          ],
          [
            -118.805614120206,
            34.0078313448399
          ],
          [
            -118.805857397004,
            34.0080794710792
          ],
          [
            -118.805904013514,
            34.0081227792956
          ],
          [
            -118.806005698014,
            34.0082167272637
          ]
        ],
        [
          [
            -118.813287496818,
            34.0169872055835
          ],
          [
            -118.813418514996,
            34.0170212125524
          ],
          [
            -118.813855477666,
            34.016690475618
          ],
          [
            -118.813866593472,
            34.0167021941186
          ],
          [
            -118.81404437802,
            34.0168664777242
          ],
          [
            -118.813617538627,
            34.0171732319983
          ],
          [
            -118.813471522156,
            34.0170342149124
          ],
          [
            -118.813452517958,
            34.0170632171214
          ],
          [
            -118.813400511074,
            34.0170572155598
          ],
          [
            -118.813331501291,
            34.0170232100004
          ],
          [
            -118.813287496818,
            34.0169872055835
          ]
        ]
      ]
    }
    
  2. Click Send to run the request. You should see only two features that are contained by the polygon provided.

  1. In Postman click Code below the Send button. Select a programming language and use Postman to generate sample code for your application to run this request.

Congratulations, you're done!

You have successfully queried a feature layer with a SQL and geometry expression.

Challenge

Return the total number of matching features

If you only need the number of features matching your query, add returnCountOnly to the parameters with a value of true. You can learn more about returnCountOnly and other similar parameters such as returnIdsOnly and returnExtentOnly in the ArcGIS REST API Feature Layer query documentation.

Get statistics about query results

Instead of returning features directly, you can ask the ArcGIS REST API to calculate statistics about the results of your query, such as the min and max of a field. Try adding the outStatistics parameter with a JSON value such as:

[
  {
    "statisticType": "min",
    "onStatisticField": "ELEV_FT",
    "outStatisticFieldName": "MIN_ELEV_FT"
  },
  {
    "statisticType": "max",
    "onStatisticField": "ELEV_FT",
    "outStatisticFieldName": "MAX_ELEV_FT"
  },
  {
    "statisticType": "avg",
    "onStatisticField": "ELEV_FT",
    "outStatisticFieldName": "AVG_ELEV_FT"
  }
]

You can learn more about the outStatistics parameter in the ArcGIS REST API Feature Layer query documentation.