Select to view content in your preferred language

Arcade script" IF/Else trouble

251
3
Jump to solution
11-03-2025 03:35 PM
Labels (1)
Yeaton
by
Regular Contributor

I'm having trouble with an if/else statement for an expression that evaluates the intersection of two polygons.

I get the first return, or the correct value(s) for the "if" portion, but nothing for the else portion.

Thanks for any help.

Best,

Aaron Y.

// Define a variable to hold the FeatureSet of the intersecting layer.
// Replace "Polygon Layer Name" with the actual name of your polygon layer in the map.
var intersectLayer = FeatureSetByName($map, "Future Land Use Designations");

// Find the features in 'intersectLayer' that intersect the current feature ($feature).
var intersectingFeatures = Intersects(intersectLayer, $feature);

// Initialize an empty array to store the attributes you want to display.
var attributesToDisplay = [];

// Iterate through the intersecting features and extract desired attributes.
// Replace "AttributeField1" and "AttributeField2" with the actual field names from your polygon layer.
for (var f in intersectingFeatures) {
    Push(attributesToDisplay, f['FLUM_Cat']);
}
//Remove square brackets from array and and "/" instead of comma
var myarray = attributesToDisplay;
var arrayasstring = Text(attributesToDisplay);
var nobrakets= Replace(Replace(Replace(arrayasstring, '[', ''), ']', ''),',','/'); 

var NoBracketsDist = Distinct(nobrakets)


// Check if any intersecting features were found.
if (Count(NoBracketsDist) > 0) {
    return "This parcel is within the following Land Use Designation Categories:" + " "+ Concatenate(NoBracketsDist);}

else if (Count(NoBracketsDist) == 0) {
    return "This parcel is outside of the Land Use Designation Categories";
}

layers

0 Kudos
1 Solution

Accepted Solutions
CodyPatterson
MVP Regular Contributor

Hey @Yeaton 

I think that your function here is turning nobrakets into a string, which Distinct needs a list or array, so it's turning your string into a list, so "example" would be e,x,a,m,p,l,e so it's always going to show up wrong.

In this case, I would something like this:

// Define a variable to hold the FeatureSet of the intersecting layer.
// Replace "Polygon Layer Name" with the actual name of your polygon layer in the map.
var intersectLayer = FeatureSetByName($map, "Future Land Use Designations");

// Find the features in 'intersectLayer' that intersect the current feature ($feature).
var intersectingFeatures = Intersects(intersectLayer, $feature);

// Initialize an empty array to store the attributes you want to display.
var attributesToDisplay = [];

// Iterate through the intersecting features and extract desired attributes.
// Replace "AttributeField1" and "AttributeField2" with the actual field names from your polygon layer.
for (var f in intersectingFeatures) {
    Push(attributesToDisplay, f['FLUM_Cat']);
}
//Remove square brackets from array and and "/" instead of comma
var myarray = attributesToDisplay;

var NoBracketsDist = Distinct(myarray)

var joined = Concatenate(NoBracketsDist, "/");


// Check if any intersecting features were found.
if (Count(joined) > 0) {
    return "This parcel is within the following Land Use Designation Categories:" + " "+ Concatenate(joined);}

else if (Count(joined) == 0) {
    return "This parcel is outside of the Land Use Designation Categories";
}

The Concatenate function can replace the commas with / if you'd like by default, no need to replace, here's the docs for that: https://developers.arcgis.com/arcade/function-reference/text_functions/

Let me know if that helps!

Cody

View solution in original post

3 Replies
CodyPatterson
MVP Regular Contributor

Hey @Yeaton 

I think that your function here is turning nobrakets into a string, which Distinct needs a list or array, so it's turning your string into a list, so "example" would be e,x,a,m,p,l,e so it's always going to show up wrong.

In this case, I would something like this:

// Define a variable to hold the FeatureSet of the intersecting layer.
// Replace "Polygon Layer Name" with the actual name of your polygon layer in the map.
var intersectLayer = FeatureSetByName($map, "Future Land Use Designations");

// Find the features in 'intersectLayer' that intersect the current feature ($feature).
var intersectingFeatures = Intersects(intersectLayer, $feature);

// Initialize an empty array to store the attributes you want to display.
var attributesToDisplay = [];

// Iterate through the intersecting features and extract desired attributes.
// Replace "AttributeField1" and "AttributeField2" with the actual field names from your polygon layer.
for (var f in intersectingFeatures) {
    Push(attributesToDisplay, f['FLUM_Cat']);
}
//Remove square brackets from array and and "/" instead of comma
var myarray = attributesToDisplay;

var NoBracketsDist = Distinct(myarray)

var joined = Concatenate(NoBracketsDist, "/");


// Check if any intersecting features were found.
if (Count(joined) > 0) {
    return "This parcel is within the following Land Use Designation Categories:" + " "+ Concatenate(joined);}

else if (Count(joined) == 0) {
    return "This parcel is outside of the Land Use Designation Categories";
}

The Concatenate function can replace the commas with / if you'd like by default, no need to replace, here's the docs for that: https://developers.arcgis.com/arcade/function-reference/text_functions/

Let me know if that helps!

Cody

Yeaton
by
Regular Contributor

Hi Cody,

Your solution works great! Thanks for the help!

Best,

Aaron

Corbinb
New Contributor

If you’re running into IF/ELSE trouble in an Arcade script, it usually comes down to syntax issues, incorrect field references, or a logic flow that doesn’t evaluate the way you expect. In Arcade, each condition must be clearly defined, strings must be in quotes Read more here, and fields must be wrapped in $feature.fieldname. Also remember that Arcade stops evaluating once it finds a true condition, so ordering your IF/ELSEIF statements correctly is essential.