Select to view content in your preferred language

Arcade Code

705
2
08-10-2023 08:05 AM
Ed_
by MVP Regular Contributor
MVP Regular Contributor

So I am following this blog, Introducing Data Expressions in ArcGIS Dashboards to learn Arcade for use in Dashboards. 

Can someone please explain these two pieces of codes line by line?

// Empty dictionary to capture each hazard reported as separate rows.
var choicesDict = {'fields': [{ 'name': 'split_choices', 'type': 'esriFieldTypeString'}],
'geometryType': '', 'features': []}; // Need help understanding the syntax

 

var index = 0;

// Split comma separated hazard types and store in dictionary.
for (var feature in fs) {
var split_array = Split(feature["Report_road_condition"], ',')
var count_arr = Count(split_array)
for(var i = 0; i < count_arr; i++ ){
choicesDict.features[index++] = {
'attributes': { 'split_choices': Trim(split_array[i]), // Don't know what this line is doing
}}
}}

Question | Analyze | Visualize
Tags (1)
0 Kudos
2 Replies
JohannesLindner
MVP Alum

To post code:

JohannesLindner_0-1689266678674.pngJohannesLindner_1-1689266693900.png

 

var choicesDict = {
  'fields': [
    {'name': 'split_choices', 'type': 'esriFieldTypeString'},
    ],
  'geometryType': '',
  'features': []
  }

 

They are creating a Dictionary that is used to return the final Featureset. Featuresets are collections of features. They can be constructed from a Dictionary or from a JSON string (representing the dictionary).

A Featureset has to have the following keys:

  • fields: an Array of Dictionaries describing the fields of the Featureset. These dictionaries have the following keys:
    • name: String, the field's name
    • type: String, the field's type. The important types are:
      • esriFieldTypeString
      • esriFieldTypeInteger
      • esriFieldTypeDouble
      • esriFieldTypeDate
    • alias (optional): String, the field's alias
  • geometryType: String, the geometry type of the contained features. Possible values are
    • esriGeometryPoint
    • esriGeometryMultipoint
    • esriGeometryPolyline
    • esriGeometryPolygon
    • empty string (no geometry)
  • features: Array of Dictionaries describing the contained features

 

In the snippet above, they are creating this structure. They are declaring the featureset to be a non-spatial table (geometryType = "") with one String field called split_choices. They leave the contained features empty. This is a common workflow. You create your dictionary and then fill in the features in a later step (see next snippet).

 

The second snippet uses an old workflow, I'm going to update it here:

for (var f in fs) {
  var split_array = Split(f["Report_road_condition"], ',')
  for(var i in split_arr){
    var new_feature = {
      'attributes': { 'split_choices': Trim(split_array[i]) }
      }
    Push(choicesDict.features, new_feature)
  }
}

 

  1. Repeat the following block (until line 9) for each feature (f) in the input featureset (fs)
  2. Take the value in the field Report_road_condition and Split it by comma, creating an array of string values
  3. Repeat the following block (until line 😎 for each index in that array
  4. create a dictionary describing a feature
    1. these dictionaries have the following keys
      1. attributes: dictionary of the feature's attributes { field_name: field_value }
      2. geometry (optional): the feature's geometry
  5. Trim the current element of the array and use the output as value for the field split_choices
  6.  
  7. Push the feature we just created into the features array of the output dictionary

 

So in this step, they loop over each feature in the input featrueset, split a field value of that feature, and append each part to the output featureset. If the input featureset has one feature with the value "A, B, C", the output featureset will have three features with the values "A", "B", "C".

 

The last step in these workflows is to return the Dictionary as Featureset:

return Featureset(choicesDict)

Have a great day!
Johannes
0 Kudos
JohannesLindner
MVP Alum

It's also worth to note that there are two ways to address a dictionary key or field value (the example uses both, which is ugly...)

  • bracket notation with the dictionary key or field name as string:
    • dict["key"]
    • $feature["Field"]
  • dot notation
    • dict.key
    • $feature.Field

Have a great day!
Johannes
0 Kudos