Select to view content in your preferred language

Execution Error in Arcade Script

764
10
08-08-2024 10:27 AM
jacob_ekn
Occasional Contributor

Hello, I'm writing a script for a Dashboard where I have lots of points and want the user to be able to export data on points that land in fire zones or weather events. The script creates a list of the polygons each point is within (under the condition that it's within a fire zone or weather event in the first place) and adds them as one string to a new field. I made sort of a variation of a script written by @jcarlson linked in another post I had seen. It looks like this:

var portal = Portal("https://www.arcgis.com");

var fire_poly = FeatureSetByPortalItem(
    portal,
    'd957997ccee7408287a963600a77f61f',
    1,
    [
        'IncidentName'
    ],
    true
);

var weather_poly = FeatureSetByPortalItem(
    portal,
    'a6134ae01aad44c499d12feec782b386',
    6,
    [
        'Event',
        'Severity'
    ],
    true
);

var pt_fs = FeatureSetByPortalItem(
    portal,
    'item_id_removed_for_security',
    0,
    [
        'SAP_EQUIP_',
        'SAP_STRUCT',
        'TLINE_NM',
        'SAP_FUNC_L',
        'GIS_LAT',
        'GIS_LONG'
    ],
    true
);

var features = [];
var feat;

for (var pnt in pt_fs) {
    var in_fire = [];
    var in_event = [];

    if (Within(pnt, fire_poly) || Within(pnt, weather_poly)) {
        for (var poly in fire_poly) {
            if (Within(pnt, poly)) {
                Push(in_fire, poly['IncidentName']);
            }
        }

        for (var poly in weather_poly) {
            if (Within(pnt, poly)) {
                Push(in_event, poly['Severity'] + " " + poly['Event'])
            }
        }

        feat = {
            'attributes': {
                'SAP_EQUIP': pnt['SAP_EQUIP_'],
                'STRUCT_NO': pnt['SAP_STRUCT'],
                'TLINE_NM': pnt['TLINE_NM'],
                'FUNC_LOC': pnt['SAP_FUNC_L'],
                'GIS_LAT': pnt['GIS_LAT'],
                'GIS_LONG': pnt['GIS_LONG'],
                'fire': IIf(IsEmpty(First(in_fire)), "N/A", Concatenate(in_fire, ", ")),
                'weather_event': IIf(IsEmpty(First(in_event)), "N/A", Concatenate(in_event, ", "))
            }
        };

        Push(features, feat);
    }
}

var out_dict = {
    'fields': [
        {'name': 'SAP_EQUIP', 'alias': 'SAP Equipment ID', 'type': 'esriFieldTypeString'},
        {'name': 'STRUCT_NO', 'alias': 'Structure Number', 'type': 'esriFieldTypeString'},
        {'name': 'TLINE_NM', 'alias': 'Transmission Line', 'type': 'esriFieldTypeString'},
        {'name': 'FUNC_LOC', 'alias': 'SAP Functional Location', 'type': 'esriFieldTypeString'},
        {'name': 'GIS_LAT', 'alias': 'Latitude', 'type': 'esriFieldTypeDouble'},
        {'name': 'GIS_LONG', 'alias': 'Longitude', 'type': 'esriFieldTypeDouble'},
        {'name': 'fire', 'alias': 'Name of Fire', 'type': 'esriFieldTypeString'},
        {'name': 'weather_event', 'alias': 'Weather Event', 'type': 'esriFieldTypeString'}
    ],
    'geometryType': '',
    'features': features
};

return FeatureSet(out_dict);

It won't run in Dashboards and when I put it in Playground it gives me this: Test execution error: Execution error - Logical expression can only be combined with || or &&. Verify test data. Since Playground doesn't tell me what line the error is from I'm not really sure what the issue is or what to try to change. Hoping it's just something I'm overlooking.

0 Kudos
10 Replies
KenBuja
MVP Esteemed Contributor

Your problem is on line 46 (as well as 48 and 54). Within returns a FeatureSet, which can't be directly evaluated in an if statement. You have to return some sort of boolean, such as

if (Count(Within(pnt, fire_poly)) > 0 || Count(Within(pnt, weather_poly) > 0)) {

 

 

0 Kudos
jacob_ekn
Occasional Contributor

Thank you! It's interesting because the function reference says that Within returns a boolean, but this got rid of that error. However, I'm now getting an "unknown error" so I guess my struggle continues for now.

0 Kudos
KenBuja
MVP Esteemed Contributor

I was looking at the FeatureSet Within instead of the Feature Within. Still, when I ran a test on the Feature Within, it returns a FeatureSet.

Snag_1997644.png

I misplaced a parenthesis in the second Count. Try this instead.

if (Count(Within(pnt, fire_poly)) > 0 || Count(Within(pnt, weather_poly)) > 0) {

 

 

0 Kudos
jacob_ekn
Occasional Contributor

Yes I changed it to that, but there's still an unknown error, I'm guessing it's probably a different issue somewhere else in the script.

0 Kudos
jacob_ekn
Occasional Contributor

(By the way, there's a missing semicolon on line 55 that I already fixed but I don't seem to be able to edit my post)

0 Kudos
KenBuja
MVP Esteemed Contributor

You can use Console to test out where the script is breaking. If I have a problem with the script, I sprinkle them throughout to find the problem line.

As it turns out, I was incorrect for lines 48 and 54. I was mixing up the Withins. The one on line 46 is a FeatureSet Within and doesn't return a Boolean, while the other two are Geometry Withins which do return a Boolean.

    for (var poly in fire_poly) {
      if (Within(pnt, poly)) {
        Push(in_fire, poly["IncidentName"]);
      }
    }

    for (var poly in weather_poly) {
      if (Within(pnt, poly)) {
        Push(in_event, poly["Severity"] + " " + poly["Event"]);
      }
    }

 

0 Kudos
jacob_ekn
Occasional Contributor
for (var pnt in pt_fs) {
    var in_fire = [];
    var in_event = [];
    var has_fire = Count(Within(pnt, fire_poly)) > 0;
    var has_weather = Count(Within(pnt, weather_poly)) > 0;
    Console("Outer for loop iterating.");

    if (has_fire || has_weather) {
        Console("First conditional passed.");

I gave this a try and got a good number of iterations on the outer loop (I'm guessing on points that weren't within any polygons) then eventually got the unknown error again.

0 Kudos
KenBuja
MVP Esteemed Contributor

You'd have to use this.

for (var pnt in pt_fs) {
    var in_fire = [];
    var in_event = [];
    var has_fire = Within(pnt, fire_poly);
    var has_weather = Within(pnt, weather_poly);
    Console("Outer for loop iterating.");

    if (has_fire || has_weather) {
        Console("First conditional passed.");

I was testing on some public data and got it to work

var portal = Portal("https://www.arcgis.com");

var fire_poly = FeatureSetByPortalItem(
  portal,
  "d957997ccee7408287a963600a77f61f",
  1,
  ["IncidentName"],
  true
);

var weather_poly = FeatureSetByPortalItem(
  portal,
  "a6134ae01aad44c499d12feec782b386",
  6,
  ["Event", "Severity"],
  true
);

var pt_fs = Top(FeatureSetByPortalItem(
  portal,
  "9e2f2b544c954fda9cd13b7f3e6eebce",
  0,
  ["*"],
  true
), 80);

var features = [];
var feat;
for (var pnt in pt_fs) {
  var in_fire = [];
  var in_event = [];
  if (Count(Within(pnt, fire_poly)) > 0 || Count(Within(pnt, weather_poly)) > 0) {
    
    for (var poly in fire_poly) {
      if (Within(pnt, poly)) {
        Push(in_fire, poly["IncidentName"]);
      }
    }

    for (var poly in weather_poly) {
      if (Within(pnt, poly)) {
        Push(in_event, poly["Severity"] + " " + poly["Event"]);
      }
    }
console(pnt)
    feat = {
      attributes:
        {
          SAP_EQUIP: pnt["place"],
          fire: IIf(IsEmpty(First(in_fire)), "N/A", Concatenate(in_fire, ", ")),
          weather_event:
            IIf(IsEmpty(First(in_event)), "N/A", Concatenate(in_event, ", "))
        }
    };

    Push(features, feat);
  }
}

var out_dict = {
  fields: [
    { name: "SAP_EQUIP", alias: "SAP Equipment ID", type: "esriFieldTypeString" },
    { name: "fire", alias: "Name of Fire", type: "esriFieldTypeString" },
    {
      name: "weather_event",
      alias: "Weather Event",
      type: "esriFieldTypeString"
    }
  ],
  geometryType: "",
  features: features
};

return FeatureSet(out_dict);

Snag_1e40a1f.png

0 Kudos
jacob_ekn
Occasional Contributor

Is it possible that the size of my point dataset is too large and that could be causing an error? The point feature class has around 150k points.

0 Kudos