Select to view content in your preferred language

Arcade script" IF/Else trouble

91
2
Jump to solution
yesterday
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

2 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