Skip to content

Learn how to access the spatial analysis service using user authentication.

Prerequisites

Steps

Get the starter app

  1. Go to the Sign in with user authentication (server) tutorial and download the solution.

  2. Unzip the folder and open it in a text editor of your choice.

  3. Navigate to the user-auth-basic folder.

  4. Install the required ArcGIS REST JS packages as defined in package.json.

    Use dark colors for code blocksCopy
    1
    npm install

Set up authentication

Create a new OAuth credential to register the application.

  1. Go to the Create OAuth credentials for user authentication tutorial to create an OAuth credential. Set the redirect URL to https://<YOUR_SERVER>[:YOUR_PORT]/authenticate, for example https://localhost:3000/authenticate.
  2. Copy the Client ID and Redirect URL from your OAuth credentials item and paste them to a safe location. It will be used in the next step.

Set developer credentials

  1. Open the config.json file and paste your developer credentials, replacing the placeholders with your actual values.
    config.json
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    {
      "clientId": "YOUR_CLIENT_ID",
      "redirectUri": "YOUR_REDIRECT_URI",
      "portal": "https://www.arcgis.com/sharing/rest"
    }

Make a job request

  1. Import Job and JOB_STATUSES from arcgis-rest-request and the portal helper functions from @esri/arcgis-rest-portal.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    
    import express from "express";
    import fs from "fs";
    import path from "path";
    import { Job, JOB_STATUSES, ArcGISIdentityManager } from "@esri/arcgis-rest-request";
    import { getSelf } from "@esri/arcgis-rest-portal";
    
  2. Create a function to run the hot spot analysis. This function submits a job to the spatial analysis service, using your organization’s analysis URL and authenticating with a session.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    async function runHotSpotAnalysis(session) {
      const portalSelf = await getSelf({ authentication: session });
      const analysisUrl = portalSelf.helperServices.analysis.url;
    
      const params = {
        analysisLayer: {
          url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/sf_traffic_parking_violations_sa_osapi/FeatureServer/0"
        },
        shapeType: "Hexagon",
        outputName: {
          serviceProperties: {
            name: `RestJS_find_hot_spots_${Date.now()}`
          }
        }
      };
    
      const jobReq = await Job.submitJob({
        url: `${analysisUrl}/FindHotSpots/submitJob`,
        params,
        authentication: session
      });
    
      jobReq.on(JOB_STATUSES.Status, (info) => {
        console.log("Job Status:", info.status);
      });
    
      const jobResults = await jobReq.getAllResults();
      return jobResults;
    }
  3. Call the function after authentication. Exchange the authorization code for a session, then run the hot spot analysis.

    Expand
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
        ArcGISIdentityManager.exchangeAuthorizationCode(credentials, req.query.code)
          .then((session) => {
            res.status(200).send(
              "Session successfully acquired. Check your server console to see job status and analysis results as the job completes."
            );
    
            return runHotSpotAnalysis(session);
          })
          .then((jobResults) => {
            console.log("Hot spot analysis results: ", JSON.stringify(jobResults, null, 2));
          })
          .catch((err) => {
            console.error("Error:", err);
            res.status(500).send(err.message);
          });
    
    Expand
  4. Save the file, then run it from the terminal.

    Use dark colors for code blocksCopy
    1
    node index.js
  5. Open the URL shown in your terminal (e.g., https://localhost:3000/authorize) and sign in with your ArcGIS account. After signing in, the hot spot analysis job will start automatically. You can monitor its progress in your console. Once the job completes, the console will display the analysis metadata, including the URL of the resulting feature layer.

The analysis metadata should look like this:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
  "hotSpotsResultLayer": {
    "paramName": "hotSpotsResultLayer",
    "dataType": "GPString",
    "value": {
      "url": "FEATURE_SERVICE_URL",
      "itemId": "ITEM_ID"
    }
  },
  "processInfo": {
    "paramName": "processInfo",
    "dataType": "GPString",
    "value": [
      "{\"messageCode\": \"SS_00002\", \"message\": \"The following report outlines the workflow used to optimize your Find Hot Spots result:\", \"params\": {}, \"style\": \"<b></b><br/>\"}",
      "{\"message\": \"Initial Data Assessment\", \"messageCode\": \"SS_84428\", \"params\": {}, \"style\": \"<u><b></b></u><br/>\"}",
Expand

What's next?

Learn how to perform additional spatial analyses in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.