Select to view content in your preferred language

Popup calculations with Arcade - with null values

103
2
Jump to solution
Wednesday
Labels (2)
Jennifer_T
New Contributor

Hi, I'm trying to calculate the areas and lengths of intersecting layers within a selected polygon. I found a really helpful script here: Solved: Popup calculations with Arcade - Esri Community. It has gotten me to 90% to where I need to be. The problem is that this arcade script doesn't like blank or null values. I believe HasKey is the culprit. I need these "blank" values calculated in the popup (and renamed if possible). Is there a solution or workaround? maybe incorporating DefautValue or IsEmpty somehow? 

Thank you

var managementareas = FeatureSetByName($map,"ManagmentAreas");

var int_ma = Intersects($feature, managementareas)

var int_dict = {}

var xs = 0

for (var i in int_ma){
    var xs = AreaGeodetic(Intersection($feature, i), 'acres')
    if(HasKey(int_dict,i.Name)){
        int_dict[i.NAME] += xs
    } else {
        int_dict[i.NAME] = xs
    }

var out_str = 'Management Area Acreage: ';

for (var d in int_dict){
    out_str += '\n' + d + '\t|\t' + Text(int_dict[d], '#,###.#')
}}

return out_str

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Create a key that uses a substitute string if i.NAME is blank (line 9) and use that key for the dictionary (lines 10, 11, and 13). Note that I moved the calculation of out_str outside the for loop. It doesn't need to be calculated for each item in the intersected FeatureSet.

var managementareas = FeatureSetByName($map, "ManagmentAreas");
var int_ma = Intersects($feature, managementareas);

var int_dict = {};
var xs = 0;

for (var i in int_ma) {
  var xs = AreaGeodetic(Intersection($feature, i), "acres");
  var key = iif(IsEmpty(i.NAME), "Empty", i.NAME);
  if (HasKey(int_dict, key)) {
    int_dict[key] += xs;
  } else {
    int_dict[key] = xs;
  }
}

var out_str = "Management Area Acreage: ";
for (var d in int_dict) {
  out_str += "\n" + d + "\t|\t" + Text(int_dict[d], "#,###.#");
}

return out_str;

 

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

Create a key that uses a substitute string if i.NAME is blank (line 9) and use that key for the dictionary (lines 10, 11, and 13). Note that I moved the calculation of out_str outside the for loop. It doesn't need to be calculated for each item in the intersected FeatureSet.

var managementareas = FeatureSetByName($map, "ManagmentAreas");
var int_ma = Intersects($feature, managementareas);

var int_dict = {};
var xs = 0;

for (var i in int_ma) {
  var xs = AreaGeodetic(Intersection($feature, i), "acres");
  var key = iif(IsEmpty(i.NAME), "Empty", i.NAME);
  if (HasKey(int_dict, key)) {
    int_dict[key] += xs;
  } else {
    int_dict[key] = xs;
  }
}

var out_str = "Management Area Acreage: ";
for (var d in int_dict) {
  out_str += "\n" + d + "\t|\t" + Text(int_dict[d], "#,###.#");
}

return out_str;

 

Jennifer_T
New Contributor

Amazing! this did the trick

0 Kudos